C# 模拟点击另一个窗口中的按钮
我想关闭自动弹出的对话框,但在使其正常工作时遇到一些问题。 经过几年的有限使用,我的 Win32 编程有点生疏了。
我正在使用 FindWindowEx 获取对话框的句柄和我想要单击的按钮。 我的印象是,将 WM_COMMAND 发送到对话框,并在 wParam 参数中添加按钮句柄就可以解决问题。
Window window = Window.FindWindow("TSomeDialog", null);
Window cancelButton = Window.FindWindow("TButton", "Cancel", window);
Message message = Message.Create(window.HWnd, 0x0111, cancelButton.HWnd, IntPtr.Zero);
PostMessage(message);
public void PostMessage(Message message)
{
// Win32 API import
PostMessage(message.HWnd, message.Msg, message.WParam, message.LParam);
}
Window 是一个实现 IWin32Window 并包装一些 Win32 API 调用的类。 我已经内联了 WM_COMMAND (0x111) 常量。
我究竟做错了什么? :)
I'd like to close a dialog that pops up automatically, but I'm having some trouble getting it to work. My Win32 programming is a bit rusty after several years of limited usage.
I'm using FindWindowEx to get handles to the dialog and the button I want to click. I was under the impression that sending a WM_COMMAND to the dialog, with the button handle in the wParam parameter would do the trick.
Window window = Window.FindWindow("TSomeDialog", null);
Window cancelButton = Window.FindWindow("TButton", "Cancel", window);
Message message = Message.Create(window.HWnd, 0x0111, cancelButton.HWnd, IntPtr.Zero);
PostMessage(message);
public void PostMessage(Message message)
{
// Win32 API import
PostMessage(message.HWnd, message.Msg, message.WParam, message.LParam);
}
Window is a class that implements IWin32Window and wraps some Win32 API calls. I have inlined the constant for WM_COMMAND (0x111).
What am I doing wrong? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,根据 WM_COMMAND 的文档,lParam 应该是控件窗口的句柄(看起来您在 wParam 中传递它)。
wParam 的高位字应等于 BN_CLICKED,其低位字应等于控件的标识符。
(您可以使用 GetWindowLong 和 GWL_ID 来检索它,但可能是它的 IDCANCEL。)
Well, according to the documentation for WM_COMMAND, lParam should be the handle to the control's window (it looks like you're passing it in wParam).
wParam should have its high order word equal to BN_CLICKED and its low order word equal to the control's identifier.
(You can use GetWindowLong with GWL_ID to retrieve this, but presumably its IDCANCEL.)
为什么不直接发送带有 SC_CLOSE 参数的 WM_SYSCOMMAND 消息呢? 那应该关闭窗口。
Why not just send a WM_SYSCOMMAND message with a SC_CLOSE parameter? That should close the window.
为什么不发送 WM_CLOSE 消息呢?
Why not send a WM_CLOSE message instead ?