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;
var form1:tform1;
begin
if wparam=WM_LBUTTONDBLCLK then
begin
{messagebeep(mb_iconasterisk);
WinStr:=FindWindow(nil,'form1');
OldProc:=FARPROC(GetWindowLong(WinStr,GWL_WNDPROC));
//SetWindowLong(WinStr,GWL_WNDPROC,Longint(@WinProc));
mousehookdemou.s:='asdf'; }
form1:=tform1.Create(nil);
form1.Label1.Caption:='123';
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 WinProc(Hwnd,Msg,wParam,lParam:longint):LRESULT; stdcall;
begin
{在这做出对消息的处理 case Msg of
WM_ACTIVATEAPP:exit;
WM_ACTIVATE:exit;
WM_KILLFOCUS:exit;
WM_SETFOCUS:exit;
end;
上面这些消息是窗口失去焦点和获得焦点的屏蔽
}
//将窗口消息传递给Windows处理
Result:=CallWindowProc(OldProc,Hwnd,Msg,wParam,lParam);
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.
程序如何修改,请指教?
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;
var form1:tform1;
begin
if wparam=WM_LBUTTONDBLCLK then
begin
{messagebeep(mb_iconasterisk);
WinStr:=FindWindow(nil,'form1');
OldProc:=FARPROC(GetWindowLong(WinStr,GWL_WNDPROC));
//SetWindowLong(WinStr,GWL_WNDPROC,Longint(@WinProc));
mousehookdemou.s:='asdf'; }
form1:=tform1.Create(nil);
form1.Label1.Caption:='123';
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 WinProc(Hwnd,Msg,wParam,lParam:longint):LRESULT; stdcall;
begin
{在这做出对消息的处理 case Msg of
WM_ACTIVATEAPP:exit;
WM_ACTIVATE:exit;
WM_KILLFOCUS:exit;
WM_SETFOCUS:exit;
end;
上面这些消息是窗口失去焦点和获得焦点的屏蔽
}
//将窗口消息传递给Windows处理
Result:=CallWindowProc(OldProc,Hwnd,Msg,wParam,lParam);
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.
程序如何修改,请指教?
解决方案 »
- delphi 为什么data access中没有clientDataSet?
- 关于 TDATASET 和 TQUERY 的 问题
- 求救,急等,谢谢先!
- MSComm控件的传输问题
- 根据选择的不同,在窗体同一块地方显示不同的界面,不要使用tabcontrol什么的,是怎么实现的,多谢!
- DOS下复制整个文件夹的命令是什么 ???!!
- 如何把一些信息写到一幅图像中?挺急的,在线等。
- 关于Batchmove的问题
- 怎样在“单击或双击”事件中增加树节点的图标?
- 急急!!寻求有关Delphi或C++Builder与硬件接口的资料
- DLL:cannot assign a tfont to a tfont
- DBGrid 表格间数据自动计算
如果只是这个程序中获得单击消息on mouse click就行了吧
如果是系统的话就要HOOK 消息了