winform用SendMessage怎么发送一个鼠标消息,指定鼠标在某个窗口(32,32)处按下,指定坐标在sendmess

2024-12-15 20:40:05
推荐回答(2个)
回答1:

你先要用FindWindow(string lpClassName,string lpWindowName)获取该窗口的句柄
IntPtr hwnd QQ= FindWindow(null,"QQ2011");这样就能获取QQ登录窗口的句柄
然后再使用SendMessage(IntPtr hwnd,int msg,IntPtr wParam,IntPtr lParam)
向该窗口发送消息
SendMessage(hwndQQ,WM_LBUTTONDBLCLK ,MK_LBUTTON,32 * 65535 + 32);
public const int WM_LBUTTONDBLCLK = 515
public const int MK_LBUTTON = 1;
(在电脑中搜索winuser.h,这个文件中包含消息值)
这个消息是左键单击消息
lParam的低16位填鼠标位置的x值,高16位填y值
要先声明
[DllImport( "user32.dll ", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lparam);
[DllImport( "user32.dll ", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

回答2:

///


/// 发送鼠标左键单击消息
///

/// X坐标
/// Y坐标
public static void EsSendKey_MouseLeftClick(int x, int y)
{
int xx = Screen.PrimaryScreen.Bounds.Width;
int yy = Screen.PrimaryScreen.Bounds.Height;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x * 65536 / xx, y * 65536 / yy, 0, 0);
}

///
/// 鼠标事件
///

[DllImport("user32.dll")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

没用过SendMessage 我自己写的