实现输入法TSF接口的代码不需要写在Main函数中,也不需要引用System.Windows.Forms,一般是在一个独立的类库工程中进行编码,然后编译成DLL文件并注册到Windows系统中,系统会自动加载和调用输入法TSF接口中的方法。
在Visual Studio中创建一个Class Library工程,然后添加一个类InputMethod并实现ITfTextInputProcessor接口,示例代码如下:
csharp Copy code
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.Win32;
namespace MyInputMethod
{
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[ClassInterface(ClassInterfaceType.None)]
public class InputMethod : ITfTextInputProcessor
{
public InputMethod()
{
// 初始化输入法
}
#region ITfTextInputProcessor
public uint Activate(ITfThreadMgr pThreadMgr, ITfClientId cliid)
{
// 输入法激活
return 0;
}
public uint Deactivate()
{
// 输入法撤销
return 0;
}
public uint OnActivated(ITfDocumentMgr pdimFocus, ref Guid rguidService)
{
// 文档激活
return 0;
}
public uint OnDeactivated(ITfDocumentMgr pdimFocus, ref Guid rguidService)
{
// 文档撤销
return 0;
}
public uint OnKeyDown(WPARAM wParam, LPARAM lParam, ITfThreadMgr pThreadMgr)
{
// 按键按下事件处理
return 0;
}
public uint OnKeyUp(WPARAM wParam, LPARAM lParam, ITfThreadMgr pThreadMgr)
{
// 按键抬起事件处理
return 0;
}
public uint OnSetFocus(bool fForeground)
{
// 输入法窗口获取焦点事件处理
return 0;
}
public uint OnKillFocus(bool fForeground)
{
// 输入法窗口失去焦点事件处理
return 0;
}
public uint GetSelection(ITfContext pContext, out ITfRange ppRange)
{
// 获取选择的文本范围
ppRange = null;
return 0;
}
public uint GetText(ITfContext pContext, uint dwFlags, out TextCookie pCookie, out string pbstrText)
{
// 获取文本
pCookie = null;
pbstrText = string.Empty;
return 0;
}
public uint RequestEditSession(ITfContext pContext, IntPtr hwndOwner, ref TF_HALTCOND phc, ITfEditSession pes, uint dwFlags, out uint pdwSessionId)
{
// 请求文本编辑会话
pdwSessionId = 0;
return 0;
}
#endregion
}
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITfTextInputProcessor
{
uint Activate(ITfThreadMgr pThreadMgr, ITfClientId cliid);
uint Deactivate();
uint OnActivated(ITfDocumentMgr pdimFocus, ref Guid rguidService);
uint OnDeactivated(ITfDocumentMgr pdimFocus, ref Guid rguidService);
uint OnKeyDown(WPARAM wParam, LPARAM lParam, ITfThreadMgr pThreadMgr);
uint OnKeyUp(WPARAM wParam, LPARAM lParam, ITfThreadMgr pThreadMgr);
uint OnSetFocus(bool fForeground);
uint OnKillFocus(bool fForeground);
uint GetSelection(ITfContext pContext, out ITfRange ppRange);
uint GetText(ITfContext pContext, uint dwFlags, out TextCookie pCookie, out string pbstrText);
uint RequestEditSession(ITfContext pContext, IntPtr hwndOwner, ref TF_HALTCOND phc, ITfEditSession pes, uint dwFlags, out uint pdwSessionId);
}
[StructLayout(LayoutKind.Sequential)]
public struct TF_HALTCOND
{
public IntPtr pfnHalt;
public IntPtr pv1;
public IntPtr pv2;
public IntPtr pv3;
}
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITfEditSession
{
void DoEditSession(TfEditCookie ec);
}
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITfRange
{
void GetText(TfEditCookie ec, uint dwFlags, IntPtr psz, int cchMax, out int pcch);
void GetFormattedText(TfEditCookie ec, IntPtr ppData, out int pcb, ITfRange pRange);
void GetEmbedded(TfEditCookie ec, ref Guid rguidService, ref Guid riid, out IntPtr ppunk);
void GetStart(TfEditCookie ec, out int plPos);
void GetEnd(TfEditCookie ec, out int plPos);
void GetGravity(out TfGravity pGravity);
void SetGravity(TfEditCookie ec, TfGravity gravity);
void ShiftStart(TfEditCookie ec, int cchReq, out int pcch);
void ShiftEnd(TfEditCookie ec, int cchReq, out int pcch);
void ShiftStartToRange(TfEditCookie ec, ITfRange pRange, TfAnchor paStart);
void ShiftEndToRange(TfEditCookie ec, ITfRange pRange, TfAnchor paEnd);
void ShiftEndToRangeGravity(TfEditCookie ec, ITfRange pRange, TfAnchor paEnd);
void SetEndPoint(TfEditCookie ec, ITfRange pRange, TfAnchor anchor);
void GetTextAndAttributes(TfEditCookie ec, uint dwFlags, IntPtr pacpStart, IntPtr pchText, out int pcch, IntPtr ppAttributeRanges, out int pcAttributeRanges);
void FindNextAttrTransition(TfEditCookie ec, int acpStart, int acpHalt, out int pacpNext, TfActiveSelEnd pSel);
void FindPrevAttrTransition(TfEditCookie ec, int acpStart, int acpHalt, out int pacpPrev, TfActiveSelEnd pSel);
void AdviseTextStoreSink(ITfContext pic, ITfRange pRange, bool fRegister);
void UnadviseTextStoreSink(ITfContext pic);
}
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITfThreadMgr
{
void Activate(out uint pdwClientId);
void Deactivate();
void CreateDocumentMgr(out ITfDocumentMgr ppdim);
void EnumDocumentMgrs(out IEnumTfDocumentMgrs ppEnum);
void GetFocus(out ITfDocumentMgr ppdimFocus);
void SetFocus(ITfDocumentMgr pdimFocus);
}
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITfClientId
{
}
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ITfDocumentMgr
{
void CreateContext(uint dwFlags, IntPtr punk, out ITfContext ppic);
void Push(ITfContext pic);
void Pop(uint dwFlags);
void AssociateFocus(IntPtr hwnd, ITfDocumentMgr pdimPrev, out ITfDocumentMgr ppdimNew);
void GetFocus(out ITfContext ppic);
void SetFocus(ITfContext pic);
void GetTop(out ITfContext ppic);
void GetBase(out ITfContext ppic);
}
[Flags]
public enum TfEditCookie
{
TF_EC_READLOCK = 0x01,
TF_EC_WRITELOCK = 0x02,
TF_EC_DELETE = 0x04,
TF_EC_INSERTBYDRAGANDDROP = 0x08,
TF_EC_SELECTION = 0x10,
TF_EC_REPLACE = 0x20,
TF_EC_COMPOSITION = 0x40,
TF_EC_RECONVERTSTRING = 0x80,
TF_EC_INSERTFROMSIP = 0x100,
TF_EC_INSERTFROMDRAGANDDROP = 0x200,
}
public enum TfGravity
{
TF_GRAVITY_BACKWARD = 0,
TF_GRAVITY_FORWARD = 1
}
public enum TfAnchor
{
TF_ANCHOR_START = 0,
TF_ANCHOR_END = 1
}
public enum TfActiveSelEnd
{
TF_ASE_NONE = 0,
TF_ASE_SOM = 1,
TF_ASE_EOM = 2
}
public enum WPARAM : uint
{
VK_BACK = 0x08,
VK_TAB = 0x09,
VK_RETURN = 0x0D,
VK_SHIFT = 0x10,
VK_CONTROL = 0x11,
VK_MENU = 0x12,
VK_CAPITAL = 0x14,
VK_KANA = 0x15,
VK_ESCAPE = 0x1B,
VK_SPACE = 0x20,
VK_PRIOR = 0x21,
VK_NEXT = 0x22,
VK_END = 0x23,
VK_HOME = 0x24,
VK_LEFT = 0x25,
VK_UP = 0x26,
VK_RIGHT = 0x27,
VK_DOWN = 0x28,
VK_SNAPSHOT = 0x2C,
VK_INSERT = 0x2D,
VK_DELETE = 0x2E,
VK_HELP = 0x2F,
VK_LWIN = 0x5B,
VK_RWIN = 0x5C,
VK_APPS = 0x5D,
VK_NUMPAD0 = 0x60,
VK_NUMPAD1 = 0x61,
VK_NUMPAD2 = 0x62,
VK_NUMPAD3 = 0x63,
VK_NUMPAD4 = 0x64,
VK_NUMPAD5 = 0x65,
VK_NUMPAD6 = 0x66,
VK_NUMPAD7 = 0x67,
VK_NUMPAD8 = 0x68,
VK_NUMPAD9 = 0x69,
VK_MULTIPLY = 0x6A,
VK_ADD = 0x6B,
VK_SEPARATOR = 0x6C,
VK_SUBTRACT = 0x6D,
VK_DECIMAL = 0x6E,
VK_DIVIDE = 0x6F,
VK_F1 = 0x70,
VK_F2 = 0x71,
VK_F3 = 0x72,
VK_F4 = 0x73,
VK_F5 = 0x74,
VK_F6 = 0x75,
VK_F7 = 0x76,
VK_F8 = 0x77,
VK_F9 = 0x78,
VK_F10 = 0x79,
VK_F11 = 0x7A,
VK_F12 = 0x7B,
VK_F13 = 0x7C,
VK_F14 = 0x7D,
VK_F15 = 0x7E,
VK_F16 = 0x7F,
VK_F17 = 0x80,
VK_F18 = 0x81,
VK_F19 = 0x82,
VK_F20 = 0x83,
VK_F21 = 0x84,
VK_F22 = 0x85,
VK_F23 = 0x86,
VK_F24 = 0x87,
VK_NUMLOCK = 0x90,
VK_SCROLL = 0x91,
VK_LSHIFT = 0xA0,
VK_RSHIFT = 0xA1,
VK_LCONTROL = 0xA2,
VK_RCONTROL = 0xA3,
VK_LMENU = 0xA4,
VK_RMENU = 0xA5
}
public enum LPARAM : int
{
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WM_CHAR = 0x102,
WM_IME_STARTCOMPOSITION = 0x10D,
WM_IME_ENDCOMPOSITION = 0x10E
}
}