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

Fru*_*unk 3 c# mouse virtual click

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

我该怎么做?

Bri*_*ian 7

以下代码是此处的转发:

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
}
Run Code Online (Sandbox Code Playgroud)


Ron*_*ijm 4

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

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

您可以在 codeproject 上阅读更多相关信息,并下载一个发送击键的演示项目。

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

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

PostMessage(hWnd, WM_LBUTTONDBLCLK, 0, l);
Run Code Online (Sandbox Code Playgroud)

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

我确信如果您在互联网上搜索 PostMessage 鼠标事件的示例,您会发现很多