dll文件
library GMousehook;uses
SysUtils,wintypes,winprocs,messages,windows, Dialogs, mousehookdemou,
Classes;
var
ishooked:boolean;
hookhandle:hhook;
desktopwin:hwnd;
OldProc:FARPROC;function hookproc(code:integer;wparam:wparam;lparam:lparam):lresult;stdcall;
var
WinStr:HWND;
begin
if wparam=WM_LBUTTONDBLCLK then
begin
showmessage('鼠标左键双击');
end;
result:=callnexthookex(hookhandle,code,wparam,lparam);
end;function sethook:boolean;stdcall;
begin
result:=false;
if ishooked then
exit;
//desktopwin:=getdesktopwindow;
hookhandle:=setwindowshookex(wh_mouse,hookproc,hinstance,0);
ishooked:=true;
result:=hookhandle<>0;
end;function removehook:boolean;stdcall;
begin
result:=false;
if (not ishooked) and (hookhandle<>0) then
result:=unhookwindowshookex(hookhandle);
ishooked:=false;
end;
exports
sethook name 'sethook',
removehook name 'removehook',
hookproc name 'hookproc';{$R *.res}
begin
ishooked:=false;
end.主程序:
unit mousehookdemou;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public { Public declarations }
end;
function sethook:boolean;stdcall;
function removehook:boolean;stdcall;
var
Form1: TForm1;
s:string;
implementation
function sethook;external 'gmousehook.dll' name 'sethook';
function removehook;external 'gmousehook.dll' name 'removehook';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if sethook then
showmessage('global mouse hook set, click on desktop ')
else
showmessage('global mouse hook not set');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if removehook then
showmessage('global mouse hook removed,click on desktop')
else
showmessage('global mouse hook not removed');
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
showmessage(s);
end;
end.
问题是主程序中如何知道有鼠标左键双击呢,在主程序中添加一个字符串变量dll文件可以调用吗?
library GMousehook;uses
SysUtils,wintypes,winprocs,messages,windows, Dialogs, mousehookdemou,
Classes;
var
ishooked:boolean;
hookhandle:hhook;
desktopwin:hwnd;
OldProc:FARPROC;function hookproc(code:integer;wparam:wparam;lparam:lparam):lresult;stdcall;
var
WinStr:HWND;
begin
if wparam=WM_LBUTTONDBLCLK then
begin
showmessage('鼠标左键双击');
end;
result:=callnexthookex(hookhandle,code,wparam,lparam);
end;function sethook:boolean;stdcall;
begin
result:=false;
if ishooked then
exit;
//desktopwin:=getdesktopwindow;
hookhandle:=setwindowshookex(wh_mouse,hookproc,hinstance,0);
ishooked:=true;
result:=hookhandle<>0;
end;function removehook:boolean;stdcall;
begin
result:=false;
if (not ishooked) and (hookhandle<>0) then
result:=unhookwindowshookex(hookhandle);
ishooked:=false;
end;
exports
sethook name 'sethook',
removehook name 'removehook',
hookproc name 'hookproc';{$R *.res}
begin
ishooked:=false;
end.主程序:
unit mousehookdemou;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public { Public declarations }
end;
function sethook:boolean;stdcall;
function removehook:boolean;stdcall;
var
Form1: TForm1;
s:string;
implementation
function sethook;external 'gmousehook.dll' name 'sethook';
function removehook;external 'gmousehook.dll' name 'removehook';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if sethook then
showmessage('global mouse hook set, click on desktop ')
else
showmessage('global mouse hook not set');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if removehook then
showmessage('global mouse hook removed,click on desktop')
else
showmessage('global mouse hook not removed');
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
showmessage(s);
end;
end.
问题是主程序中如何知道有鼠标左键双击呢,在主程序中添加一个字符串变量dll文件可以调用吗?
为 sethook 增加一个参数,把主窗口句柄传进去
function sethook(mainwnd: Cardinal):boolean;stdcall;
begin
FMainWnd := mainwnd;
// other code
end;在钩子的回调中function hookproc(code:integer;wparam:wparam;lparam:lparam):lresult;stdcall;
var
WinStr:HWND;
begin
if wparam=WM_LBUTTONDBLCLK then
begin
//showmessage('鼠标左键双击');
PostMessage(FMainWnd, CUSTOM_LBUTTONDBLCLK, wparam, lparam);
end;
result:=callnexthookex(hookhandle,code,wparam,lparam);
end;
主程序:
unit mousehookdemou;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure OnCustomeLButtonDblClick(var msg: TMessage); message CUSTOM_LBUTTONDBLCLK;
private
{ Private declarations }
public { Public declarations }
end;
function sethook:boolean;stdcall;
function removehook:boolean;stdcall;
var
Form1: TForm1;
s:string;
implementation
function sethook;external 'gmousehook.dll' name 'sethook';
function removehook;external 'gmousehook.dll' name 'removehook'; procedure TForm1.OnCustomeLButtonDblClick(var msg: TMessage);
begin
ShowMessage('mouse hook get lbuttondblclick!');
end;