在不使用鼠标的情况下在C#中生成鼠标单击

问题描述:

我已将鼠标光标设置到我的c#form应用程序中的特定坐标,但我不知道如何生成点击它。我想点击特定坐标而不使用鼠标。



我尝试了什么:



我不知道如何在c#中做到这一点,没有内置fn

I have set my mouse cursor to specific coordinate in my c# form application , but I dont know how to generate click on that.I want to click on the specific coordinate without using mouse.

What I have tried:

I dont know how to do it in c# , there is no built in fn

请看这里:如何:在代码中模拟鼠标和键盘事件 [ ^ ]


您需要使用互操作服务。这应该可以让你开始:



使用System.Runtime.InteropServices;



You need to use interop services. This should get you started:

using System.Runtime.InteropServices;

llImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, 
                      int dwData, int dwExtraInfo);

[Flags]
public enum MouseEventFlags
{
    LEFTDOWN = 0x00000002,
    LEFTUP = 0x00000004,
    MIDDLEDOWN = 0x00000020,
    MIDDLEUP = 0x00000040,
    MOVE = 0x00000001,
    ABSOLUTE = 0x00008000,
    RIGHTDOWN = 0x00000008,
    RIGHTUP = 0x00000010
}

public static void LeftClick(int x, int y)
{
    Cursor.Position = new System.Drawing.Point(x, y);
    mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
    mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}