【发布时间】:2013-05-23 17:43:15
【问题描述】:
正如标题所示,我正在尝试以编程方式模拟 MessageBox 中的按钮单击。我之前尝试通过标题找到它的句柄来关闭消息框,然后在SendMessage() 中应用WM_CLOSE 或SC_CLOSE。但是,由于存在是/否按钮,这不起作用(X 按钮变灰)。
现在我正在尝试单击“否”按钮,如下所示-:
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
IntPtr Window_hWnd = CloseMessageBox.FindWindowByCaption("#32770", "LastQuestion"); //Could use null as the first argument too. "#32770" represents classname Dialog.
CloseMessageBox.EnumChildWindows(Window_hWnd, (hWnd, lParam) =>
{
StringBuilder sb = new StringBuilder();
foreach (var control in GCHandle.FromIntPtr(lParam).Target as List<IntPtr>)
{
CloseMessageBox.GetWindowText(control, sb, 250);
if (sb.Equals("&No"))
{
CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseDown, 0, 0);
CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseUp, 0, 0);
}
}
return false;
}, GCHandle.ToIntPtr(listHandle));
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
在 IRC 某人的建议下走了这么远,我发现之前进行了一些编辑,我得到了按钮句柄(只有“&Yes”按钮),但不是全部。然后他提出了这种方法,但control 列表没有填充,因此它永远不会进入foreach。我该怎么做才能解决这个问题?
【问题讨论】:
-
根据您的问题,可能与stackoverflow.com/questions/2271883/…重复
-
@gunr2171: MessageBox 按钮点击肯定不同于表单控件点击?
-
我撤回我的声明。显然红得太快了。
标签: c# pinvoke findwindow