qwidget捕获鼠标事件_勾住鼠标以捕获应用程序外的事件

qwidget捕获鼠标事件

Learn how to track the mouse activity even when your Delphi application is not active, sits in the tray or does not have any UI at all.

了解即使在您的Delphi应用程序处于非活动状态,坐在托盘中或根本没有任何UI的情况下 ,如何跟踪鼠标活动。

By installing a system-wide (or global) mouse hook you can monitor what the user is doing with the mouse and act accordingly.

通过安装系统范围(或全局)的鼠标挂钩,您可以监视用户使用鼠标所做的事情并采取相应的措施。

什么是挂钩,它如何工作? ( What Is a Hook and How Does It Work? )

In short, a hook is a (callback) function you can create as part of a DLL (dynamic link library) or your application to monitor the 'goings on' inside the Windows operating system.There are 2 types of hooks — global and local. A local hook monitors things happening only for a specific program (or thread). A global hook monitors the entire system (all threads).

简而言之,钩子是一个( 回调 )函数,您可以将其创建为DLL( 动态链接库 )或应用程序的一部分,以监视Windows操作系统内部的“运行中”。钩子有两种类型:全局和本地钩子。 本地挂钩监视仅针对特定程序(或线程)发生的事情。 全局挂钩监视整个系统(所有线程)。

To create a global hook you need 2 projects, 1 to make the executable file and 1 to make a DLL containing the hook procedure.

要创建全局挂钩,您需要2个项目,其中1个创建可执行文件,1个创建包含挂钩过程的DLL。

Our article on working with keyboard hooks from Delphi explains how to intercept the keyboard input for controls that cannot receive the input focus (like TImage).

我们有关使用Delphi的键盘挂钩的文章介绍了如何拦截无法接收输入焦点的控件的键盘输入(例如TImage)。

钩鼠标 ( Hooking the Mouse )

By design, the movement of the mouse is restricted by the size of your desktop screen (including the Windows Task Bar). When you move the mouse to the left/right/top/bottom edge, the mouse will "stop" — as expected (if you do not have more that one monitor).

根据设计,鼠标的移动受桌面屏幕(包括Windows任务栏)大小的限制。 当您将鼠标移到左/右/上/下边缘时,鼠标将“停止” —如预期的那样(如果您没有一台以上的显示器)。

Here's an idea for the system-wide mouse hook: If for example, you want to move the mouse to the right side of the screen when it moves toward the left edge (and "touches" it), you might write a global mouse hook to reposition the mouse pointer.

这是系统范围的鼠标挂钩的一个想法:例如,如果要在鼠标向左边缘移动(并“触摸”)时将鼠标移动到屏幕的右侧,则可以编写一个全局鼠标挂钩重新定位鼠标指针。

You start by creating a dynamic link library project. The DLL should export two methods: "HookMouse" and "UnHookMouse".

您首先创建一个动态链接库项目。 DLL应该导出两个方法:“ HookMouse”和“ UnHookMouse”。

The HookMouse procedure calls the SetWindowsHookEx API passing the "WH_MOUSE" for the first parameter — thus installing a hook procedure that monitors mouse messages. One of the parameters to the SetWindowsHookEx is your callback function Windows will call when there is a mouse message to be processed:

HookMouse过程调用SetWindowsHookEx API,并为第一个参数传递“ WH_MOUSE”,从而安装了监视鼠标消息的钩子过程。 SetWindowsHookEx的参数之一是您的回调函数,当有鼠标消息要处理时,Windows将调用它:

SetWindowsHookEx(WH_MOUSE, @HookProc, HInstance,0) ;

SetWindowsHookEx(WH_MOUSE,@HookProc,HInstance,0);

The last parameter (value = 0) in the SetWindowsHookEx defines we are registering a global hook.

SetWindowsHookEx中的最后一个参数(值= 0)定义我们正在注册一个全局钩子。

The HookProc parses the mouse related messages and sends a custom message ("MouseHookMessage") to our test project:

HookProc解析与鼠标有关的消息,并将自定义消息(“ MouseHookMes​​sage”)发送到我们的测试项目:

 function HookProc(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
    mousePoint: TPoint;
    notifyTestForm : boolean;
    MouseDirection : TMouseDirection;
 begin
    mousePoint := PMouseHookStruct(Data)^.pt;
    notifyTestForm := false;
    if (mousePoint.X = 0) then
    begin
      Windows.SetCursorPos(-2 + Screen.Width, mousePoint.y) ;
      notifyTestForm := true;
      MouseDirection := mdRight;
    end;

....
....
if notifyTestForm then
    begin
      PostMessage(FindWindow('TMainHookTestForm', nil), MouseHookMessage, MsgID, Integer(MouseDirection)) ;
    end;

   Result := CallNextHookEx(Hook,nCode,MsgID,Data) ;
结果:= CallNextHookEx(Hook,nCode,MsgID,Data);
end;

Tip: Read the Win32 SDK Help files to find out about the PMouseHookStruct record and the signature of the HookProc function.

提示 :阅读Win32 SDK帮助文件以了解有关PMouseHookStruct记录和HookProc函数的签名。

Note: A hook function does not need to send anything anywhere - the PostMessage call is used only to indicate that the DLL can communicate with the "outer" world.

注意 :钩子函数不需要在任何地方发送任何内容-PostMessage调用仅用于指示DLL可以与“外部”世界进行通信。

鼠标钩“侦听器” ( Mouse Hook "Listener" )

The "MouseHookMessage" message is posted to your test project — a form named "TMainHookTestForm". You'll override the WndProc method to get the message and act as needed:

“ MouseHookMes​​sage”消息被发布到您的测试项目-名为“ TMainHookTestForm”的表单。 您将重写WndProc方法以获取消息并根据需要进行操作:

 procedure TMainHookTestForm.WndProc(var Message: TMessage) ;
begin
    inherited WndProc(Message) ;
if Message.Msg = HookCommon.MouseHookMessage then
    begin
      //implementation found in the accompanying code
      Signal(TMouseDirection(Message.LParam)) ;
    end;
end;

Of course, when the form is created (OnCreate) you call the HookMouse procedure from the DLL, when it gets closed (OnDestroy) you call the UnHookMouse procedure.

当然,当窗体创建(OnCreate)时,您可以从DLL调用HookMouse过程,当窗体关闭(OnDestroy)时,您可以调用UnHookMouse过程。

Note: Hooks tend to slow down the system because they increase the amount of processing the system must perform for each message. You should install a hook only when necessary, and remove it as soon as possible.

注意:挂钩通常会使系统变慢,因为它们增加了系统必须为每个消息执行的处理量。 仅应在必要时安装挂钩,并尽快将其卸下。

翻译自: https://www.thoughtco.com/how-to-hook-the-mouse-1058467

qwidget捕获鼠标事件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值