你如何模拟C#中的鼠标点击?

New*_*bie 116 .net c# winforms

你如何模拟C#中的鼠标点击?

Mar*_*ona 127

一个例子,我发现某处 这里过去.可能会有所帮助:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class Form1 : Form
{
   [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
   public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
   //Mouse actions
   private const int MOUSEEVENTF_LEFTDOWN = 0x02;
   private const int MOUSEEVENTF_LEFTUP = 0x04;
   private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
   private const int MOUSEEVENTF_RIGHTUP = 0x10;

   public Form1()
   {
   }

   public void DoMouseClick()
   {
      //Call the imported function with the cursor's current position
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
   }

   //...other code needed for the application
}
Run Code Online (Sandbox Code Playgroud)

  • 也许在某处[这里](http://www.gamedev.net/topic/321029-how-to-simulate-a-mouse-click-in-c/)? (12认同)
  • 你需要将X和Y转换为uints. (12认同)
  • 打的好.添加了对原始答案的引用 (3认同)
  • 我在这段代码中遇到错误:调用PInvoke函数'MyForm!MyForm.Form1 :: mouse_event'使堆栈失去平衡.这很可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.有任何想法吗? (2认同)

Kei*_*ith 125

我已经结合了几个来源来生成下面的代码,我目前正在使用它.我还删除了Windows.Forms引用,因此我可以在没有其他引用的情况下从控制台和WPF应用程序中使用它.

using System;
using System.Runtime.InteropServices;

public class MouseOperations
{
    [Flags]
    public enum MouseEventFlags
    {
        LeftDown = 0x00000002,
        LeftUp = 0x00000004,
        MiddleDown = 0x00000020,
        MiddleUp = 0x00000040,
        Move = 0x00000001,
        Absolute = 0x00008000,
        RightDown = 0x00000008,
        RightUp = 0x00000010
    }

    [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetCursorPos(int x, int y);      

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetCursorPos(out MousePoint lpMousePoint);

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

    public static void SetCursorPosition(int x, int y) 
    {
        SetCursorPos(x, y);
    }

    public static void SetCursorPosition(MousePoint point)
    {
        SetCursorPos(point.X, point.Y);
    }

    public static MousePoint GetCursorPosition()
    {
        MousePoint currentMousePoint;
        var gotPoint = GetCursorPos(out currentMousePoint);
        if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
        return currentMousePoint;
    }

    public static void MouseEvent(MouseEventFlags value)
    {
        MousePoint position = GetCursorPosition();

        mouse_event
            ((int)value,
             position.X,
             position.Y,
             0,
             0)
            ;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MousePoint
    {
        public int X;
        public int Y;

        public MousePoint(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 鉴于问题含糊不清,我认为人们可能会从一个允许他们做更多事情的例子中获益,而不仅仅是左键点击右键或中键点击或允许点击和拖动. (42认同)
  • 我看不出这个长源代码的优势超过17个月前发布的Marcos Placona. (2认同)
  • 效果很好.我来这里寻找类似的东西 (2认同)
  • @ninjaxelite 我猜你发送了`1。SetCursorPos, 2. MouseEvent(LeftButtonDown), 3. SetCursorPos, 4. MouseEvent(LeftButtonUp)`。可能将其包装在辅助方法中 (2认同)

小智 14

一些控件,如System.Windows.Forms中的Button,有一个"PerformClick"方法来做到这一点.


Rus*_*ail 7

Mouse.Click();
Run Code Online (Sandbox Code Playgroud)

Microsoft.VisualStudio.TestTools.UITesting

  • 我只是花了一个小时试图通过将 DLL 从“编码的 UI 测试”项目复制到另一个项目来使其工作,除非您使用的是“编码的 UI 测试”项目类型,否则我的建议是:不要打扰。即使我复制了所有必需的 DLL,它也会抛出一个 `InvalidUITestExtensionPackageException`,因此遗憾的是,在特定项目类型中运行似乎非常繁琐。 (3认同)
  • 看起来Visual Studio Community版本没有附带此DLL。 (2认同)

小智 5

我已经尝试过 Marcos 发布的代码,但它对我不起作用。无论我被赋予 Y 坐标,光标都不会在 Y 轴上移动。如果您希望光标的位置相对于桌面的左上角,而不是相对于您的应用程序,则下面的代码将起作用。

    [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);
    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_MOVE = 0x0001;

    public void DoMouseClick()
    {
        X = Cursor.Position.X;
        Y = Cursor.Position.Y;

        //move to coordinates
        pt = (Point)pc.ConvertFromString(X + "," + Y);
        Cursor.Position = pt;       

        //perform click            
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
Run Code Online (Sandbox Code Playgroud)

我只使用 mouse_event 函数来实际执行点击。你可以给 X 和 Y 你想要的坐标,我使用了文本框中的值:

            X = Convert.ToInt32(tbX.Text);
            Y = Convert.ToInt32(tbY.Text);
Run Code Online (Sandbox Code Playgroud)