C# 单击屏幕上的某个点
我正在尝试通过 C# 执行鼠标单击。我使用 mouse_event 函数来做到这一点。
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
我尝试了两种方法:
将鼠标移动到该点并单击它:
Cursor.Position = new Point(100, 100);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
将所需单击的 x, y 传递给函数:
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,100, 100, 0, 0);
无论哪种方式,我都是这个奇怪的错误:
托管调试助手“PInvokeStackImbalance”已检测到“中存在问题” C:\Users\or\Documents\Visual Studio 2010\Projects\ProjectName\ProjectName\bin\Debug\ProjectName.vshost.exe'。 其他信息:对 PInvoke 函数“ProjectName!ProjectName.MainForm::mouse_event”的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。
关于解决方案有什么想法吗?
I am trying to perform a mouse click through c#. I used the mouse_event function to do it.
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
I tried two methods :
Moving the mouse to the point and clicking it :
Cursor.Position = new Point(100, 100);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
Passing the x, y of the desired click to the function :
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,100, 100, 0, 0);
Either way, I'm this weird error :
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\or\Documents\Visual Studio 2010\Projects\ProjectName\ProjectName\bin\Debug\ProjectName.vshost.exe'.
Additional Information: A call to PInvoke function 'ProjectName!ProjectName.MainForm::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Any ideas on a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用以下 p/invoke 签名
注意,虽然
mouse_event
很方便,但它已被 SendInput。您可以在以下 URL 中找到 SendInput 和 Input 结构的合理 p/invoke 声明。http://www.pinvoke.net/default.aspx/user32.SendInput
I would suggest you use the following p/invoke signature
Note, that while
mouse_event
is convenient, it has been superseded by SendInput. You can find a reasonable p/invoke declaration for SendInput and the Input structure at the following URL.http://www.pinvoke.net/default.aspx/user32.SendInput
显然,您已经从旧版 VB6 代码中获得了此代码。 long 在 .NET 和 C# 中是 64 位,与 VB6 不同,在 VB6 中它是 32 位。
从long改成int,应该就可以了。
编辑:我有点快:dwExtraInfo 应该是 IntPtr 类型。
You have obviously got this code from legacy VB6 code. long is 64-bit in .NET and C#, unlike VB6 where it is 32-bit.
Switch from long to int, and it should be okay.
edit: I was a little fast: dwExtraInfo should be of type IntPtr.