+-
c – 在不移动光标的情况下模拟鼠标单击
我编写了一个应用程序来检测所有活动的 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()).但是这些函数将模拟事件注入到实际鼠标驱动程序发布到的相同输入队列中,因此它们将对鼠标光标产生物理效果 – 即在屏幕上移动它等.

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.然后你可以:

>直接向其发送WM_LBUTTONDOWNWM_LBUTTONUP消息.或者,在标准Win32按钮控件的情况下,发送单个BM_CLICK消息.
>使用UI Automation API的AccessibleObjectFromWindow()功能访问控件的IAccessible接口,然后调用其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 – 在不移动光标的情况下模拟鼠标单击 - 乐贴网