什么可能导致 PostMessage 发送的鼠标点击被忽略?
我正在寻找一种将鼠标点击发送到 Windows 上的后台应用程序(即通过句柄)的方法,我用来确认我的代码正在工作的测试窗口接受并处理点击,但我的目标应用程序不接受并处理点击(即使 Spy++显示消息)。
可能是什么原因造成的?有解决方法吗?
这是我正在使用的 C# 代码。
public enum WMessages : int
{
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14,
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public void SendClick(WMessages type, Point pos)
{
switch(type)
{
case WMessages.WM_LBUTTONDOWN:
PostMessage(new HandleRef(null, this.process.MainWindowHandle),
(UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
case WMessages.WM_LBUTTONUP:
PostMessage(new HandleRef(null, this.process.MainWindowHandle),
(UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
default:
return;
}
}
SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages,WM_LBUTTONUP, Cursor.Position);
这有可能实现吗?有没有更好的方法来实现这一目标?
注意:当应用程序处于活动状态并且鼠标悬停在正确位置时,上述代码也不起作用。我还在寻找专门将输入发送到后台应用程序的方法,因此 SendInput 和其他方法是不可能的。
谢谢
I was looking for a way to send mouse clicks to a background application on Windows (ie. by handle), The test window I used to confirm my code was working accepts and processes the clicks, but my target application does not (even though Spy++ shows the messages).
What could be causing this? And is there a work-around?
here's the C# code i'm using.
public enum WMessages : int
{
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14,
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public void SendClick(WMessages type, Point pos)
{
switch(type)
{
case WMessages.WM_LBUTTONDOWN:
PostMessage(new HandleRef(null, this.process.MainWindowHandle),
(UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
case WMessages.WM_LBUTTONUP:
PostMessage(new HandleRef(null, this.process.MainWindowHandle),
(UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
default:
return;
}
}
SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages,WM_LBUTTONUP, Cursor.Position);
Is this possible to achieve? Is there a better way of acheiving this?
Note: The above code doesn't work when the application is active and the mouse is hovered in the correct location, either. I'm also looking specifically send input to a background application, so SendInput and others are out of the question.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以只要先读(1)然后读(2)你的问题就解决了
so just read (1) first then (2) and your problem is solved
您是否尝试使用 SendMessage 调用而不是 PostMessage ? SendMessage 立即调用窗口的处理程序。 PostMessage 将消息放入列表中以供稍后处理。
Did you try a SendMessage call, instead of PostMessage? SendMessage immediately calls the handler for the Window. PostMessage puts the message on a list for later processing.