gpt4 book ai didi

c++ - 模拟鼠标点击而不移动光标

转载 作者:行者123 更新时间:2023-11-30 16:27:57 33 4
gpt4 key购买 nike

我编写了一个应用程序来检测所有事件的 Windows 并将它们放入列表中。

有没有一种方法可以模拟鼠标单击屏幕上相对于 Windows 位置的某个点,而无需实际移动光标?

我无权访问应该单击的按钮句柄,只能访问窗口的句柄

最佳答案

Is there a way to simulate a mouseclick on a spot on the screen relative to the Windows location without actually moving the cursor?

回答您的具体问题 - 不。鼠标单击只能指向单击时鼠标光标实际所在的位置。模拟鼠标输入的正确方法是使用SendInput()(或旧系统上的mouse_event())。但是这些函数将模拟事件注入(inject)到实际鼠标驱动程序发布到的同一输入队列中,因此它们将对鼠标光标产生物理效果 - 即在屏幕上移动鼠标等。

How do I simulate input without SendInput?

SendInput operates at the bottom level of the input stack. It is just a backdoor into the same input mechanism that the keyboard and mouse drivers use to tell the window manager that the user has generated input. The SendInput function doesn't know what will happen to the input. That is handled by much higher levels of the window manager, like the components which hit-test mouse input to see which window the message should initially be delivered to.

When something gets added to a queue, it takes time for it to come out the front of the queue

When you call Send­Input, you're putting input packets into the system hardware input queue. (Note: Not the official term. That's just what I'm calling it today.) This is the same input queue that the hardware device driver stack uses when physical devices report events.

The message goes into the hardware input queue, where the Raw Input Thread picks them up. The Raw Input Thread runs at high priority, so it's probably going to pick it up really quickly, but on a multi-core machine, your code can keep running while the second core runs the Raw Input Thread. And the Raw Input thread has some stuff it needs to do once it dequeues the event. If there are low-level input hooks, it has to call each of those hooks to see if any of them want to reject the input. (And those hooks can take who-knows-how-long to decide.) Only after all the low-level hooks sign off on the input is the Raw Input Thread allowed to modify the input state and cause Get­Async­Key­State to report that the key is down.

完成您所要求的操作的唯一真正方法是找到位于所需屏幕坐标处的 UI 控件的 HWND。然后您可以:

  1. 直接向其发送 WM_LBUTTONDOWNWM_LBUTTONUP 消息。或者,对于标准 Win32 按钮控件,发送单个 BM_CLICK 消息。

  2. 使用 AccessibleObjectFromWindow() API 的 UI Automation 函数访问控件的 IAccessible 接口(interface),然后调用其 accDoDefaultAction() 方法,对于按钮来说,该方法将单击它。

话虽这么说,...

I don't have access to the buttons handle that is supposed to be clicked.

您可以访问任何具有 HWND 的内容。例如,看看 WindowFromPoint() 。您可以使用它来查找占据所需屏幕坐标的按钮的 HWND (当然,有注意事项: WindowFromPoint, ChildWindowFromPoint, RealChildWindowFromPoint, when will it all end? )。

关于c++ - 模拟鼠标点击而不移动光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566606/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com