SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0);function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT; var theMsg: TMessage; begin Result := 0; if Code < 0 then begin Result := CallNextHookEx(HOOK, Code, WParam, LParam); Exit; end; if Code = HC_ACTION then begin if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin theMsg.Msg := PMsg(LParam).message; theMsg.WParam := PMsg(LParam).wParam; theMsg.LParam := PMsg(LParam).lParam; Proc(theMsg); end; Result := CallNextHookEx(HOOK, Code, WParam, LParam); end end;procedure Proc(var Message: TMessage); var hIMC: Integer; dwSize: Integer; hstr: Integer; lpstr: Pointer; begin if Message.Msg = WM_IME_COMPOSITION then begin if (Message.lParam and GCS_RESULTSTR) <> 0 then begin hIMC := ImmGetContext(GetFocus); dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0); dwSize := dwSize + sizeof(WCHAR); hstr := GlobalAlloc(GHND,dwSize); lpstr := GlobalLock(hstr); ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize); ImmReleaseContext(GetFocus, hIMC); SaveStrToFile(lpstr);//保存函数你自己写吧 end; end; end;
多谢你的回复! 但按照你的代码运行,不能拦截到任何数据。我的程序如下,请看看哪里不对。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,Imm;type TForm1 = class(TForm) Edit1: TEdit; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; HOOK:integer; implementation {$R *.dfm} procedure Proc(var Message: TMessage); var hIMC: Integer; dwSize: Integer; hstr: Integer; lpstr: Pointer; begin if Message.Msg = WM_IME_COMPOSITION then begin if (Message.lParam and GCS_RESULTSTR) <> 0 then begin hIMC := ImmGetContext(GetFocus); dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0); dwSize := dwSize + sizeof(WCHAR); hstr := GlobalAlloc(GHND,dwSize); lpstr := GlobalLock(hstr); ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize); ImmReleaseContext(GetFocus, hIMC); //SaveStrToFile(lpstr);//保存函数你自己写吧 showmessage('拦截成功'); end; end; end;function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT; var theMsg: TMessage; begin Result := 0; if Code < 0 then begin Result := CallNextHookEx(HOOK, Code, WParam, LParam); Exit; end; if Code = HC_ACTION then begin if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin theMsg.Msg := PMsg(LParam).message; theMsg.WParam := PMsg(LParam).wParam; theMsg.LParam := PMsg(LParam).lParam; Proc(theMsg); end; Result := CallNextHookEx(HOOK, Code, WParam, LParam); end end; procedure TForm1.FormCreate(Sender: TObject); begin Hook:=SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0); end;end.
var
theMsg: TMessage;
begin
Result := 0;
if Code < 0 then begin
Result := CallNextHookEx(HOOK, Code, WParam, LParam);
Exit;
end;
if Code = HC_ACTION then begin
if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin
theMsg.Msg := PMsg(LParam).message;
theMsg.WParam := PMsg(LParam).wParam;
theMsg.LParam := PMsg(LParam).lParam;
Proc(theMsg);
end;
Result := CallNextHookEx(HOOK, Code, WParam, LParam);
end
end;procedure Proc(var Message: TMessage);
var
hIMC: Integer;
dwSize: Integer;
hstr: Integer;
lpstr: Pointer;
begin
if Message.Msg = WM_IME_COMPOSITION then begin
if (Message.lParam and GCS_RESULTSTR) <> 0 then begin
hIMC := ImmGetContext(GetFocus);
dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0);
dwSize := dwSize + sizeof(WCHAR);
hstr := GlobalAlloc(GHND,dwSize);
lpstr := GlobalLock(hstr);
ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
ImmReleaseContext(GetFocus, hIMC);
SaveStrToFile(lpstr);//保存函数你自己写吧
end;
end;
end;
但按照你的代码运行,不能拦截到任何数据。我的程序如下,请看看哪里不对。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Imm;type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
HOOK:integer;
implementation
{$R *.dfm}
procedure Proc(var Message: TMessage);
var
hIMC: Integer;
dwSize: Integer;
hstr: Integer;
lpstr: Pointer;
begin
if Message.Msg = WM_IME_COMPOSITION then begin
if (Message.lParam and GCS_RESULTSTR) <> 0 then begin
hIMC := ImmGetContext(GetFocus);
dwSize := ImmGetCompositionString(hIMC, GCS_RESULTSTR, nil, 0);
dwSize := dwSize + sizeof(WCHAR);
hstr := GlobalAlloc(GHND,dwSize);
lpstr := GlobalLock(hstr);
ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
ImmReleaseContext(GetFocus, hIMC);
//SaveStrToFile(lpstr);//保存函数你自己写吧
showmessage('拦截成功');
end;
end;
end;function GetMsgProc(Code: Integer; WParam: WParam; LParam: LParam): LRESULT;
var
theMsg: TMessage;
begin
Result := 0;
if Code < 0 then begin
Result := CallNextHookEx(HOOK, Code, WParam, LParam);
Exit;
end;
if Code = HC_ACTION then begin
if (PMsg(LParam).message = WM_IME_COMPOSITION) then begin
theMsg.Msg := PMsg(LParam).message;
theMsg.WParam := PMsg(LParam).wParam;
theMsg.LParam := PMsg(LParam).lParam;
Proc(theMsg);
end;
Result := CallNextHookEx(HOOK, Code, WParam, LParam);
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Hook:=SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance,0);
end;end.