SendMessage WM_LBUTTONDOWN/UP 适用于按钮,但不适用于窗口
我正在尝试使用 SendMessage 向 Windows 计算器发送一些简单的鼠标向下/向上消息。我已经能够通过直接向按钮发送消息来按下按钮。但是,我无法成功地将相同的消息发送到主计算器窗口句柄。鉴于 hWnd 是计算器的窗口句柄,这是我的代码。
IntPtr fiveKey = FindWindowEx(hWnd, IntPtr.Zero, "Button", "5");
int x = 5; // X coordinate of the click
int y = 5; // Y coordinate of the click
IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
const uint downCode = 0x201; // Left click down code
const uint upCode = 0x202; // Left click up code
SendMessage(fiveKey, downCode, wParam, lParam); // Mouse button down
SendMessage(fiveKey, upCode, wParam, lParam); // Mouse button up
谁能向我解释为什么将消息发送到 hWnd 而不是 FiveKey 并将 x/y 偏移量更改为“5”键的位置不起作用?我想最终使用此代码来模拟没有计算器等按钮的不同应用程序上的鼠标单击。
I am trying to send some simple mouse down/up messages to Windows Calculator using SendMessage. I have been able to press the buttons by sending the messages to the buttons directly. However, I have not been able to successfully send the same messages to the main calculator window handle. Given that hWnd is the window handle to calculator, this is my code.
IntPtr fiveKey = FindWindowEx(hWnd, IntPtr.Zero, "Button", "5");
int x = 5; // X coordinate of the click
int y = 5; // Y coordinate of the click
IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
const uint downCode = 0x201; // Left click down code
const uint upCode = 0x202; // Left click up code
SendMessage(fiveKey, downCode, wParam, lParam); // Mouse button down
SendMessage(fiveKey, upCode, wParam, lParam); // Mouse button up
Can anyone explain to me why sending the messages to hWnd instead of fiveKey with the x/y offsets changed to the position of the "5" key does not work? I would like to eventually use this code to simulate mouse clicks on a different application that doesn't have buttons like calculator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否关注你。您是否尝试将 WM_LBUTTONDOWN 发送到主窗口,其中包含 5 按钮所在的坐标,并希望 5 按钮将被“单击”?如果是这样,那是行不通的。 WM_LBUTTONDOWN 仅发送到鼠标光标下的窗口。理论上,主窗口可以处理 WM_LBUTTONDOWN 并查看其子窗口是否位于该位置,但没有人这样做,因为这不是 WM_LBUTTONDOWN 的设计工作方式。
I'm not sure I follow you. Are you trying to send WM_LBUTTONDOWN to the main window with the coordinates of where the 5 button is, with the hopes that the 5 button will get "clicked"? If so, that's just not going to work. WM_LBUTTONDOWN is only ever sent to the window under the mouse cursor. In theory the main window could handle WM_LBUTTONDOWN and see if any of its child windows are at that location, but nobody does that because that's not how WM_LBUTTONDOWN is designed to work.