//在进程中试了一下,修改消息应该没问题(系统钩子应该也差不多,知道原理变通一下就可以了)。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const WM_END = 0; //在Windows系统中,0消息代表没有消息。
WM_TESTMSG = 1000; //自定义的一个消息,在实践中这个消息可以修改成你要截获的消息。
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Procedure TestMsg(var Msg:TMessage);Message WM_TESTMSG; //自定义消息处理过程
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
HookHandle:HHOOK;
implementation
{$R *.dfm}
//Hook消息函数
Function TestHookProc(iCode:Integer;
wParam:LongInt;
lParam:LongInt):LongInt; stdCall;
begin
if iCode = HC_ACTION then
begin
if PMsg(lParam)^.message = WM_TESTMSG then
begin
ShowMessage('钩子已经启动,消息已被我截获');
PMsg(lParam)^.message := WM_END; //结束这个消息。
end;
ResUlt := CallNextHookEx(HookHandle,iCode,wParam,LongInt(@lParam));
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
HookHandle := SetWindowsHookEx(WH_GETMESSAGE,TestHookProc,0,GetCurrentThreadID);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(HookHandle); //卸载钩子。
Close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
PostMessage(Form1.Handle,WM_TESTMSG,0,0);
end;
procedure TForm1.TestMsg(var Msg: TMessage);
begin
if Msg.Msg = WM_TESTMSG then
ShowMessage('呵呵!还没装上钩子。');
end;
end.
给的是进程用钩子拦截自己的消息。如果想拦截其他程序收到或者发出的消息,需要将钩子放在独立的DLL里面。