TSF输入法是基于Windows操作系统的,可以使用Windows消息机制来实现如图标点击、显示菜单等操作。以下是一个简单的示例代码,用于实现TSF输入法的中英文切换图标。假设我们在Windows Forms应用程序中创建了一个名为Form1的窗体,并在窗体中添加了一个名为NotifyIcon1的通知栏图标控件。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace TSFInputMethod
{
public partial class Form1 : Form
{
private Icon iconEN = new Icon("icon_EN.ico");
private Icon iconCN = new Icon("icon_CN.ico");
private static readonly Guid CLSID_TF_ThreadMgr = new Guid("{529A9E6B-6587-4F23-AB9E-9C7D683E3C50}");
private ITfThreadMgr threadMgr;
private int langCount;
public Form1()
{
InitializeComponent();
InitThreadMgr();
UpdateLanguageIcon();
}
private void InitThreadMgr()
{
// 获取TSF线程管理对象
threadMgr = (ITfThreadMgr)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_TF_ThreadMgr));
if (threadMgr == null)
{
throw new Exception("Failed to initialize TSF thread manager");
}
// 注册语言条目改变事件
ITfSource source = (ITfSource)threadMgr;
if (source != null)
{
source.AdviseSink(new LanguageBarSink(this), out int cookie);
}
// 获取可用的语言条目数
ITfInputProcessorProfiles profiles = (ITfInputProcessorProfiles)new InputProcessorProfilesClass();
if (profiles != null)
{
profiles.GetLanguageList(out int[] langIds, out langCount);
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && langCount >= 2)
{
SwitchKeyboardLayout();
}
}
private void SwitchKeyboardLayout()
{
// 获取当前输入法线程
threadMgr.GetFocus(out ITfDocumentMgr docMgr);
docMgr.GetBase(out ITfInputProcessorProfiles profiles, out Guid clsid);
// 切换到下一个输入法语言
TF_LANGUAGEPROFILE[] profilesNext = new TF_LANGUAGEPROFILE[1];
profiles.Next(langCount, profilesNext, out int fetched);
profiles.SetActiveLanguageProfile(ref clsid, ref profilesNext[0].clsid, ref profilesNext[0].langid);
UpdateLanguageIcon();
}
private void UpdateLanguageIcon()
{
// 获取当前输入法语言
threadMgr.GetFocus(out ITfDocumentMgr docMgr);
docMgr.GetBase(out ITfInputProcessorProfiles profiles, out Guid clsid);
int langId = 0;
profiles.GetActiveLanguageProfile(ref clsid, out Guid guid, out langId);
// 根据当前输入法语言切换图标
if (langId == 0x0409) // 英文
{
notifyIcon1.Icon = iconEN;
}
else if (langId == 0x0804) // 中文(简体)
{
notifyIcon1.Icon = iconCN;
}
}
}
// 语言条目改变事件回调
internal class LanguageBarSink : ITfLanguageProfileNotifySink
{
private Form1 form;
public LanguageBarSink(Form1 form)
{
this.form = form;
}
public void OnLanguageChange(int langId, out bool pfAccept)
{
pfAccept = true;
form.UpdateLanguageIcon();
}
public void OnLanguageChanged()
{
form.UpdateLanguageIcon();
}
}
}
在代码中,我们使用了以下重要的TSF接口:
ITfThreadMgr接口用于获取当前输入法线程。
ITfDocumentMgr接口用于获取当前输入法涉及的文档对象。
ITfInputProcessorProfiles接口用于获取可用的输入法条目和设置当前激活的输入法条目。
ITfLanguageProfileNotifySink接口用于处理语言条目改变事件。
在代码中,我们创建了一个名为iconEN的英文图标和一个名为iconCN的中文图标,分别用于在中英文模式切换时更新通知栏图标。我们还创建了一个InitThreadMgr()方法,用于获取TSF线程管理对象,并注册LanguageBarSink回调函数来接受并处理语言条目改变事件。在SwitchKeyboardLayout()方法中,我们获取当前输入法线程,以及通过SetActiveLanguageProfile()方法来切换到下一个输入法语言。在UpdateLanguageIcon()方法中,我们获取当前输入法语言,并根据语言类型来切换通知栏图标。