C# 将鼠标移动到位置并左键单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13520705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move Mouse to Position and Left Click
提问by Mathlight
I'm working on an Windows Form Application in C#, Framework 4 (32 bit).
我正在使用 C# 框架 4(32 位)开发 Windows 窗体应用程序。
I have a list that holds coords of the mouse, and I can capture them. So far so good.
我有一个包含鼠标坐标的列表,我可以捕获它们。到现在为止还挺好。
But at some point, I want to go to those coords and left mouse click on it.
但是在某些时候,我想转到这些坐标并用鼠标左键单击它。
This is how it looks like right now:
这是它现在的样子:
for (int i = 0; i < coordsX.Count; i++)
{
Cursor.Position = new Point(coordsX[i], coordsY[i]);
Application.DoEvents();
Clicking.SendClick();
}
And the Clicking class:
和 Clicking 类:
class Clicking
{
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);
// public static void SendClick(Point location)
public static void SendClick()
{
// Cursor.Position = location;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}
}
But I'm getting this error:
但我收到此错误:
Could not load type 'program.Clicking' from assembly 'program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'mouse_event' has no implementation (no RVA).
And i realy don't understand what the problem is... Do you guys know what the problem is? or do you know an better way to do what i'm trying to do?
我真的不明白问题是什么......你们知道问题是什么吗?或者你知道做我想做的事情的更好方法吗?
采纳答案by Serdalis
Have you included the following line?
您是否包含以下行?
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
UIntPtr dwExtraInfo);
This will import the function mouse_eventfrom the user32dll, which is what you are trying to use within your program. Currently your program does not know about this method within the DLL untill you specify wher it comes from.
这将导入功能mouse_event从user32DLL,这是您要在程序中使用的。当前,您的程序不知道 DLL 中的此方法,直到您指定它的来源。
The website PInvoke.net user32 Mouse Eventis quite handy for the basics on this sort of thing.
网站PInvoke.net user32 Mouse Event对于这类事情的基础知识非常方便。
The answer to Directing mouse events [DllImport(“user32.dll”)] click, double clickwill be of great help to your understanding as well.
Directing mouse events [DllImport(“user32.dll”)] click, double click的答案对你的理解也有很大的帮助。
The flagsare what commands you want to send into the mouse_inputfunction, in that example you can see that he is sending both mouse downand mouse upin the same line, this is fine because the mouse_eventfunction will split those flags up and execute them consecutively.
将flags要发送到哪些命令是mouse_input功能,在例子中,你可以看到,他派出了mouse down和mouse up在同一条线上,这是很好的,因为该mouse_event功能会分裂的标志并连续执行。
Also note that this method has been superseded by the SendInputcommand, a good example of SendInputand SetMousePoscan be found At this Blog
另请注意,此方法已被SendInput命令取代SendInput,SetMousePos可以在此博客中找到一个很好的示例
回答by Afnan Bashir
I guess you are missing the following line
我想你错过了以下行
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

