windows SendMessage WM_LBUTTONDOWN/UP 适用于按钮但不适用于窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3443325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SendMessage WM_LBUTTONDOWN/UP works on buttons but not window
提问by Tom Sweeney
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.
我正在尝试使用 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
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.
谁能向我解释为什么将消息发送到 hWnd 而不是 FiveKey 并将 x/y 偏移量更改为“5”键的位置不起作用?我想最终使用此代码来模拟鼠标在没有计算器等按钮的不同应用程序上的点击。
采纳答案by Luke
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.
我不确定我是否跟随你。您是否尝试将 WM_LBUTTONDOWN 发送到带有 5 按钮所在坐标的主窗口,希望 5 按钮会被“点击”?如果是这样,那是行不通的。WM_LBUTTONDOWN 只发送到鼠标光标下的窗口。理论上主窗口可以处理 WM_LBUTTONDOWN 并查看它的任何子窗口是否在该位置,但没有人这样做,因为这不是 WM_LBUTTONDOWN 的设计方式。