在 c# 中使用 WINAPI 单击消息框的“确定”按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14962081/
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
Click on 'OK' button of message box using WINAPI in c#
提问by Virus
I am trying to click on 'OK' button on a message box of C# windows form using winapi. Below is the code that I am working on.
我正在尝试使用 winapi 在 C# windows 窗体的消息框中单击“确定”按钮。下面是我正在处理的代码。
private const int WM_CLOSE = 16;
private const int BN_CLICKED = 245;
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
//this works
hwnd = FindWindow(null, "Message");
if(hwnd!=0)
SendMessage(hwnd, WM_CLOSE, 0, IntPtr.Zero);
//this doesn't work.
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "ok");
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
Though i get a value in hwndChild
, it is not recognising BN_CLICKED
.
I am not sure what am I missing. any help?
虽然我得到了一个值hwndChild
,但它没有识别BN_CLICKED
. 我不确定我错过了什么。有什么帮助吗?
I am trying to close the message box button of another application and this is what I am doing. But, I m still missing something.
我正在尝试关闭另一个应用程序的消息框按钮,这就是我正在做的。但是,我仍然缺少一些东西。
IntPtr hwndChild = IntPtr.Zero;
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero,' '"Button", "OK");
SendMessage((int)hwndChild, WM_COMMAND, (BN_CLICKED '<<16) | IDOK, hwndChild);
采纳答案by Virus
Finallu, this works for me. First click probably activates the window and second click clicks the button.
Finallu,这对我有用。第一次单击可能会激活窗口,第二次单击可能会单击按钮。
SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
回答by Dark Falcon
BN_CLICKED is not a message. You need to send a WM_COMMAND message containing the BN_CLICKEDnotification and the button ID in the wParam
and the button handle in lParam
.
BN_CLICKED 不是消息。您需要发送一条 WM_COMMAND 消息,其中包含BN_CLICKED通知和 中的按钮 IDwParam
和 中的按钮句柄lParam
。
The parent window of the button receives this notification code through the WM_COMMAND message.
按钮的父窗口通过 WM_COMMAND 消息接收此通知代码。
private const uint WM_COMMAND = 0x0111;
private const int BN_CLICKED = 245;
private const int IDOK = 1;
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
SendMessage(hwndChild, WM_COMMAND, (BN_CLICKED << 16) | IDOK, hwndChild);