利用HOOK建立鼠标增强程序(DELPHI)
在Windows系统中提供了一种系统消息挂钩的(Message hook)功能,使用消息挂钩,可
以实时监视处理系统中的各种消息。很多鼠标增强软件就是利用消息挂钩来拦截所有的
鼠标消息进行处理的。
要设置鼠标消息挂钩,一般先建立一个使用鼠标消息挂钩的动态连接库(DLL)文件,然
后就可以在其它程序中使用这个DLL文件处理鼠标消息。 下面的程序介绍通过鼠标消息挂钩监视鼠标消息,从而实现类似于一些鼠标增强软件一
样的使窗口上下左右滚动的功能。
1.建立动态连接库
选择菜单 File|New ,选择DLL产生一个DLL模版,保存为 MHook.Dpr file://MHook.Dpr源程序 library MHook;
uses
SysUtils, Classes,
hkproc in 'hkproc.pas';
exports
EnableMouseHook, DisableMouseHook;
begin
hNextHookProc:=0;
procSaveExit:=ExitProc; ExitProc:=@HotKeyHookExit; end.
再选择菜单 File|New ,选择Unit建立一个Pas文件,保存为 HKProc.pas file://HKProc.pas源程序 unit hkproc;
interface uses
Windows,Messages; const
Move_Up = 0;
Move_Down=1; Move_Left=2; Move_Right=3; var
hNextHookProc:HHook; procSaveExit:Pointer; M_Direct:Integer; LPoint:TPoint;
NowWindow:Integer;
function MouseProc(iCode:Integer;wParam:WPARAM; lParam:Pointer):LRESULT; stdcall;export;
function EnableMouseHook(WndHandle:integer):BOOL;export;
function DisableMouseHook:BOOL;export;
function GetDirect(FPoint : TPoint;LPoint : TPoint):integer; procedure HotKeyHookExit;far;
implementation
file://GetDirect函数根据光标的移动决定窗口滚动的方向。
function GetDirect(FPoint : TPoint;LPoint : TPoint):integer; var
iWidth,iHeight:integer; begin
iWidth:=LPoint.x-FPoint.x; iHeight:=lPoint.y-FPoint.y; Result:=-1;
if ((iWidth=0)or(iHeight=0))then exit;
if ((abs(iWidth) div abs(iHeight))>=2) then if iWidth<0 then file://Move to left Result:=Move_Left else
Result:=Move_Right
else if ((abs(iHeight) div abs(iWidth))>=2) then if iHeight<0 then file://Move to top Result:=Move_Up else
Result:=Move_Down;
end;
function MouseProc(iCode:Integer;wParam:WPARAM; lParam:Pointer):LRESULT; stdcall;export; var
pMouse:^MOUSEHOOKSTRUCT; l:integer; begin
file://如果用户按下鼠标右键同时Scroll Lock键为按下状态则 file://滚动窗口。
if ((wParam=WM_RBUTTONDOWN) and Boolean(GetKeyState(145))) then begin
pMouse:=lParam;
l:=GetDirect(lPoint,pMouse.pt); if l>=0 then M_Direct:=l;
lPoint:=pMouse.pt;
NowWindow:=WindowFromPoint(lPoint); if M_Direct=Move_Up then
SendMessage(NowWindow,WM_VSCROLL,SB_PAGEUP,0) else if M_Direct=Move_Down then
SendMessage(NowWindow,WM_VSCROLL,SB_PAGEDOWN,0) else if M_Direct=Move_Left then
SendMessage(NowWindow,WM_HSCROLL,SB_PAGELEFT,0) else if M_Direct=Move_Right then
SendMessage(NowWindow,WM_HSCROLL,SB_PAGERIGHT,0); Result:=1; exit; end
else if ((wParam=WM_RBUTTONUP) and Boolean(GetKeyState(145))) then Result:=1 else begin
Result:=0;
if iCode<0 then begin
Result:=CallNextHookEx(hNextHookProc,iCode,wParam, integer(lParam)); Exit; end;
end; end;
function EnableMouseHook(WndHandle:integer):BOOL;export; begin
GetCursorPos(lPoint); Result:=False;
if hNextHookProc<>0 then exit;
file://设置Mouse hook
hNextHookProc:=SetWindowsHookEx(WH_MOUSE,@MouseProc, Hinstance,0);
Result:=hNextHookProc<>0; end;
function DisableMouseHook:BOOL;export; begin
if hNextHookProc<>0 then begin
UnHookWindowsHookEx(hNextHookProc); hNextHookProc:=0; end;
Result:=hNextHookProc=0; end;
procedure HotKeyHookExit; begin
if hNextHookProc<>0 then DisableMouseHook;
ExitProc:=procSaveExit; end;
end.
在菜单中选择 Project|BuildMHook建立DLL文件。
2.建立程序调用动态连接库
在这里我们还是使用Delphi建立程序,当然也可以使用诸如VB等调用动态连接库。
在菜单中选 File|New Application建立一个新程序,将工程文件保存为Project1.dpr
file://project1的源程序 program Project1;
uses Forms,
Sample1 in 'Sample1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize; file://隐藏窗口
Application.ShowMainForm := False;
Application.CreateForm(TForm1, Form1); Application.Run; end.
将Form1的源程序文件保存成Sample1.pas
file://Form1的源程序 unit Sample1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus, ImgList,ShellApi, ExtCtrls;
const
WM_ICONMESSAGE=WM_USER+$100;
type
TForm1 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private
procedure WMBarIcon(varMessage:TMessage);message WM_ICONMESSAGE;
public
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库利用HOOK建立鼠标增强程序(DELPHI)在线全文阅读。
相关推荐: