c#通过句柄发送click
时间: 2023-11-12 19:00:31 浏览: 45
在 C# 中,可以使用 SendMessage 函数向指定的窗口发送消息。要发送一个单击事件,需要使用 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息。
以下是一个示例代码:
```csharp
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;
// 发送一个模拟的鼠标单击事件
public static void SendClick(IntPtr handle, int x, int y)
{
uint lparam = (uint)((y << 16) | x);
SendMessage(handle, WM_LBUTTONDOWN, IntPtr.Zero, (IntPtr)lparam);
SendMessage(handle, WM_LBUTTONUP, IntPtr.Zero, (IntPtr)lparam);
}
```
其中,handle 是要模拟单击事件的窗口句柄,x 和 y 是单击的坐标位置。
使用示例:
```csharp
IntPtr handle = FindWindow(null, "窗口标题");
SendClick(handle, 100, 100);
```
这个示例代码会在标题为“窗口标题”的窗口中模拟一个鼠标单击事件,单击位置为 (100, 100)。