试试下面的代码:
// --------------------------------------------------
// 切换到指定的输入法
//
// SetActivateIme('CHAJEI.IME');
==> 切换到仓额输入法
// SetActivateIme('Phon.ime');
==> 切换到注音输入法
// 传入空字符串时, 切换到英数输入法
// --------------------------------------------------
function SetActivateIme(sWanted: string): boolean;
var
iHandleCount : integer;
pList : array[1..nHKL_LIST] of HKL;
szImeFileName : array[0..MAX_PATH] of char;
sImeFileName : string;
bInstalled : boolean;
i : integer;
begin
Result := False;
sWanted := AnsiUpperCase(sWanted);
// 传入空字符串, 切成英数输入模式
if Length(sWanted) = 0 then
begin
ToChinese(0, False);
Result := True;
Exit;
end;
// 看看是否安装了这个输入法
bInstalled := False;
iHandleCount := GetKeyboardLayoutList(nHKL_LIST, pList);
for i := 1 to iHandleCountdo
begin
ImmGetIMEFileName(pList, szImeFileName, MAX_PATH);
sImeFileName := AnsiUpperCase(StrPas(szImeFileName));
if sImeFileName = sWanted then
begin
bInstalled := True;
Break;
end;
end;
// 如果这个输入法已安装了, 让那个输入法的键盘分布(KeyLayout)作用
if bInstalled then
begin
ActivateKeyboardLayout(pList, 0);
Result := True;
end;
end;
{ of SetActivateIme }
// --------------------------------------------------
// 侦测目前作用中的输入法文件名称
// 传回值为空字符串时, 表示英数输入模式
//
// --------------------------------------------------
function GetImeFileName: string;
var
szImeFileName : array[0..MAX_PATH] of char;
begin
if ImmGetIMEFileName(GetKeyboardLayout(0), szImeFileName, MAX_PATH) <> 0 then
Result := AnsiUpperCase(StrPas(szImeFileName))
else
Result := '';
end;
// --------------------------------------------------
// 切换成中文输入法, 并且指定使用半/全角输入模式
// 传回值: True: 成功 / False 切换失败
// 使用示例: ImeFullShape(Form1.Handle, True);
// 全角
// ImeFullShape(Form1.Handle, False);
// 半角
// --------------------------------------------------
(*
这个函数也可以用以下的方式来作作看:
if not ImmIsIME(GetKeyboardLayout(0)) then
ImmSimulateHotKey(hWindow, IME_THOTKEY_IME_NONIME_TOGGLE);
Application.ProcessMessages;
ImmSimulateHotKey(hWindow, IME_THOTKEY_SHAPE_TOGGLE);
*)
function ImeFullShape(hWindow: HWND;
bToFullShape: BOOL): BOOL;
var
hic : HIMC;
Conversion, Sentence: DWORD;
msgPeekResult : TMsg;
begin
Result := False;
if hWindow = 0 then
hWindow := GetFocus;
if hWindow = 0 then
Exit;
// 切换成中文输入法
if not ImmIsIME(GetKeyboardLayout(0)) then
ImmSimulateHotKey(hWindow, IME_THOTKEY_IME_NONIME_TOGGLE);
while PeekMessage(msgPeekResult, hWindow, 0, 0, PM_REMOVE)do
begin
TranslateMessage(msgPeekResult);
DispatchMessage(msgPeekResult);
end;
// 转换成半/全角输入模式
hic := ImmGetContext(hWindow);
if hIC = 0 then
Exit;
try
if not ImmGetConversionStatus(hIc, Conversion, Sentence) then
Exit;
if bToFullShape then
Conversion := Conversion or IME_CMODE_FULLSHAPE
else
Conversion := Conversion and (not IME_CMODE_FULLSHAPE);
if not ImmSetConversionStatus(hic, Conversion, Sentence) then
Exit;
Result := True;
finally
ImmReleaseContext(hWindow, hic);
end;
end;
{ of ImeFullShape }