hook截获自定义消息
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; const WM_TestMessage = WM_USER + 2000; type TForm1 = class(TForm) btn1: TButton; edt1: TEdit; btn2: TButton; procedure FormCreate(Sender: TObject); procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} var HookHandle: HHOOK; function TestHookProc(Code: Integer; WParam: Longint;Msg:Longint): Longint;stdcall; begin if (Code = HC_ACTION) then if PMsg(Msg)^.Message = WM_TestMessage then begin showMessage('已经截获该消息'); end; Result := CallNextHookEx(HookHandle, Code, WParam, Longint(@Msg)); end; procedure TForm1.FormCreate(Sender: TObject); begin HookHandle:=SetWindowsHookEx(WH_GETMESSAGE,TestHookProc,0,GetCurrentThreadID); end; procedure TForm1.btn1Click(Sender: TObject); begin PostMessage(self.Handle,WM_TestMessage,0,0); end; procedure TForm1.btn2Click(Sender: TObject); var s:string; begin s:= 'abc'; SendMessage(edt1.Handle,WM_SETTEXT,0,integer(PChar(s))); end; end.
书搞进脑袋 创新 创造; 积极