使用DELPHI制作钩子 如何用DELPHI制作1个钩子钩住某个程序,在这个程序中我可以使用快捷键呼出我原来用DEIPHI制作的FORM,因为小弟学习不久,还请各位大大帮忙,最好有事例和部分说明,小弟在次先谢谢拉! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 做个键盘钩子,截取键盘消息,响应快捷键调用你的程序;具体查询hook的相关资料吧,我也没用过 我之前在CSDN发过一个键盘钩子的示例,你所需要做的是扩展一下,对键盘特定按键进行响应 我有类似的代码可供你参考,键盘和鼠标的HOOK主程序:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;const WM_MyMessage = WM_USER + 1;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure MyMessage(var msg: TMessage); message WM_MyMessage; {定义一个消息方法接受消息} end; function SetHook: Boolean; stdcall; function DelHook: Boolean; stdcall; function SetHWnd(hwnd: HWND): Boolean; stdcall;var Form1: TForm1;implementation{$R *.dfm}function SetHook; external 'MyHook.dll';function DelHook; external 'MyHook.dll';function SetHWnd; external 'MyHook.dll';procedure TForm1.FormCreate(Sender: TObject);begin FormStyle := fsStayOnTop; {为了测试, 让窗口一直在前面} SetHook; SetHWnd(Handle);end;procedure TForm1.FormDestroy(Sender: TObject);begin DelHook;end;{把接受到的内容显示在窗体}procedure TForm1.MyMessage(var msg: TMessage);begin Text := PChar(msg.LParam);end;end.DLL如下:library MyHook;uses SysUtils, Windows, Messages, Classes, dialogs;{$R *.res}const WM_MyMessage = WM_USER + 1; {自定义消息}var hook: HHOOK; info: string; h: HWND; {用作外部窗口的句柄}{获取外部窗口的句柄}function SetHWnd(hwnd: HWND): Boolean; stdcall;begin h := hwnd; Result := True;end;function MouseHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;begin case wParam of WM_MOUSEMOVE : info := '鼠标位置'; WM_LBUTTONDOWN : info := '按下'; WM_LBUTTONUp : info := '放开'; end; info := Format('%s: %d,%d', [info, PMouseHookStruct(lParam)^.pt.X, PMouseHookStruct(lParam)^.pt.Y]); {通过消息把数据传递给指定窗口} PostMessage(h, WM_MyMessage, 0, Integer(PChar(info))); Result := CallNextHookEx(hook, nCode, wParam, lParam);end;function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;const_KeyPressMask = $80000000;begin Result := 0; //注释后就可以屏蔽键盘 If iCode < 0 Then begin Result := CallNextHookEx(hook,iCode,wParam,lParam); Exit; end; if ((lParam and _KeyPressMask) = 0) then begin info:=chr(wparam); PostMessage(h, WM_MyMessage, 0, Integer(PChar(info))); end;end;function SetHook: Boolean; stdcall;const WH_MOUSE_LL =14;begin //hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHook, HInstance, 0); Hook :=SetWindowsHookEx(WH_KEYBOARD,@KeyboardHookHandler,HInstance,0); Result := hook <> 0;end;function DelHook: Boolean; stdcall;begin Result := UnhookWindowsHookEx(hook);end;exports SetHook, DelHook, MouseHook, SetHWnd;beginend. 使用 钩子对 那个引用程序下一个热键,之后就能处理了。如何使用钩子,可以参考:HookInjEx在网上有很多介绍,不过是C++版本的。 你们在客户的ODBC源是怎么创建的? DataSnap2009问题 继承的窗体显示时为什么闪烁? 求救TDateTimePicker出错 图片存储问题???? 我的这段update语句不报错,但是数据库也没有更新,为什么◎ 招聘软件开发工程师 我要寻找一种在接受点击时会陷下去的按钮控件... ... DBgrid 为何不能显示数据?急,急,急. 我想实现播放图片的功能!!那位高手肯帮帮忙!!!紧急!!有源代码!!! 如何通过TabOrder的设定控制Button的Enable? 如何对数据库中的text,image这类字段中写入数据啊?
主程序:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;const WM_MyMessage = WM_USER + 1;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MyMessage(var msg: TMessage); message WM_MyMessage; {定义一个消息方法接受消息}
end; function SetHook: Boolean; stdcall;
function DelHook: Boolean; stdcall;
function SetHWnd(hwnd: HWND): Boolean; stdcall;var
Form1: TForm1;implementation{$R *.dfm}function SetHook; external 'MyHook.dll';
function DelHook; external 'MyHook.dll';
function SetHWnd; external 'MyHook.dll';procedure TForm1.FormCreate(Sender: TObject);
begin
FormStyle := fsStayOnTop; {为了测试, 让窗口一直在前面}
SetHook;
SetHWnd(Handle);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
DelHook;
end;{把接受到的内容显示在窗体}
procedure TForm1.MyMessage(var msg: TMessage);
begin
Text := PChar(msg.LParam);
end;end.
DLL如下:library MyHook;uses
SysUtils,
Windows,
Messages,
Classes,
dialogs;{$R *.res}const WM_MyMessage = WM_USER + 1; {自定义消息}var
hook: HHOOK;
info: string;
h: HWND; {用作外部窗口的句柄}{获取外部窗口的句柄}
function SetHWnd(hwnd: HWND): Boolean; stdcall;
begin
h := hwnd;
Result := True;
end;function MouseHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
case wParam of
WM_MOUSEMOVE : info := '鼠标位置';
WM_LBUTTONDOWN : info := '按下';
WM_LBUTTONUp : info := '放开';
end;
info := Format('%s: %d,%d', [info, PMouseHookStruct(lParam)^.pt.X, PMouseHookStruct(lParam)^.pt.Y]); {通过消息把数据传递给指定窗口}
PostMessage(h, WM_MyMessage, 0, Integer(PChar(info))); Result := CallNextHookEx(hook, nCode, wParam, lParam);
end;function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin Result := 0; //注释后就可以屏蔽键盘
If iCode < 0 Then
begin
Result := CallNextHookEx(hook,iCode,wParam,lParam);
Exit;
end;
if ((lParam and _KeyPressMask) = 0) then
begin
info:=chr(wparam);
PostMessage(h, WM_MyMessage, 0, Integer(PChar(info)));
end;
end;function SetHook: Boolean; stdcall;
const
WH_MOUSE_LL =14;
begin
//hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHook, HInstance, 0);
Hook :=SetWindowsHookEx(WH_KEYBOARD,@KeyboardHookHandler,HInstance,0);
Result := hook <> 0;
end;function DelHook: Boolean; stdcall;
begin
Result := UnhookWindowsHookEx(hook);
end;exports SetHook, DelHook, MouseHook, SetHWnd;
begin
end.
如何使用钩子,可以参考:
HookInjEx
在网上有很多介绍,不过是C++版本的。