在Windows系統中提供了一種系統消息掛鉤的(Message hook)功能,使用消息掛鉤,可以實時監視處理系統中的各種消息。很多鼠標增強軟件就是利用消息掛鉤來攔截所有的鼠標消息進行處理的。 要設置鼠標消息掛鉤,一般先建立一個使用鼠標消息掛鉤的動態連接庫(DLL)文件,然后就可以在其它程序中使用這個DLL文件處理鼠標消息。 下面的程序介紹通過鼠標消息掛鉤監視鼠標消息,從而實現類似于一些鼠標增強軟件一樣的使窗口上下左右滾動的功能。
1.建立動態連接庫 選擇菜單 File|New ,選擇DLL產生一個DLL模版,保存為 MHook.Dpr //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 //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
//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 //Move to left Result:=Move_Left else Result:=Move_Right
else if ((abs(iHeight) div abs(iWidth))>=2) then if iHeight<0 then //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 //如果用戶按下鼠標右鍵同時Scroll Lock鍵為按下狀態則 //滾動窗口。 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; //設置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|Build MHook建立DLL文件。
2.建立程序調用動態連接庫 在這里我們還是使用Delphi建立程序,當然也可以使用諸如VB等調用動態連接庫。 在菜單中選 File|New Application建立一個新程序,將工程文件保存為Project1.dpr
//project1的源程序 program Project1;
uses Forms, Sample1 in 'Sample1.pas' {Form1};
{$R *.RES}
begin Application.Initialize; //隱藏窗口 Application.ShowMainForm := False;
Application.CreateForm(TForm1, Form1); Application.Run; end.
將Form1的源程序文件保存成Sample1.pas
//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(var Message:TMessage);message WM_ICONMESSAGE;
public
end;
function EnableMouseHook(WndHandle:integer):BOOL;external 'MHook.DLL'; function DisableMouseHook:BOOL;external'MHook.DLL';
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.WMBarIcon (var Message:TMessage); begin //用戶雙擊任務欄圖標則關閉程序 if Message.LParam = WM_LBUTTONDBLCLK then close; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); var lpData:PNotifyIconData; begin //刪除任務欄圖標 lpData := new(PNotifyIconDataA); lpData.cbSize := 88;//SizeOf(PNotifyIconDataA); lpData.Wnd := Form1.Handle; lpData.hIcon := Form1.Icon.Handle; lpData.uCallbackMessage := WM_ICONMESSAGE; lpData.uID :=0; lpData.szTip := '鼠標演示'; lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP; Shell_NotifyIcon(NIM_DELETE,lpData); dispose(lpData); //解除Mouse hook DisableMouseHook; end;
procedure TForm1.FormCreate(Sender: TObject); var lpData:PNotifyIconData; begin EnableMouseHook(Form1.Handle); Form1.Visible := False; lpData := new(PNotifyIconDataA); lpData.cbSize := 88;//SizeOf(PNotifyIconDataA); lpData.Wnd := Form1.Handle; lpData.hIcon := Form1.Icon.Handle; lpData.uCallbackMessage := WM_ICONMESSAGE; lpData.uID :=0; lpData.szTip := '鼠標演示'; lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP; Shell_NotifyIcon(NIM_ADD,lpData); dispose(lpData); end;
end.
運行程序,按下Scroll Lock鍵使其有效,將光標移動到文本窗口中(如IE、Word),移動鼠標,點擊鼠標右鍵,窗口就可以依上一次移動的方向滾動。 利用上面的原理,將程序做一些改動,就可以象專業的鼠標增強程序一樣做出例如縮放窗口,運行程序等很多鼠標增強效果來。 上面的程序在Windows95,Delphi4.0下運行通過。
|