看了下MSDN对hook方面的描述,就开始写了个测试程序,实现功能如下,拦截并屏蔽用户定义消息wm_user1
const wm_user1=WM_USER+12;
安装钩子:hHook:=SetwindowsHookEx(WH_GETMESSAGE,@GetMsgProc,0,GetCurrentThreadId());function GetMsgProc(code:integer; // hook code
wParam:WPARAM; // removal option
lParam:LPARAM // message
):integer;stdcall;
var
myMsg:TMsg;
begin
if (code<>HC_ACTION) then
begin
result := CallNextHookEx(hHook,code,WParam,LParam);
exit;
end
else
begin
myMsg := PMsg(lParam)^;
if (myMsg.message=wm_user1) then
begin
myMsg.message:=WM_NULL;//瞎蒙的
Result :=1; end
else
begin
result := CallNextHookEx(hHook,code,WParam,LParam);
end;
end;
end;然后用PostMessage(self.Handle,wm_user1,0,0);不过测试后发现不能屏蔽wm_user1消息,应该是我没操作好,文档没写清楚,不知道红色的地方该怎么改,特请大家帮个忙,谢谢。(备注:鼠标键盘钩子以前用简单点的还很成功,第一次尝试下用WH_GETMESSAGE拦截其他类型的消息,写的很差就别批评)
const wm_user1=WM_USER+12;
安装钩子:hHook:=SetwindowsHookEx(WH_GETMESSAGE,@GetMsgProc,0,GetCurrentThreadId());function GetMsgProc(code:integer; // hook code
wParam:WPARAM; // removal option
lParam:LPARAM // message
):integer;stdcall;
var
myMsg:TMsg;
begin
if (code<>HC_ACTION) then
begin
result := CallNextHookEx(hHook,code,WParam,LParam);
exit;
end
else
begin
myMsg := PMsg(lParam)^;
if (myMsg.message=wm_user1) then
begin
myMsg.message:=WM_NULL;//瞎蒙的
Result :=1; end
else
begin
result := CallNextHookEx(hHook,code,WParam,LParam);
end;
end;
end;然后用PostMessage(self.Handle,wm_user1,0,0);不过测试后发现不能屏蔽wm_user1消息,应该是我没操作好,文档没写清楚,不知道红色的地方该怎么改,特请大家帮个忙,谢谢。(备注:鼠标键盘钩子以前用简单点的还很成功,第一次尝试下用WH_GETMESSAGE拦截其他类型的消息,写的很差就别批评)
我用下面的程序测试,并单步跟踪,实际上能hook到消息wm_user1,但是我现在是不知道该怎么将该消息给屏蔽掉,即:杀死该消息或者转换该消息为另一个消息,如果哪位兄弟知道请告诉我
procedure wm_user11(var Message:TMessage);message wm_user1;procedure TForm1.wm_user11(var Message:TMessage);
begin
MessageBox(self.Handle,'测试接受','wm_user11',MB_OK);
end;
一起学习!
我将值copy,当成指针引用了,思路错误,感谢各位