强制以编程方式关闭MessageBox [英] Force to close MessageBox programmatically

查看:205
本文介绍了强制以编程方式关闭MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我为您提供背景信息.

Let me give you the background.

我们有一个Application(中等大小),它在各个地方(数百个地方)使用MessageBox.Show(....).

We have an Application(medium sized) that is using MessageBox.Show (....) at various places (in hundreds).

这些消息框是工作流的一部分,用于通知,警告或接受用户输入.如果没有活动,则应在一定时间后自动注销应用程序.我们有一个要求,在注销应用程序时,只是为了清理会话数据,清除视图并隐藏自身,以便在下次启动时不必执行启动过程,而启动过程会花费大量时间.

These message boxes are part of workflow and being used for informing,warning or taking input from an user. Application is supposed to automatically log off after certain time if there is no activity. We have a requirement that while logging out the application, just to clean the session data , to clear views and to hide itself so that in next launch, it won't have to execute the startup process which is costly in terms of time.

一切正常,但是在以下情况下:屏幕上有一些消息框,并且用户没有响应消息框就离开了计算机,然后由于没有活动使应用程序注销.问题是消息框不会消失.

Everything is working fine but in a scenario when there is some message box on the screen and user left the machine without responding to message box and then due to no activity to make the application to log out. Problem is Message box won't disappear.

在隐藏应用程序的同时如何关闭打开的消息框?

How I can close the opened messagebox, if any, while hiding the application?

推荐答案

以下是基于 UIAutomation (一种很酷但还不太常用的API),它试图关闭当前进程的所有模式窗口(包括用MessageBox打开的模式窗口):

Here is a piece of code based on UIAutomation (a cool but still not very used API) that attempts to close all modal windows (including the one opened with MessageBox) of the current process:

    /// <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关闭窗口"菜单(在任务栏中右键单击)关闭该应用程序:

For example, if you have a WinForms application that pops up a MessageBox when you press some button1, you will still be able to close the app using Windows "Close Window" menu (right click in the task bar):

    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,这只是一个示例.

You could use CloseModalWindows somewhere else in your code of course, this is just a sample.

这篇关于强制以编程方式关闭MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆