• 全部
  • 语言基础/算法/系统设计
  • 数据库相关
  • 图形处理/多媒体
  • 网络通信/分布式开发
  • VCL组件开发及应用
  • Windows SDK/API
  • 问答

鼠标hook问题?(很困惑)

dudo 2003-03-21 05:46:53
我的程序需要在任何时候知道用户点下了那个鼠标键,所以我做了一个
全局的hook,但是这个hook只能截获我自己的程序的鼠标消息,其他程序的截获不了,请高手指点一下。

library MyHook;
uses
Windows,
Messages,
HookUnit in 'HookUnit.pas';

exports
InstallHook,
UninstallHook,
IsHooked;
end.
unit HookUnit;

interface
uses
Windows,Messages;

const
MSG_HOOK_MOUSE_EVENT='My_MOUSE_EVENT';

function InstallHook(Hwnd: Cardinal): Boolean;
function UninstallHook: Boolean;
function IsHooked: Boolean;

implementation
var
MouseHookEvent: integer;
HookHandle: HHook;
WindowHandle: HWND;

function MouseHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
begin
if nCode = HC_ACTION then
begin
with PMouseHookStruct(lParam)^ do
PostMessage(WindowHandle,MouseHookEvent, wParam, (pt.x and $FFFF) or (pt.y shl 16));
end;
Result:=CallNextHookEx(HookHandle, nCode, wParam, lParam);
end;

function InstallHook(Hwnd: Cardinal): Boolean;
begin
if HookHandle=0 then
begin
if MouseHookEvent=0 then
MouseHookEvent:=RegisterWindowMessage(PChar(MSG_HOOK_MOUSE_EVENT));
WindowHandle := Hwnd;
HookHandle:=SetWindowsHookEx(WH_MOUSE, @MouseHookProc, HInstance , 0);
end;
Result := HookHandle>0;
end;

function UninstallHook: Boolean;
begin
if HookHandle<>0 then
begin
UnhookWindowsHookEx(HookHandle);
HookHandle := 0;
end;
Result:=HookHandle=0;
end;

function IsHooked: Boolean;
begin
Result:=HookHandle<>0;
end;

end.

unit Demo;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, AppEvnts;
const
MSG_HOOK_MOUSE_EVENT='EBOARD_MOUSE_EVENT';

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
winHandle: HWND;
myMsgId: integer;
procedure mymsg(var msg: TMessage);
public
{ Public declarations }
end;

var
Form1: TForm1;
function InstallHook(WindowHandle: HWND) : Boolean;external 'eBoardHook.dll';
function DisableHotKeyHook :Boolean;external 'eBoardHook.dll';

implementation

{$R *.dfm}
{ TForm1 }

procedure TForm1.mymsg(var msg: TMessage);
begin
if msg.Msg=MyMsgId then
if Msg.WParam <> WM_MouseMove then
memo1.Lines.Add('asdfd');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if not installHook(winHandle) then
showmessage('adsfds');
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
WinHandle := Classes.AllocateHWnd(MyMsg);
mymsgid:=RegisterWindowMessage(PChar(MSG_HOOK_MOUSE_EVENT));
end;
...全文
9 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dudo 2003-03-21
有点小错误,'EBOARD_MOUSE_EVENT'应为 'My_MOUSE_EVENT'
回复
发帖
Delphi
创建于2007-08-02

4870

社区成员

Delphi 开发及应用
申请成为版主
帖子事件
创建了帖子
2003-03-21 05:46
社区公告
暂无公告