SendMessage WM_LBUTTONDOWN/UP对按钮有效,但对窗口无效 [英] SendMessage WM_LBUTTONDOWN/UP works on buttons but not window

查看:909
本文介绍了SendMessage WM_LBUTTONDOWN/UP对按钮有效,但对窗口无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SendMessage将一些简单的鼠标按下/向上消息发送到Windows计算器.通过将消息直接发送到按钮,我已经能够按下按钮.但是,我无法将相同的消息成功发送到主计算器窗口句柄.鉴于hWnd是计算器的窗口句柄,这就是我的代码.

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

有人可以向我解释为什么将消息发送到hWnd而不是将x/y偏移更改为"5"键位置的FiveKey发送给hWnd吗?我最终希望使用此代码来模拟在没有计算器等按钮的其他应用程序上的鼠标点击.

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.

推荐答案

我不确定我是否关注您.您是否正在尝试将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.

这篇关于SendMessage WM_LBUTTONDOWN/UP对按钮有效,但对窗口无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆