function sethook:bool;export;
function unhook:bool;export;
function mouseProc(code:integer;w:integer;l:longint):bool;export;
var
Form1: TForm1;
idhook:longint;
formok:bool;
implementation
{*********************************************************************
声明安装函数setWindowsHookEx(),
在Delphi中如果用函数setWindowsHook()则不需声明。
微软说函数setWindowsHook已在Windows3.1中废弃,为与Windows3.0兼容仍保留。
实际上该函数setWindowsHook在Windows3.1和Windows95中仍可使用。
{*********************************************************************}
function setwindowsHookEx(id:integer;proc:tfarproc;hinst,htask:thandle):
longint;far;external 'user';
{$R *.DFM}
{安装鼠标钩子函数mouseProc}
function sethook:bool;
var
hinst:thandle; {该动态连接库自己的模块局柄}
proc:tfarproc; {鼠标钩子函数mouseProc的地址}
begin
{在动态连接库中创建form1}
if formok=false then form1:=tform1.create(application) else exit;
formok:=true;{安装form1 后,设置formok,不许再安装form1}
{动态连接库的application指:调用动态连接库的主程序}
form1.show;
proc:=getProcAddress(hinst,'mouseProc');
idhook:=setWindowsHookEx(WH_MOUSE,proc,hinst,0);
{用WH_MOUSE参数安装鼠标钩子后,移动鼠标时,系统自动调用mouseProc钩子}
if idhook =0 then sethook:=false else sethook:=true;
end;
{解除鼠标钩子函数mouseProc的安装}
function unhook:bool;
begin
if formok=true then form1.free else exit; {检查form1是否已经关闭}
formok:=false;{关闭了form1,设置formok=0}
if idhook=0 then exit;
unhookWindowsHookEx(idhook);
unhook:=true;
end;
{mouseProc不由应用程序调用,而是在鼠标移动后,由系统调用}
function mouseProc(code:integer;w:integer;l:longint):bool;
var
p:^TMouseHookStruct;
poff:word;
pseg:word;
pmemo:pchar;
begin
if code<0 then begin
mouseProc:=true;
CallNextHookEx(idhook,0,w,l);
end;
if code=HC_NOREMOVE then form1.caption:='HC_NOREMOVE';
form1.caption:='mouse hook';
mouseProc:=false;
{显示系统传来的wParam参数,w是各种鼠标消息的标识符 }
form1.label1.caption:='wParam='+intTostr(w);
{显示系统传来的lParam参数,l是MOUSEHOOKSTRUCT结构的地址}
form1.label2.caption:='lParam='+intTostr(l);
function sethook:bool;export;
function unhook:bool;export;
function mouseProc(code:integer;w:integer;l:longint):bool;export;
var
Form1: TForm1;
idhook:longint;
formok:bool;
implementation
{*********************************************************************
声明安装函数setWindowsHookEx(),
在Delphi中如果用函数setWindowsHook()则不需声明。
微软说函数setWindowsHook已在Windows3.1中废弃,为与Windows3.0兼容仍保留。
实际上该函数setWindowsHook在Windows3.1和Windows95中仍可使用。
{*********************************************************************}
function setwindowsHookEx(id:integer;proc:tfarproc;hinst,htask:thandle):
longint;far;external 'user';
{$R *.DFM}
{安装鼠标钩子函数mouseProc}
function sethook:bool;
var
hinst:thandle; {该动态连接库自己的模块局柄}
proc:tfarproc; {鼠标钩子函数mouseProc的地址}
begin
{在动态连接库中创建form1}
if formok=false then form1:=tform1.create(application) else exit;
formok:=true;{安装form1 后,设置formok,不许再安装form1}
{动态连接库的application指:调用动态连接库的主程序}
form1.show;
proc:=getProcAddress(hinst,'mouseProc');
idhook:=setWindowsHookEx(WH_MOUSE,proc,hinst,0);
{用WH_MOUSE参数安装鼠标钩子后,移动鼠标时,系统自动调用mouseProc钩子}
if idhook =0 then sethook:=false else sethook:=true;
end;
{解除鼠标钩子函数mouseProc的安装}
function unhook:bool;
begin
if formok=true then form1.free else exit; {检查form1是否已经关闭}
formok:=false;{关闭了form1,设置formok=0}
if idhook=0 then exit;
unhookWindowsHookEx(idhook);
unhook:=true;
end;
{mouseProc不由应用程序调用,而是在鼠标移动后,由系统调用}
function mouseProc(code:integer;w:integer;l:longint):bool;
var
p:^TMouseHookStruct;
poff:word;
pseg:word;
pmemo:pchar;
begin
if code<0 then begin
mouseProc:=true;
CallNextHookEx(idhook,0,w,l);
end;
if code=HC_NOREMOVE then form1.caption:='HC_NOREMOVE';
form1.caption:='mouse hook';
mouseProc:=false;
{显示系统传来的wParam参数,w是各种鼠标消息的标识符 }
form1.label1.caption:='wParam='+intTostr(w);
{显示系统传来的lParam参数,l是MOUSEHOOKSTRUCT结构的地址}
form1.label2.caption:='lParam='+intTostr(l);
function HookMouseProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
pMouseHook: MOUSEHOOKSTRUCT;
pt: TPoint;
rect: TRect;
begin
Result := CallNextHookEx(g_hHook, iCode, wParam, lParam);
if iCode >= 0 then
begin
{if not IsMouseNeedHook then
begin
Exit;
end;}
case wParam of
WM_MOUSEMOVE:
begin
// Specifies aPOINT structure that contains the x- and y-coordinates of the cursor, in screen coordinates.
pMouseHook := pMOUSEHOOKSTRUCT(lparam)^;
pt := Point(pMouseHook.pt.X, pMouseHook.pt.Y);
ScreenToClient(g_hDestWnd, pt);
PostMessage(g_hDestWnd, SHOW_MENU_MSG, pt.X, pt.Y);
end;
WM_NCLBUTTONDOWN:
begin
g_CanPost := False;
end;
WM_LBUTTONDOWN: // ÆË×½µ¥»÷ʼþ
begin
// pt := Point(LOWORD(lParam), HIWORD(lParam));
if not g_CanPost then
begin
g_CanPost := True;
end
else
begin
g_CanPost := False; // 04.8.30 TKL Add
GetCursorPos(pt);
ScreenToClient(g_hDestWnd, pt);
PostMessage(g_hDestWnd, MY_MOUSE_LCLICK, pt.X, pt.Y);
end;
end;
WM_NCLBUTTONDBLCLK:
begin
Result := 1;
Exit;
end;
WM_MBUTTONDOWN: YOURS..
The WM_MBUTTONDOWN message is posted when the user presses the middle mouse button while the cursor is in the client area of a window. If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.