给你一个鼠标钩子的代码,稍改一下就能实现你要的功能。主窗口:
function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';
function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';procedure TForm1.Button1Click(Sender: TObject);
begin
if EnableHotKeyHook then
ShowMessage('HotKey Testing...');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('HotKey Testing..., DONE!!');
end;钩子工程文件:
library HKTest;uses
HKProc in 'HKProc.pas';exports
EnableHotKeyHook,
DisableHotKeyHook;begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.钩子单元文件:
unit HKProc;interfaceuses
Windows, Messages;var
hNextHookProc: HHook;
procSaveExit: Pointer;procedure HotKeyHookExit;
function mouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall;
function EnableHotKeyHook: BOOL;
function DisableHotKeyHook: BOOL;
implementationfunction mouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end; if ((wParam = wm_rbuttondown)or(wparam = WM_LBUTTONDBLCLK))
and(globalfindatom('appactivated')<>0) then
begin
Result := 1;
WinExec('Notepad.exe', sw_normal); // 记事本
end;
end;function EnableHotKeyHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_mouse 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_mouse,
mouseHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 mouse Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;procedure HotKeyHookExit;
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;end.
function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';
function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';procedure TForm1.Button1Click(Sender: TObject);
begin
if EnableHotKeyHook then
ShowMessage('HotKey Testing...');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('HotKey Testing..., DONE!!');
end;钩子工程文件:
library HKTest;uses
HKProc in 'HKProc.pas';exports
EnableHotKeyHook,
DisableHotKeyHook;begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.钩子单元文件:
unit HKProc;interfaceuses
Windows, Messages;var
hNextHookProc: HHook;
procSaveExit: Pointer;procedure HotKeyHookExit;
function mouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall;
function EnableHotKeyHook: BOOL;
function DisableHotKeyHook: BOOL;
implementationfunction mouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end; if ((wParam = wm_rbuttondown)or(wparam = WM_LBUTTONDBLCLK))
and(globalfindatom('appactivated')<>0) then
begin
Result := 1;
WinExec('Notepad.exe', sw_normal); // 记事本
end;
end;function EnableHotKeyHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_mouse 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_mouse,
mouseHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 mouse Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;procedure HotKeyHookExit;
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;end.
解决方案 »
- 大家来讨论一下,公司给员工的工资的依据:是能力还是对公司的贡献呢?
- 大虾指教: 怎么实现这样的屏幕效果 急急!!!
- 控件的属性使用方法,请帮忙.谢谢
- 除了tchart外,通过canvas如何画曲线
- 如何使得RxDBLookupCombo空白中默认填写下拉框内的第一条记录
- 用Delphi播放Flash的问题...
- ghost2002简体中文版序列号,那位知道?在线等待,急用!!!
- 紧急求教(来者有分)
- 如何使用FileStream进行文件的读写?
- 请教各位大吓关于数据加密...
- 如何在一张大图片上,定义若干形状不规则的热区???急急急!!!(csdn高手你们该出手了!)
- 怎么在api form中使用vcl组件?
if button=mbRight then
begin
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
CurPoint:TPoint;
Left,Top:integer;
begin
GetCursorPos(CurPoint);
form1.caption:='('+inttostr(CurPoint.x)
+','+inttostr(CurPoint.y)+')';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
CurPoint:TPoint;
Left,Top:integer;
begin
GetCursorPos(CurPoint);
form1.caption:='('+inttostr(CurPoint.x)
+','+inttostr(CurPoint.y)+')';
end;
begin
SetCapture(handle);//start Capture
end;
procedure button2.click
begin
ReleaseCapture(handle);//end Capture;
end;
procedure TForm1.onmousedown
var
pt:TPoint;
begin
GetCursorPos(pt);
判断左右键
end;
{
if(Message.Msg==WM_NCACTIVATE)
{
if(!(bool)Message.WParam)//失去焦点
{
TPoint CurPos;
GetCursorPos(&CurPos);
//做你想做的事
//...
}
}
}
var
curpos:tpoint;
begin
if (Message.Msg=WM_NCACTIVATE) then
begin
if(Message.WParam=0)//失去焦点
begin
GetCursorPos(CurPos);
//做你想做的事
//...
end;
end;end;
library HKTest;uses
Windows, Messages;var
hNextHookProc: HHook;
procSaveExit: Pointer;function mouseHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall;
var
H: thandle;
begin
Result := 0;
if iCode < 0 then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end; if (wParam = wm_rbuttondown) or (wparam = WM_LBUTTONDown) then
begin
Result := 1;
h := findwindow('TTestForm', 'TestForm');
if h <> 0 then sendmessage(h, wm_clear, 0, 0); //WinExec('Notepad.exe', sw_normal); // 记事本
end;
end;function EnableHotKeyHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_mouse 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_mouse,
mouseHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 mouse Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;procedure HotKeyHookExit;
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;exports
EnableHotKeyHook,
DisableHotKeyHook;begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.主程序:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, OleCtnrs;type
TTestForm = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure msghandle(var msg:tmessage);message wm_clear;
public
{ Public declarations }
end;var
TestForm: TTestForm;implementation{$R *.DFM}
function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';
function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';procedure ttestform.msghandle(var msg:tmessage);
var
pos:tpoint;
begin
getcursorpos(pos);
label1.caption:=inttostr(pos.x);
label2.caption:=inttostr(pos.y);
end;procedure TTestForm.Button1Click(Sender: TObject);
begin
EnableHotKeyHook;
end;procedure TTestForm.Button2Click(Sender: TObject);
begin
DisableHotKeyHook;
end;end.