我的问题已解决,用了2个钩子,日志钩子处理键盘,低级鼠标钩子处理鼠标,现在贴出来,
谁有更好的解决办法呢?
-------------------------------------------
library hookprj;
uses
SysUtils,
Classes,
hkprocunit in 'hkprocunit.pas';
{$R *.RES}
exports
EnableMouseHook,
DisableMouseHook,
EnableKeyHook,
DisableKeyHook;
begin
end.
------------------------------------------------------------
unit hkprocunit;
interface
uses
forms, extctrls, Windows, Messages, stdctrls, Sysutils, Classes;
var
hkMouse: HHOOK;
hkKey: HHOOK;
f_wnd: Hwnd;
f_rect: TRect;
f_form: TForm;
f_panel: TPanel;
function MouseHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT; stdcall;
function EnableMouseHook(a_form: TForm; a_panel: Tpanel; a_wnd: Hwnd):Boolean; stdcall; export;
function DisableMouseHook:Boolean; stdcall; export;
function KeyHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT; stdcall;
function EnableKeyHook(a_wnd: Hwnd):Boolean; stdcall; export;
function DisableKeyHook:Boolean; stdcall; export;
implementation
function MouseHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
var
m: PMOUSEHOOKSTRUCT;
v_rect: TRect;
PEvt: ^EVENTMSG;
begin
Result := 0;
if nCode < 0 then
begin
Result := CallNextHookEx(hkMouse,nCode,WParam,LParam);
exit;
end;
case wParam of
WM_RBUTTONDOWN,
WM_RBUTTONUP,
WM_LBUTTONDOWN,
WM_LBUTTONUP:
begin
m := PMOUSEHOOKSTRUCT(lParam);
v_rect := f_panel.BoundsRect;
MapWindowPoints(f_form.handle, 0, v_rect, 2); // 座标换算
Dec(v_rect.Right, 18);
Dec(v_rect.Bottom, 18);
// 如果鼠标在v_rect范围内
if PtInRect(v_rect, Point(m.pt.x, m.pt.y)) then
// 如果Form处于激活状态
if GetActiveWindow=f_form.handle then
begin
Result := 1; //丢弃消息
exit;
end;
end;
end;
Result := CallNextHookEx(hkMouse, nCode, wParam, lParam);
end;
function EnableMouseHook(a_form: TForm; a_panel: Tpanel; a_wnd: Hwnd):Boolean; stdcall; export;
begin
f_form := a_form;
f_panel := a_panel;
f_wnd := a_wnd;
if hkMouse = 0 then
begin
hkMouse := SetWindowsHookEx(14, @MouseHookProc,Hinstance,0); //使用低级鼠标钩子
Result := True;
end
else
Result := False;
end;
function DisableMouseHook:Boolean; stdcall; export;
begin
if hkMouse <> 0 then
begin
UnHookWindowsHookEx(hkMouse);
hkMouse := 0;
Result := True;
end
else
Result := False;
end;
function KeyHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
var
PEvt: ^EVENTMSG;
begin
Result := 0;
if nCode < 0 then
begin
Result := CallNextHookEx(hkKey,nCode,WParam,LParam);
exit;
end;
if (nCode = HC_ACTION) then
begin
pEvt := Pointer(DWord(lParam));
if (pEvt.message = WM_KEYDOWN) or (pEvt.message = WM_KEYUP) then
if f_wnd=GetActiveWindow then
begin
pEvt.paramL := 0; //将按键键值置为0
end;
end;
Result := CallNextHookEx(hkKey, nCode, wParam, lParam);
end;
function EnableKeyHook(a_wnd: Hwnd):Boolean; stdcall; export;
begin
f_wnd := a_wnd;
if hkKey = 0 then
begin
hkKey := SetWindowsHookEx(WH_JOURNALRECORD,@KeyHookProc,Hinstance,0);
Result := True;
end
else
Result := False;
end;
function DisableKeyHook:Boolean; stdcall; export;
begin
if hkKey <> 0 then
begin
UnHookWindowsHookEx(hkKey);
hkMouse := 0;
Result := True;
end
else
Result := False;
end;
end.
-----------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, RxHook, OleCtrls, SHDocVw, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
Panel1: TPanel;
WebBrowser1: TWebBrowser;
Button4: TButton;
Button5: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
type
TWBPosition = (wbPosBottom, wbPosTop, wbPosRight);
var
Form1: TForm1;
v_wnd: HWND;
v_rec: TRect;
//下面是引用hookprj.dll中的函数。
function EnableMouseHook(a_form: TForm; a_panel: Tpanel; a_wnd: Hwnd):Boolean; stdcall; external 'Hookprj.dll';
function DisableMouseHook:Boolean; stdcall; external 'Hookprj.dll';
function EnableKeyHook(a_wnd: Hwnd):Boolean; stdcall; external 'Hookprj.dll';
function DisableKeyHook:Boolean; stdcall; external 'Hookprj.dll';
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
EnableMouseHook(Form1, Panel1, v_wnd);
EnableKeyHook(v_wnd);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DisableMouseHook;
DisableKeyHook;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
OpenDialog1.Execute;
WebBrowser1.Navigate(OpenDialog1.FileName);
v_wnd := GetActiveWindow;
end;
end.
------------------------------------------------------------------