我想在窗口(背景)x / y坐标处模拟左键单击。
我已经搜索并发现很多东西来模拟鼠标点击其他窗口,但它不能很好地工作。
我的代码:
private const int WM_LBUTTONDOWN = 0x201; //Left mousebutton down
private const int WM_LBUTTONUP = 0x202; //Left mousebutton up
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
public static extern bool PostMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
public void SimulateMouseLeft(IntPtr hwnd, int x,int y)
{
int coordinates = x | (y << 16);
//PostMessage(hwnd, WM_LBUTTONDOWN, IntPtr.Zero, (IntPtr)coordinates);
//PostMessage(hwnd, WM_LBUTTONUP, IntPtr.Zero, (IntPtr)coordinates);
PostMessage(hwnd, WM_LBUTTONDOWN, (IntPtr)0x1, (IntPtr)coordinates);
PostMessage(hwnd, WM_LBUTTONUP, (IntPtr)0x1, (IntPtr)coordinates);
//send left button up
//PostMessage(hwnd, 0x202, (IntPtr)0x1, (IntPtr)coordinates);
}
如果我启动此代码并模拟单击到另一个窗口,则另一个窗口会左键单击而不是x / y。点击位于我的鼠标所在的位置,点击后我的鼠标跳到屏幕上的另一个位置。
感谢您的帮助!