以下代码是此处的转发:
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)
我认为你正在寻找的功能是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)
我确信如果您在互联网上搜索 PostMessage 鼠标事件的示例,您会发现很多