笔者通过实践和摸索,查找了相关的IME资料,利 用了WINDOWSAPI函数,实现了IME的功能。
常用函数有:
API函数:BOOLImmSimulateHotKey
(HWNDhWnd,DWORDdwHotKeyID);//模拟热键
其中Hwnd为程序窗口的句柄,dwHotHKeyID
为模拟的热键,若成功则返回True
HKLGetKeyboardLayout(DWORDdwLayout);
//获得当前键盘状态
BOOLImmIsIME(HKLhKL);
//判断当前是否处于中文输入状态,若是则返回True
自定义函数:
打开相应输入法:OpenIme(imename:string),
例OpenIme(全拼输入法);
关闭中文输入法:CloseIme;
以下是一个简单的例子,仅起参考作用。
使用时uses中加上imm
具体的实现方法及源代码如下:
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Classes,
Graphics,Controls,Forms,Dialogs,
StdCtrls,Buttons,imm;
type
TForm1=class(TForm)
ComboBox1:TComboBox;
BitBtn1:TBitBtn;
BitBtn2:TBitBtn;
BitBtn3:TBitBtn;
procedureFormShow(Sender:TObject);
procedureOpenIme(imename:string);
procedurecloseIme;
procedureComboBox1Change(Sender:TObject);
procedureBitBtn1Click(Sender:TObject);
procedureBitBtn2Click(Sender:TObject);
procedureBitBtn3Click(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
{$R*.DFM}
procedureTForm1.FormShow(Sender:TObject);
var
j:integer;
begin
forj:=0toscreen.imes.count-1do
begin
ComBoBox1.Items.Add(screen.Imes.strings[j]);
//获取系统中已安装的中文输入法
end;
end;
procedureTform1.OpenIme(imename:string);
var
I:integer;
myhkl:hkl;
begin
ifImeName<>then
begin
ifScreen.Imes.Count<>0then
begin
I:=scr .Imes.indexof(imename);
ifI>=0then
myhkl:=hkl(screen.Imes.objects[i]);
activatekeyboardlayout(myhkl,
KLF_ACTIVATE);//设置相应的输入法
end;
end;
end;
procedureTForm1.closeime;
var
myhkl:hkl;
begin
myhkl:=GetKeyBoardLayOut(0);
ifImmIsIME(myhkl)then
//判断是否在中文状态,若是则关闭它
immsimulateHotkey(handle,
IME_CHotKey_IME_NonIME_Toggle);
end;
procedureTForm1.ComboBox1Change(Sender:TObject);
begin
OpenIme(ComboBox1.Text);
end;
procedureTForm1.BitBtn1Click(Sender:TObject);
begin
immsimulateHotkey(handle,
IME_CHotKey_shape_Toggle);//切换半角和全角模式
end;
procedureTForm1.BitBtn2Click(Sender:TObject);
begin
immsimulateHotkey(handle,
IME_CHotKey_symbol_Toggle);
//切换中文标点模式和英文标点模式
end;
procedureTForm1.BitBtn3Click(Sender:TObject);
begin
closeime;
end;
end.