如何执行虚拟鼠标而不使用鼠标单击C#

时间:2013-02-28 21:47:38

标签: c# click mouse virtual

我想在Windows应用程序中执行单击,而不使用真正的鼠标(因此我可以将其最小化)。就像机器人的行为一样。

我该怎么做?

7 个答案:

答案 0 :(得分:6)

以下代码是here的转贴:

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);

  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
     int X = Cursor.Position.X;
     int Y = Cursor.Position.Y;
     mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
  }
  //...other code needed for the application
}

答案 1 :(得分:4)

当我模拟击键和鼠标点击时,我使用Windows Input Simulator

答案 2 :(得分:3)

我认为您正在寻找的功能是PostMessage

[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

您可以阅读有关它的更多信息here on codeproject,并下载一个发送按键的演示项目。

此方法根据您使用的进程句柄直接在与程序关联的输入队列上发布消息(hWnd)

您还可以使用此功能通过发布按钮事件来发送鼠标点击,如下所示:

PostMessage(hWnd, WM_LBUTTONDBLCLK, 0, l);

可以找到有关这些按钮事件的更多信息here on MSDN

我敢肯定,如果您在互联网上搜索PostMessage鼠标事件的样本,您会发现很多

答案 3 :(得分:2)

您可以使用计时器或按键按键,并在计时器勾选或按键功能中创建模拟鼠标单击,使用user32.dll文件(最好采用表格形状,以便您可以处理计时器间隔.. 。):

using System;
using System.Windows.Forms;
namespace Clicker
{
    public partial class Form1 : Form
    {
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    private const int MOUSEEVENT_LEFTDOWN = 0x02;
    private const int MOUSEEVENT_LEFTUP = 0x04;
    private const int MOUSEEVENT_MIDDLEDOWN = 0x20;
    private const int MOUSEEVENT_MIDDLEUP = 0x40;
    private const int MOUSEEVENT_RIGHTDOWN = 0x08;
    private const int MOUSEEVENT_RIGHTUP = 0x10;

    private int count = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
        label2.Text = "Timer is on";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        label2.Text = "Timer is off";
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
        count++;
        label3.Text = count + " amount of clicks";
    }

}

}

答案 4 :(得分:1)

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);    
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

public void MouseClick()
{
   int x = 100;
   int y = 100;
   mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
   mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

我在http://social.msdn.microsoft.com/forums/en-US/winforms/thread/86dcf918-0e48-40c2-88ae-0a09797db1ab/找到了这个。

答案 5 :(得分:1)

您需要使用其所在机器的分辨率,使用System.Windows.Forms.Screen课程,此处:SO

然后,您需要将鼠标移动到该位置,或者避免这种情况,您可能需要挂钩正在运行的程序,并向其发送导致其最小化的事件。

使用C#很难得到这样的东西,因为你需要将这个DLL注入到程序中。像C这样的低级语言可能会有所帮助。

Here's a brief explanation / question

答案 6 :(得分:0)

您可以使用Window Automation在窗口中查找Minimize按钮并执行单击。我发现它很容易并且使用了很多。这是了解整个概念的链接。 https://msdn.microsoft.com/en-us/library/windows/desktop/ff486375(v=vs.85).aspx