Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force to close MessageBox programmatically

Tags:

Let me give you the background.

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?

like image 410
NYK Avatar asked Oct 28 '11 07:10

NYK


People also ask

How to close Message box?

Click the x button from the top right corner of the dialog box that you'd like to close. Clicking this button should close the box and make it vanish.

What is the 2nd parameter in MessageBox show ()?

The first parameter msg is the string displayed in the dialog box as the message. The second and third parameters are optional and respectively designate the type of buttons and the title displayed in the dialog box. MsgBox Function returns a value indicating which button the user has chosen.

Which of the following is default button of MessageBox control?

MessageBox with Default Button By default, the first button is the default button. The MessageBoxDefaultButton enumeration is used for this purpose and it has the following three values. The following code snippet creates a MessageBox with a title, buttons, and an icon and sets the second button as a default button.


1 Answers

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();                     }                 }             }         }     } 

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);     } 

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

like image 126
Simon Mourier Avatar answered Sep 18 '22 07:09

Simon Mourier