Force to close MessageBox programmatically

后端 未结 11 2121
清酒与你
清酒与你 2020-12-03 07:21

Let me give you the background.

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

These message bo

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 08:08

    I think the cleanest way would be to implement you own message box form like

    class MyMessageBox : Form {
      private MyMessageBox currentForm; // The currently active message box
    
      public static Show(....) { // same as MessageBox.Show
        // ...
      }
    
      public static Show(...) { // define additional overloads
      }
    
      public static CloseCurrent() {
        if (currentForm != null)
          currentForm.Close();
      }
    
      // ...
    }
    

    In some of my larger projects, I found this approach useful also for other purposes (such as automatic logging of error messages etc.)

    The second idea I have would be to use GetTopWindow() (or maybe some other WIN32 function) to get the current top-level window of your application and send a WM_CLOSE message to it.

提交回复
热议问题