輸入法控制

2021-04-12 23:47:39 字數 2744 閱讀 4023

unit u_ime;

inte***ce

implementation

uses

windows, forms, imm, sysutils, messages;

varstmpclassname, stmpime: string; //失去焦點時的控制項名稱和輸入法名稱

msghandle: hhook = 0;

//判斷當前類名是否會丟失輸入法

function willlostime(sclassname: string): boolean;

begin

result := (sclassname = 'tlsdbgrid')

or (sclassname = 'tlsdatetimepicker')

or (sclassname = 'tdblookupcombobox')

or (sclassname = 'tdbcombobox');

end;

//獲取當前輸入法

function getcurrentime: string;

varimehandle: thandle;

idx: integer;

begin

result := '';

imehandle := getkeyboardlayout(0);

for idx := 0 to pred(screen.imes.count) do

if hkl(screen.imes.objects[idx]) = imehandle then

result := screen.imes[idx];

end;

//還原輸入法(如果輸入法無效則是空)

procedure restoreime;

procedure openime(imename:string);//開啟指定的輸入法

vari:integer;

myhkl:hkl;

begin

if trim(imename)<>'' then

begin

if screen.imes.count<>0 then

begin

i:=screen.imes.indexof(imename);

if i >= 0 then

begin

myhkl := hkl(screen.imes.objects[i]);

activatekeyboardlayout(myhkl, klf_activate);

end;

end;

end;

end;

begin

if trim(stmpime) <> '' then

openime(stmpime);

end;

//獲取當前控制項類名

function getcurctrclass: string;

varpt: tpoint;

hndctrl: hwnd;

buf: pchar;

begin

getcursorpos(pt);                        //螢幕取點

hndctrl := windowfrompoint(pt);          //控制項控制代碼

getmem(buf, 128);

tryif getclassname(hndctrl, buf, 128) > 0 then  //獲取類名

begin

result := string(buf);

endelse

result := '';

finally

freemem(buf);

end;

end;

function lsdmsgproc(ncode, wparam, lparam: integer): integer; stdcall;

varmsgtmp: uint;

begin

msgtmp := pcwpretstruct(lparam)^.message;

with screen.activeform do

if msgtmp = wm_setfocus then   //獲取焦點時記下類名

begin

stmpclassname := getcurctrclass;

endelse if msgtmp = wm_killfocus then  //失去焦點點時開啟輸入法

begin

if willlostime(stmpclassname) then   //如果當前控制項將失去焦點的那麼開啟輸入法

restoreime

else

stmpime := getcurrentime;  // 記下輸入法

end;

result := callnexthookex(msghandle, ncode, wparam, lparam);

end;

initialization

msghandle := setwindowshookex(wh_callwndprocret, lsdmsgproc, hinstance, getcurrentthreadid);

stmpclassname := '';

stmpime := getcurrentime; 

finalization

if msghandle <> 0 then

unhookwindowshookex(msghandle);

end.

控制輸入法演示

部落格 開發一般的b s架構的管理型別應用,都需要大量錄入表單。如果能依據輸入的內容的型別,智慧型的切換輸入法,那可太人性化了。比如 錄入中文姓名時自動換成中文輸入法,錄入英文名時自動換成英文輸入法,錄入數字時只有錄入1 9。演示的截圖 演示的html 如下 其中使用了js jquery.js,你自...

c 檢測中英輸入法 用C 控制當前輸入法

如何獲取系統中所有的輸入法,以及如何根據輸入語言及其他特殊要求選擇特定的輸入法,下面我以兩段程式片斷再加以說明。1 遍歷系統中所有的輸入法,可以用以下程式片斷 foreach inputlanguage lang in inputlanguage.installedinputlanguages th...

Visual C 程式設計控制輸入法

5 加入選擇輸入法成員函式 bool cinputlanguage selectinputlanguage il pil return true 6 其它部分 cinputlanguage cinputlanguage cinputlanguage cinputlanguage 消除鍊錶記憶體。vo...