Return to Snippet

Revision: 23658
at February 10, 2010 16:26 by jimfred


Updated Code
protected override void WndProc(ref Message m)
{
    const int WM_CLOSE = 0x0010;

    if (m.Msg == WM_CLOSE)
    {
        // I don't want 'X' to destroy the window - just hide it.
        // related: "Minimizing a window to the System Tray on a Close event" at
        // http://www.pixvillage.com/blogs/devblog/archive/2005/03/26/174.aspx
        this.Hide();
        return;
    }

    base.WndProc(ref m);
}

Revision: 23657
at February 10, 2010 16:21 by jimfred


Initial Code
protected override void WndProc(ref Message m)
      {
         const int WM_CLOSE = 0x0010;

         if (m.Msg == WM_CLOSE)
         {
            // I don't want 'X' to destroy the window - just hide it.
            // related: "Minimizing a window to the System Tray on a Close event" at
            // http://www.pixvillage.com/blogs/devblog/archive/2005/03/26/174.aspx
            this.Hide();
            return;
         }

         base.WndProc(ref m);
      }

Initial URL
http://bytes.com/topic/c-sharp/answers/258899-how-do-i-intercept-close-event

Initial Description
Typical scenario: app pops up a dialog, user clicks 'X' and the dialog disposes. If the app attempts to access the dialog then runtime errors occur. This approach allows the app to retain access to the dialog to re-show the dialog.

An alternative would be for the app to check the disposed state of the dialog but this  Close-->Hide approach is simpler and more transparent.

http://www.pixvillage.com/blogs/devblog/archive/2005/03/26/174.aspx

Source contains a simple WndProc that handles WM_CLOSE by calling Hide().

Initial Title
C# WinForm, cause 'close' ('X') to hide instead of disposing by handling the WM_CLOSE message in WndProc.

Initial Tags


Initial Language
C#