L
LearningAug
Unregistered / Unconfirmed
GUEST, unregistred user!
//DLL代码…………………………………………………………………………………………
library Project1;
uses
SysUtils,
Classes, Windows, messages, shellapi, Dialogs;
type
Tcallbackfun = procedure;//回调函数
Tmousehook = record
isrun: boolean;
hook: hhook;
callbackfun: Tcallbackfun;
end;
var
mymousehook: Tmousehook;
{$R *.res}
//钩子函数
function gethookinfo(code: integer; wp: WPARAM; lp: LPARAM): LResult; stdcall;
begin
if code < 0 then
begin
result := CallNextHookEx(mymousehook.hook, code, wp, lp);
exit;
end;
case wp of
WM_LBUTTONDOWN:
begin
mymousehook.callbackfun;//调用回调函数
end;
end;
result := CallNextHookEx(mymousehook.hook, code, wp, lp);
end;
//安装钩子
procedure installmousehook(callbackF: Tcallbackfun); stdcall;
begin
if not mymousehook.isrun then
begin
mymousehook.hook := setwindowshookex(WH_MOUSE, @gethookinfo, HInstance,0);//全局钩子
mymousehook.callbackfun := callbackf;
mymousehook.isrun := not mymousehook.isrun;
end;
end;
//卸载钩子
procedure uninstallmousehook(); stdcall;
begin
if mymousehook.isrun then
begin
UnHookWindowsHookEx(mymousehook.hook);
mymousehook.callbackfun := nil;
mymousehook.isrun := not mymousehook.isrun;
end;
end;
//DLL入口函数
procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH:
begin
mymousehook.isrun := false;
end;
DLL_PROCESS_DETACH: ;
DLL_THREAD_ATTACH: ;
DLL_THREAD_DETACH: ;
end;
end;
exports
installmousehook,
uninstallmousehook;
begin
DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
//应用程序代码……………………………………………………………………………………
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tcallbackfun = procedure;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
procedure installmousehook(callbackF: Tcallbackfun); stdcall; external
'Project1.dll';
procedure uninstallmousehook(); stdcall; external 'Project1.dll';
{$R *.dfm}
procedure addinfo;
begin
form1.Visible := true ;
ShowWindow(Application.Handle, SW_SHOWNORMAL);
form1.width:= form1.width +1;
end;
//安装钩子
procedure TForm1.Button1Click(Sender: TObject);
begin
installmousehook(addinfo);
end;
//卸装钩子
procedure TForm1.FormDestroy(Sender: TObject);
begin
uninstallmousehook();
end;
end.
我想达到的目的:在任何窗口中,只要点击鼠标左键,form1的宽度都会增加1。但是现在我只有在form1上单击鼠标左键才有效,一旦form1最小化后,在其它地方点击鼠标左键,都没有响应,请问是不是我的全局钩子有问题?我应该怎么修改呢?谢谢了!!
library Project1;
uses
SysUtils,
Classes, Windows, messages, shellapi, Dialogs;
type
Tcallbackfun = procedure;//回调函数
Tmousehook = record
isrun: boolean;
hook: hhook;
callbackfun: Tcallbackfun;
end;
var
mymousehook: Tmousehook;
{$R *.res}
//钩子函数
function gethookinfo(code: integer; wp: WPARAM; lp: LPARAM): LResult; stdcall;
begin
if code < 0 then
begin
result := CallNextHookEx(mymousehook.hook, code, wp, lp);
exit;
end;
case wp of
WM_LBUTTONDOWN:
begin
mymousehook.callbackfun;//调用回调函数
end;
end;
result := CallNextHookEx(mymousehook.hook, code, wp, lp);
end;
//安装钩子
procedure installmousehook(callbackF: Tcallbackfun); stdcall;
begin
if not mymousehook.isrun then
begin
mymousehook.hook := setwindowshookex(WH_MOUSE, @gethookinfo, HInstance,0);//全局钩子
mymousehook.callbackfun := callbackf;
mymousehook.isrun := not mymousehook.isrun;
end;
end;
//卸载钩子
procedure uninstallmousehook(); stdcall;
begin
if mymousehook.isrun then
begin
UnHookWindowsHookEx(mymousehook.hook);
mymousehook.callbackfun := nil;
mymousehook.isrun := not mymousehook.isrun;
end;
end;
//DLL入口函数
procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH:
begin
mymousehook.isrun := false;
end;
DLL_PROCESS_DETACH: ;
DLL_THREAD_ATTACH: ;
DLL_THREAD_DETACH: ;
end;
end;
exports
installmousehook,
uninstallmousehook;
begin
DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
//应用程序代码……………………………………………………………………………………
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tcallbackfun = procedure;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
procedure installmousehook(callbackF: Tcallbackfun); stdcall; external
'Project1.dll';
procedure uninstallmousehook(); stdcall; external 'Project1.dll';
{$R *.dfm}
procedure addinfo;
begin
form1.Visible := true ;
ShowWindow(Application.Handle, SW_SHOWNORMAL);
form1.width:= form1.width +1;
end;
//安装钩子
procedure TForm1.Button1Click(Sender: TObject);
begin
installmousehook(addinfo);
end;
//卸装钩子
procedure TForm1.FormDestroy(Sender: TObject);
begin
uninstallmousehook();
end;
end.
我想达到的目的:在任何窗口中,只要点击鼠标左键,form1的宽度都会增加1。但是现在我只有在form1上单击鼠标左键才有效,一旦form1最小化后,在其它地方点击鼠标左键,都没有响应,请问是不是我的全局钩子有问题?我应该怎么修改呢?谢谢了!!