Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

TSF输入法开发,怎么创建中英文模式图标?怎么才能让它显示在输入法图标左侧

  1. User

    TSF输入法开发,怎么创建中英文模式图标?怎么才能让它显示在输入法图标左侧

  2. Assistant

    创建中英文模式图标可以使用图片制作工具(如Photoshop等)或者在线图标制作工具(如iconfont、阿里巴巴矢量图标库等)制作。一般可以使用不同颜色或者不同字体来区分中英文模式。

    要让它显示在输入法图标左侧,需要在代码中添加相应的属性。具体方法如下:

    1. 打开输入法项目中的AndroidManifest.xml文件,找到对应输入法服务的节点(一般是service节点)。

    2. 在节点下添加meta-data子节点,设置name属性为com.sohu.inputmethod.sogou.SogouInputMethod类中的一个常量:INPUT_METHOD_ICON。

    3. 设置value属性为一个drawable资源,此资源即为输入法左侧图标的资源。

    4. 保存文件并重新编译运行输入法项目,左侧的图标就会显示出来了。

    示例代码如下:

    <service android:name=".SogouInputMethod"
            android:permission="android.permission.BIND_INPUT_METHOD">
            <meta-data android:name="com.sohu.inputmethod.sogou.INPUT_METHOD_ICON"
                   android:resource="@drawable/my_icon" />
            <!-- 其他属性 -->
    </service>
    

    其中@drawable/my_icon为左侧图标的资源文件,需要在项目中先添加此drawable资源。

  3. User

    windows TSF输入法开发,怎么用代码创建 中英文模式 图标?怎么才能让它显示在输入法图标左侧

  4. Assistant

    要用代码创建中英文模式图标并让它显示在输入法图标左侧,需进行以下步骤:

    1. 创建图标

    需要使用GDI+创建图标,并且使用具有图标大小的位图(比如16x16、32x32等)来绘制图标。可以使用GDI+的绘图函数(比如Graphics.DrawImage)将中文模式和英文模式的图标绘制到位图上,并保存为一个图标文件(.ico格式)。

    示例代码:

    HANDLE hIcon = nullptr;
    Bitmap* pBitmap = new Bitmap(16, 16);
    Graphics* pGraphics = Graphics::FromImage(pBitmap);
    // 在位图上绘制中文模式图标
    Image* pChineseIcon = Image::FromFile(L"ChineseIcon.png");
    pGraphics->DrawImage(pChineseIcon, 0, 0, 16, 16);
    // 在位图上绘制英文模式图标
    Image* pEnglishIcon = Image::FromFile(L"EnglishIcon.png");
    pGraphics->DrawImage(pEnglishIcon, 0, 0, 16, 16);
    // 将位图保存为图标文件
    IStream* pStream;
    if (CreateStreamOnHGlobal(nullptr, TRUE, &pStream) == S_OK)
    {
        pBitmap->Save(pStream, &CLSID_StandardPicture, nullptr);
        OleSaveToStream(static_cast<IPicture*>(pBitmap->GetThumbnailImage(16, 16)), pStream);
        ULARGE_INTEGER ulSize;
        LARGE_INTEGER liZero = { 0 };
        pStream->Seek(liZero, STREAM_SEEK_END, &ulSize);
        HGLOBAL hGlobal = GlobalAlloc(GPTR, ulSize.LowPart);
        if (hGlobal != nullptr)
        {
            pStream->Seek(liZero, STREAM_SEEK_SET, nullptr);
            pStream->Read(hGlobal, ulSize.LowPart, nullptr);
            hIcon = CreateIconFromResourceEx(reinterpret_cast<PBYTE>(GlobalLock(hGlobal)), ulSize.LowPart, TRUE, 0x00030000, 16, 16,
                LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
            GlobalUnlock(hGlobal);
            GlobalFree(hGlobal);
        }
        pStream->Release();
    }
    delete pEnglishIcon;
    delete pChineseIcon;
    delete pGraphics;
    delete pBitmap;
    
    1. 设置输入法图标

    在输入法服务(ITfInputProcessorProfiles)上设置输入法图标,即使用SetIcon()方法设置输入法图标的图标句柄、标签和UID。

    示例代码:

    ITfInputProcessorProfiles* pProfiles = nullptr;
    CLSID clsidTSF = { 0 };
    CoCreateGuid(&clsidTSF);
    if (CoCreateInstance(CLSID_TF_InputProcessorProfiles, nullptr, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfiles, reinterpret_cast<void**>(&pProfiles)) == S_OK)
    {
        WCHAR szText[64] { L"My Input Method" };
        TF_INPUTPROCESSORPROFILE tipp = { 0 };
        tipp.dwType = TF_PROFILETYPE_INPUTPROCESSOR;
        tipp.langid = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
        tipp.guidProfile = clsidTSF;
        tipp.hIcon = hIcon;
        StringCchCopyW(tipp.szDescription, ARRAYSIZE(tipp.szDescription), szText);
        StringCchCopyW(tipp.szProfileImeReg, ARRAYSIZE(tipp.szProfileImeReg), clsidTSF);
        StringCchCopyW(tipp.langid, ARRAYSIZE(tipp.langid), L"zh-CN");
        StringCchCopyW(tipp.guidProfile, ARRAYSIZE(tipp.guidProfile), clsidTSF);
        if (pProfiles->RegisterLanguageProfile(clsidTSF, tipp.langid, clsidTSF, ARRAYSIZE(tipp.szDescription), tipp.szDescription, ARRAYSIZE(tipp.szProfileImeReg),
            tipp.szProfileImeReg, ARRAYSIZE(tipp.guidProfile), tipp.guidProfile, &tipp, 0) == S_OK)
        {
            if (pProfiles->SetIcon(clsidTSF, hIcon) == S_OK)
            {
                // 成功设置图标
            }
        }
        pProfiles->Release();
    }
    

    以上代码中,hIcon为之前创建的图标句柄。设置图标后,输入法的图标就会出现在输入法列表中,且左侧会显示输入法图标。

  5. User

    以上设置输入法图标的代码,使用C#语言应该怎么写?

  6. Assistant

    以C#语言实现以上设置输入法图标的代码,可参考以下示例代码:

    1. 创建图标

    使用Image类加载中文模式和英文模式的图标,并使用Graphics绘制到位图上,最后保存为图标文件。示例代码:

    using System.Drawing;
    using System.Drawing.Imaging;
    
    private Icon CreateIcon()
    {
        Icon icon = null;
    
        using (Bitmap bitmap = new Bitmap(16, 16))
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            // 绘制中文模式图标
            Image chineseIcon = Image.FromFile("ChineseIcon.png");
            graphics.DrawImage(chineseIcon, 0, 0, 16, 16);
            // 绘制英文模式图标
            Image englishIcon = Image.FromFile("EnglishIcon.png");
            graphics.DrawImage(englishIcon, 0, 0, 16, 16);
    
            // 保存为图标文件
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                byte[] iconData = stream.ToArray();
                IntPtr hIcon = NativeMethods.CreateIconFromResourceEx(iconData, iconData.Length, true, 0x00030000, 16, 16, 0);
                if (hIcon != IntPtr.Zero)
                {
                    icon = Icon.FromHandle(hIcon);
                }
            }
        }
    
        return icon;
    }
    
    private static class NativeMethods
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr CreateIconFromResourceEx(byte[] pbIconBits, int cbIconBits, bool fIcon, uint dwVersion, int cxDesired, int cyDesired, uint uFlags);
    }
    
    1. 设置输入法图标

    使用InputProcessorProfiles类设置输入法图标,即使用SetIcon()方法设置输入法图标的图标句柄、标签和UID。示例代码:

    using System.Runtime.InteropServices;
    using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
    
    private void SetInputMethodIcon()
    {
        InputProcessorProfiles profiles = new InputProcessorProfiles();
    
        try
        {
            Icon icon = CreateIcon();
    
            if (icon != null)
            {
                string inputMethodId = "{YOUR_GUID_HERE}";
    
                // 设置输入法图标
                PropertySystemNativeMethods.PROPVARIANT pvIcon = new PropertySystemNativeMethods.PROPVARIANT();
                pvIcon.SetIUnknown(icon.Handle);
    
                profiles.SetIcon(inputMethodId, pvIcon);
    
                Marshal.ReleaseComObject(icon);
            }
        }
        finally
        {
            Marshal.ReleaseComObject(profiles);
        }
    }
    

    以上代码中,InputProcessorProfiles是一个封装了ITfInputProcessorProfiles接口的类,可调用SetIcon()等方法设置输入法属性。PropertySystemNativeMethods.PROPVARIANT是一个用于访问Windows属性系统的结构体,用于传递输入法图标句柄。必须将图标句柄包装在PROPVARAINT中,并使用IPropertyStore.SetValue()方法设置输入法图标属性。

    请注意将“{YOUR_GUID_HERE}”替换为您的输入法GUID。

  7. User

    windows 开发C# 语言怎么创建SystemTray 对象

  8. Assistant

    在Windows上,System Tray也称为Notification Area,是任务栏上的区域,用于显示应用程序图标、状态和通知。在C#语言中,创建System Tray对象可以使用NotifyIcon类。NotifyIcon类允许您将应用程序的图标添加到System Tray中,并在图标上显示通知。

    以下是使用C#语言创建System Tray对象的代码示例:

    using System;
    using System.Windows.Forms;
    
    public class MySystemTrayApp : Form
    {
        private NotifyIcon notifyIcon;
      
        public MySystemTrayApp()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = new System.Drawing.Icon("myicon.ico");
            notifyIcon.Visible = true;
          
            // 将双击图标事件与ShowMessage方法绑定
            notifyIcon.DoubleClick += new EventHandler(ShowMessage);
        }
      
        private void ShowMessage(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }
      
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
    		
            // 释放NotifyIcon资源
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }
      
        public static void Main()
        {
            Application.Run(new MySystemTrayApp());
        }
    }
    

    在上述代码中,我们创建了一个MySystemTrayApp类,并在构造函数中创建NotifyIcon对象并设置图标和可见性,然后绑定双击图标的事件,并在该事件中显示了一个MessageBox。在关闭应用程序时,需要释放NotifyIcon资源,所以Override了OnFormClosing方法。最后,通过Application.Run方法启动应用程序。

    请注意,在notifyIcon.Icon = new System.Drawing.Icon("myicon.ico");中,需要将“myicon.ico”替换为应用程序的图标文件。若果没有图标文件,可以使用Visual Studio中的图标编辑器创建一个新图标。

  9. User

    怎么控制NotifyIcon 的显示位置?

  10. Assistant

    在C#语言中,NotifyIcon类用于显示应用程序的图标和通知,而且可以通过设置其Location属性来使用自定义位置显示NotifyIcon图标。

    以下是使用C#语言控制NotifyIcon显示位置的代码示例:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MySystemTrayApp : Form
    {
        private NotifyIcon notifyIcon;
      
        public MySystemTrayApp()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = new Icon("myicon.ico");
            notifyIcon.Visible = true;
    
            // 设置NotifyIcon在屏幕上的位置
            notifyIcon.Location = new Point(100, 100);
    
            // 将双击图标事件与ShowMessage方法绑定
            notifyIcon.DoubleClick += new EventHandler(ShowMessage);
        }
    
        private void ShowMessage(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }
    
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            
            // 释放NotifyIcon资源
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }
    
        public static void Main()
        {
            Application.Run(new MySystemTrayApp());
        }
    }
    

    在上述代码中,我们创建了一个MySystemTrayApp类,并在构造函数中设置了NotifyIcon的图标、可见性和位置。在设置位置时,我们使用Point类设置NotifyIcon在屏幕上的位置。设置完成后,通过双击图标的事件ShowMessage显示了一个MessageBox。在关闭应用程序时,需要释放NotifyIcon资源,所以Override了OnFormClosing方法。最后,通过Application.Run方法启动应用程序。

    请注意,如果设置NotifyIcon的位置与任务栏位置重叠,则NotifyIcon会自动重新定位,以避免与任务栏重叠。另外,NotifyIcon并不是在窗体上显示的,而是在任务栏上显示的。因此,设置NotifyIcon的位置时,必须使用屏幕坐标而不是窗体坐标。

  11. User

    怎么让它显示在输入法图标的旁边?

  12. Assistant

    要让NotifyIcon显示在输入法图标旁边,需要在NotifyIcon的Location属性中设置正确的位置。通常情况下,输入法图标的位置是固定的,因此我们只需要获取输入法图标的位置,然后设置NotifyIcon的位置即可。

    以下是使用C#语言让NotifyIcon显示在输入法图标旁边的代码示例:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MySystemTrayApp : Form
    {
        private NotifyIcon notifyIcon;
      
        public MySystemTrayApp()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = new Icon("myicon.ico");
            notifyIcon.Visible = true;
    
            // 设置NotifyIcon在输入法图标旁边的位置
            Point inputMethodIconLocation = GetInputMethodIconLocation();
            notifyIcon.Location = new Point(inputMethodIconLocation.X - notifyIcon.Size.Width - 5, inputMethodIconLocation.Y);
    
            // 将双击图标事件与ShowMessage方法绑定
            notifyIcon.DoubleClick += new EventHandler(ShowMessage);
        }
    
        private void ShowMessage(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }
    
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            
            // 释放NotifyIcon资源
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }
    
        public static void Main()
        {
            Application.Run(new MySystemTrayApp());
        }
    
        private Point GetInputMethodIconLocation()
        {
            IntPtr taskBarWnd = NativeMethods.FindWindow("Shell_TrayWnd", null);
            IntPtr trayWnd = NativeMethods.FindWindowEx(taskBarWnd, IntPtr.Zero, "TrayNotifyWnd", null);
            IntPtr sysPager = NativeMethods.FindWindowEx(trayWnd, IntPtr.Zero, "SysPager", null);
            IntPtr rebarWnd = NativeMethods.FindWindowEx(sysPager, IntPtr.Zero, "ReBarWindow32", null);
            IntPtr toolBarWnd = NativeMethods.FindWindowEx(rebarWnd, IntPtr.Zero, "MSTaskSwWClass", null);
            IntPtr inputMethodIconWnd = NativeMethods.FindWindowEx(toolBarWnd, IntPtr.Zero, null, "输入");
    
            NativeMethods.RECT rect = new NativeMethods.RECT();
            NativeMethods.GetWindowRect(inputMethodIconWnd, ref rect);
    
            return new Point(rect.Left, rect.Top);
        }
    }
    
    public static class NativeMethods
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);
    
        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hwnd, ref RECT lpRect);
    
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    }
    

    在上述代码中,我们创建了一个MySystemTrayApp类,并在构造函数中获取输入法图标的位置,并设置NotifyIcon在输入法图标旁边的位置。在GetInputMethodIconLocation方法中,我们使用FindWindowEx函数获取系统托盘的窗口句柄,以及输入法图标的窗口句柄。然后使用GetWindowRect函数获取输入法图标在屏幕上的位置。最后返回输入法图标的位置。请注意,GetWindowRect函数返回的坐标是屏幕坐标。

    设置完NotifyIcon位置后,通过双击图标的事件ShowMessage显示了一个MessageBox。在关闭应用程序时,需要释放NotifyIcon资源,所以Override了OnFormClosing方法。最后,通过Application.Run方法启动应用程序。

  13. User

    以上代码 ,系统提示NotifyIcon 不包含Location的定义

  14. Assistant

    很抱