简体   繁体   English

如何模拟按下鼠标并保持在C#中

[英]How to simulate mouse button down and being hold in C#

I hope to simulate a left mouse button click on another window, and hold the button for about 2 seconds. 我希望模拟鼠标左键单击另一个窗口,并按住该按钮约2秒钟。 I have tried the following code: 我尝试了以下代码:

        int WM_LBUTTONDOWN = 0x0201;
        int WM_LBUTTONUP = 0x0202;
        SendMessage(hd, WM_LBUTTONDOWN, new IntPtr(1), lParam);           
        Thread.Sleep(2000);
        SendMessage(hd, WM_LBUTTONUP, new IntPtr(1), lParam);

The parameter "hd" is the handle of another window and "lParam" contains coordinate infomation. 参数“ hd”是另一个窗口的句柄,“ lParam”包含坐标信息。 But it didn't work as my expectation.I used breakpoint to debug the code. 但这没有达到我的预期。我使用断点来调试代码。 When "WM_LBUTTONDOWN" message was sent to another window, the push button in another window was clicked immediately, rather than be held and wait for the message "WM_LBUTTONUP". 当“ WM_LBUTTONDOWN”消息发送到另一个窗口时,立即单击另一个窗口中的按钮,而不是按住它并等待消息“ WM_LBUTTONUP”。

When I used real mouse to click and hold the button, spy++ showed that there are not any other messages except "WM_MOUSEMOVE" between "WM_LBUTTONDOWN" and "WM_LBUTTONUP". 当我使用真正的鼠标单击并按住按钮时,spy ++显示“ WM_LBUTTONDOWN”和“ WM_LBUTTONUP”之间除了“ WM_MOUSEMOVE”之外没有其他消息。 Picture of Spy++ showed 显示的Spy ++图片

So, how to simulate mouse button down and being hold in C#? 那么,如何模拟鼠标按下并保持在C#中? Any advice would be helpful, thank you! 任何建议都会有所帮助,谢谢!

API函数“ mouse_event”可以解决问题。但是副作用是鼠标实际上会移动,在程序运行时您不能移动鼠标,否则会意外单击错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM