ITfLangBarItemButton是用于TSF输入法中的语言栏项按钮接口。它允许开发者为输入法添加自定义按钮,并在用户点击时执行特定操作。下面是C#语言实现ITfLangBarItemButton的示例代码:
首先,我们需要实现一个类来继承ITfLangBarItemButton接口:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using mshtml;
using MSR;
namespace TSFTest
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(ITfLangBarItemButton))]
public class TSFBarItem : ITfLangBarItemButton
{
private const string kIconFile = @"C:\MyIcon.ico";
private const string kTooltipText = "My TSF Input Panel Button";
#region ITfLangBarItemButton Members
public string Text
{
get { return "My TSF Button"; }
}
public string ToolTipText
{
get { return kTooltipText; }
}
public void OnClick(TfLBIClick click, POINT point, ServiceProvider serviceProvider)
{
// 在这里添加您按钮的点击事件逻辑
MessageBox.Show("您点击了自定义按钮!");
}
public IntPtr Icon
{
get { return LoadIconFromFile(kIconFile); }
}
public bool InitMenu(Menu menu)
{
// 您可以在这里添加菜单项以及子菜单
return false;
}
#endregion
#region ITfLangBarItem Members
public Guid GUID
{
get { return Guid.NewGuid(); }
}
public string Description
{
get { return "My TSF Button"; }
}
public TfLBIStyle Style
{
get { return TfLBIStyle.TfLBIStyleBtnFace; }
}
public bool IsDisabled
{
get { return false; }
}
public void OnUpdate(TfLBIClick click, POINT point, [MarshalAs(UnmanagedType.Interface)] IUnknown data, out TfLBINFO pTfLBInfo)
{
pTfLBInfo = new TfLBINFO();
// 设置按钮的图标和文本
pTfLBInfo.dwStyle = TfLBIS.dwStyle | TfLBIS.TfLBIS_HasIcon | TfLBIS.TfLBIS_HasText;
pTfLBInfo.clrBackColor = TfLBIS.clrBackColor;
pTfLBInfo.clrTextColor = TfLBIS.clrTextColor;
pTfLBInfo.hIcon = Icon;
pTfLBInfo.tooltip = ToolTipText;
pTfLBInfo.szText = Text;
}
#endregion
#region Private Methods
private static IntPtr LoadIconFromFile(string filename)
{
// 从文件中加载图标
var icon = new Icon(filename);
return icon.Handle;
}
#endregion
}
}
在上述代码中,我们通过实现ITfLangBarItemButton接口来完成按钮的实现。在按钮被点击时,我们会弹出一个简单的消息框作为响应。
其中,InitMenu方法可以用来添加菜单项和子菜单,我们在示例代码中没有使用到。GUID属性用于标识该按钮在输入法中的位置,这里我们采用了一个新的随机GUID。Description属性表示该按钮的描述信息。Style属性表示该按钮的样式,我们采用了默认的TfLBIStyleBtnFace。IsDisabled属性用于判断按钮是否已禁用。
在实现了ITfLangBarItemButton接口后,需要将其注册到输入法中才能生效,可以在TSF输入法激活时,将它注册到输入法中:
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using MSR;
namespace TSFTest
{
[ComVisible(true)]
[Guid("F99F88DD-0FE1-4388-BD20-506E32964946")]
[ClassInterface(ClassInterfaceType.None)]
public class TSFInputMethod : ITfTextInputProcessorEx
{
private const string kClsid = "{F99F88DD-0FE1-4388-BD20-506E32964946}";
private const string kProgId = "MyTsfInputMethod";
private const string kAppId = "MyTsfInputMethod";
private const string kTipTitle = "My Input Panel";
private readonly TSFBarItem _barItem = new TSFBarItem();
#region ITfTextInputProcessorEx Members
public void ActivateEx(out bool isActivated)
{
isActivated = true;
// 注册输入法
RegisterInputMethod();
}
...
#endregion
#region Private Methods
private static void RegisterInputMethod()
{
var hKey = Registry.CurrentUser.CreateSubKey($@"SOFTWARE\Microsoft\CTF\TIP\{kClsid}", true);
hKey.SetValue("Model", kProgId, RegistryValueKind.String);
hKey.SetValue("InProcServer32", $@"{AppDomain.CurrentDomain.BaseDirectory}\MyTsfInputMethod.dll", RegistryValueKind.String);
hKey.SetValue("FunctionProvider", kClsid, RegistryValueKind.String);
hKey.SetValue("DisplayName", kTipTitle, RegistryValueKind.String);
var appKey = hKey.CreateSubKey("AppPaths", true);
appKey.SetValue(kAppId, $@"{AppDomain.CurrentDomain.BaseDirectory}\MyTsfInputMethod.exe", RegistryValueKind.String);
appKey.Close();
hKey.SetValue("LanguageProfile", "00000804:" + kProgId, RegistryValueKind.String);
hKey.Close();
}
#endregion
}
}
在示例代码中,我们定义了一个新的类TSFInputMethod来代表一个TSF输入法,并在ActivateEx方法中注册了输入法。在注册输入法时,我们将TSFBarItem作为输入法的一个语言栏项并添加到输入法中。这样,在用户打开输入法时,就会在语言栏中看到一个自定义的按钮,当用户点击它时,会触发OnClick方法,我们在OnClick方法中弹出了一个消息框来展示点击事件的效果。
需要注意的是,在实现了按钮和输入法的代码后,还需要在项目的属性中设置“启用原生代码调试”属性为False,否则在Debug模式下可能会因为调试器与TSF有冲突而导致异常。