【发布时间】:2013-02-19 16:12:47
【问题描述】:
我正在尝试使用 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);
虽然我在hwndChild 中得到了一个值,但它无法识别BN_CLICKED。
我不确定我错过了什么。有什么帮助吗?
我正在尝试关闭另一个应用程序的消息框按钮,这就是我正在做的事情。但是,我仍然缺少一些东西。
IntPtr hwndChild = IntPtr.Zero;
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero,' '"Button", "OK");
SendMessage((int)hwndChild, WM_COMMAND, (BN_CLICKED '<<16) | IDOK, hwndChild);
【问题讨论】:
-
既然你使用的是C#,你也可以使用
System.Windows.Automation命名空间。这是pushes the "7" button in Calculator 的示例。只需将“计算器”更改为“消息”,将“7”更改为“确定”即可。
标签: c# winapi messagebox