时间: 2021-07-31 作者:daque
//====================================library hookprj;uses sysutils, classes, unit1 in 'unit1.pas';exportsenablemsghook, //只有把这两个因变量输入就不妨了,disablemsghook;//beginend.//====================================//====================================unit unit1;interface
useswindows,messages;
varhookhandle: hhook;//钩子的句柄值。function msghookproc(code: integer; wparam: longint;msg:longint): lresult;stdcall;//鼠标钩子的回调因变量,即是用它来处置获得动静后要干什么。。//ncode参数是hook的标记,普遍只关怀小于0时。//wparam参数表白鼠标动静的典型//lparam参数是一个指向 tmousehookstruct 构造的南针。构造包括了鼠标动静的状况,我只用了hwnd一个//即鼠标动静要传播给的窗口句柄。//归来值即使不是0的话windows就把这个动静抛弃,其它的步调就不会再收到这个动静了。
function enablemsghook:boolean; stdcall; export;function disablemsghook:boolean; stdcall; export;//两个因变量都是boolean典型,胜利都是归来trueimplementationfunction msghookproc(code: integer; wparam: longint;msg:longint): lresult;stdcall;begin if (code = hc_action) then if pmsg(msg)^.message = wm_mousewheel then //鼠标震动 begin if hiword(pmsg(msg)^.wparam)=120 then // 上滚 begin //做你想做的。 showwindow (pmsg(msg)^.hwnd,sw_maximize ); end; if hiword(pmsg(msg)^.wparam)<>120 then // 下滚 begin //做你想做的。 showwindow (pmsg(msg)^.hwnd,sw_restore ); end; pmsg(msg)^.message := 0; end; result :=callnexthookex(hookhandle, code, wparam, longint(@msg));end;
function enablemsghook:boolean; stdcall; export;beginif hookhandle = 0 then //为了安定,必需确定一下再树立钩子。begin// 第三个参数的hinstance 在delphi中有设置,用就不妨了。第四个参数必需为0hookhandle := setwindowshookex(wh_getmessage,@msghookproc,hinstance,0);result := true;endelseresult := false;end;
function disablemsghook:boolean; stdcall; export;beginif hookhandle <> 0 then //即使有钩子就卸掉他。beginunhookwindowshookex(hookhandle);hookhandle := 0;result := true;endelseresult := false;end;
end.
//====================================