G
glldc
Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何拦截到输入的中文文字?如果用hook拦截键盘的话,则只能得到输入的英文字母,不能得到输入的中文。如何才能正确拦截到输入的中文?<br><br>在网上找到一个例子,但我运行时不能拦截到任何信息,请高手帮忙看看哪里出了问题,谢!<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls,Imm;<br><br>type<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br>var<br> Form1: TForm1;<br> HOOK:integer;<br>implementation<br>{$R *.dfm}<br>procedure Proc(var Message: TMessage);<br>var<br> hIMC: Integer;<br> dwSize: Integer;<br> hstr: Integer;<br> lpstr: Pointer;<br>begin<br> if Message.Msg = WM_IME_COMPOSITION then begin<br> if (Message.lParam and GCS_RESULTSTR) <> 0 then begin<br> hIMC := ImmGetContext(GetFocus);<br> dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0);<br> dwSize := dwSize + sizeof(WCHAR);<br> hstr := GlobalAlloc(GHND,dwSize);<br> lpstr := GlobalLock(hstr);<br> ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);<br> ImmReleaseContext(GetFocus, hIMC);<br> //SaveStrToFile(lpstr);//保存函数你自己写吧<br> showmessage('拦截成功');<br> end;<br> end;<br>end;<br><br>function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT; stdcall;<br>var<br> theMsg: TMessage;<br>begin<br> Result := 0;<br> if Code < 0 then begin<br> Result := CallNextHookEx(HOOK, Code, WParam, LParam);<br> Exit;<br> end;<br> if Code = HC_ACTION then begin<br> if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin<br> theMsg.Msg := PMsg(LParam).message;<br> theMsg.WParam := PMsg(LParam).wParam;<br> theMsg.LParam := PMsg(LParam).lParam;<br> Proc(theMsg);<br> end;<br> Result := CallNextHookEx(HOOK, Code, WParam, LParam);<br> end<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> Hook:=SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0);<br>end;<br><br>end.