- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
让我给你介绍一下背景。
我们有一个应用程序(中型),它在不同的地方(数百个)使用 MessageBox.Show (....)。
这些消息框是工作流的一部分,用于通知、警告或接受用户的输入。如果没有事件,应用程序应该在一定时间后自动注销。我们有一个要求,在注销应用程序时,只清理 session 数据,清除 View 并隐藏自身,以便在下一次启动时,它不必执行耗时的启动过程。
一切正常,但在屏幕上有一些消息框并且用户离开机器而没有响应消息框的情况下,然后由于没有事件使应用程序注销。问题是消息框不会消失。
如何在隐藏应用程序的同时关闭打开的消息框(如果有)?
最佳答案
这是一段基于UIAutomation的代码(一个很酷但仍然不是很常用的 API)试图关闭当前进程的所有模态窗口(包括使用 MessageBox 打开的窗口):
/// <summary>
/// Attempt to close modal windows if there are any.
/// </summary>
public static void CloseModalWindows()
{
// get the main window
AutomationElement root = AutomationElement.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
if (root == null)
return;
// it should implement the Window pattern
object pattern;
if (!root.TryGetCurrentPattern(WindowPattern.Pattern, out pattern))
return;
WindowPattern window = (WindowPattern)pattern;
if (window.Current.WindowInteractionState != WindowInteractionState.ReadyForUserInteraction)
{
// get sub windows
foreach (AutomationElement element in root.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)))
{
// hmmm... is it really a window?
if (element.TryGetCurrentPattern(WindowPattern.Pattern, out pattern))
{
// if it's ready, try to close it
WindowPattern childWindow = (WindowPattern)pattern;
if (childWindow.Current.WindowInteractionState == WindowInteractionState.ReadyForUserInteraction)
{
childWindow.Close();
}
}
}
}
}
例如,如果您有一个 WinForms 应用程序,当您按下某个按钮 1 时会弹出一个 MessageBox,您仍然可以使用 Windows“关闭窗口”菜单(在任务栏中右键单击)关闭该应用程序:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Don't click me. I want to be closed automatically!");
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND) // this is sent even if a modal MessageBox is shown
{
if ((int)m.WParam == SC_CLOSE)
{
CloseModalWindows();
Close();
}
}
base.WndProc(ref m);
}
当然,您可以在代码的其他地方使用 CloseModalWindows,这只是一个示例。
关于c# - 强制以编程方式关闭 MessageBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7926107/
我遇到了一个奇怪的问题。我正在 VC++ 2008 中制作 Win32 应用程序,制作一个类来封装大部分工作,以便在调用 MessageBox 时轻松重复。 .消息框已创建(我认为),但除非我按下 A
如果我的应用程序崩溃,我会使用 ExceptionFilter 来捕获崩溃,执行一些最终操作,然后向用户显示应用程序已崩溃的消息框。 因为应用程序已经崩溃,我能(或不敢)做的事情不多,因为如果我做的太
既然“messagebox”是“tkinter”包中的一个模块,为什么我必须使用 from tkinter import messagebox 代替 import tkinter.messagebox
出于某种原因,WPF MessageBox 看起来比 WindowsForms 中的更糟糕。 WPF 没有视觉样式。 如何将 Windows 样式应用于 WPF MessageBox? Windows
我无法找出两个消息框之间的主要区别。 System.Windows.MessageBox 和 System.Windows.Forms.MessageBox 有什么区别? 最佳答案 System.Wi
我通过将位图的构建操作设置为Splashscreen来使用WPF中的SplashScreen功能。在启动屏幕后面,正在检查许可信息,如果失败,我会显示一个 MessageBox。 根据这个Feedba
我正在使用http://dev.sencha.com/playpen/docs/output/Ext.MessageBox.html#prompt显示 extjs 提示,用户可以在其中输入一些文本,然
有了这个,默认就可以了。我可以设置默认取消吗? MessageBoxResult result = MessageBox.Show (message, caption, Messag
我正在将第三方基于 C 的 SDK 集成到我的 .NET 应用程序中。该应用程序将作为 Windows 服务在服务器上运行,因此它不应以任何方式与用户交互。 不幸的是,在某些错误情况下,它坚持调用 M
我有一个程序可以让用户在 2 个值之间进行选择。为此,我想打开一个带有 2 个按钮(确定、取消)和一个可供选择或写入值的字段的框。就像 QMessageBox 一样,可以填写/选择数据。我可以创建一个
如何将自定义按钮添加到 WPF 中现有的 MessageBox 中?除了通常的“确定”和“取消”按钮之外,我还需要添加 3 个按钮并处理它们的事件。 最佳答案 简短回答:不,这是不可能的,您需要编写一
我在 Windows 窗体上有几个组框,其中包含复选框、数字选择器和组合框。如果未选中该复选框,我希望禁用其他元素。我已经禁用了除加载复选框之外的所有内容,然后在 CheckedChanged 事件上
我有一个 c# 程序,正在尝试使用特定字符串(普通字符串,没什么特别的)调用 messageBox,并且当到达要与字符串连接的变量时,它显然会停止连接。代码: string first = userI
我一直认为 MessageBoxes 有点像断点——它们会阻止程序执行,直到用户单击“确定”。但是,我刚刚发现这是不准确的,我正试图弄清楚 MessageBoxes 何时会和不会停止执行。 通过 SO
我正在尝试显示一条简单的消息,我过去可能已经这样做过数千次,而现在……完整的文本字符串没有显示在 MessageBox 中。我没有做任何不同的事情,所以我没有看到问题。这是我的代码: if (Mess
我在使用模态消息框时遇到问题。 情况是这样的 用户从表单中选择 xx 出现消息框 用户打开嵌入式软件键盘(内置键盘,来自设备) 用户关闭键盘 MessageBox 失去焦点(怎么会?它应该是模态的!)
我正在使用 Kinect 开发一个使用 C# 和 WPF 的图像查看应用程序。当用户将双手放在头顶时,我会显示一个 Messagebox 询问用户是否真的想退出: //Quit if both
当我点击回车按钮时,显示MessageBox。当我像往常一样再次单击输入按钮时,我希望 MessageBox 关闭。问题是 - 它没有焦点,但 TextBox 有,当我单击输入按钮时 _textBox
我正在尝试找到一种从线程内部调用 MessageBox 的方法。但是我发现的每一段代码/示例都不起作用。有人可以用尽可能少的代码行以最简单、最简单的方式详细说明解决方案/示例吗? 到目前为止,这是我的
我在如下启动的 WPF 应用程序中有一个简单的消息框: private void Button_Click(object sender, RoutedEventArgs e) { Message
我是一名优秀的程序员,十分优秀!