How do I get control when the close box is clicked?

  • Thread starter Thread starter newscorrespondent
  • Start date Start date
N

newscorrespondent

I would like to display a form to get some cleanup instructions when an app
ends. From the Application.exit method my dialog flashes and leaves.
Is there any way to capture the event before it is too late to show a form?

Thanks
 
You can always subscribe to the Closing/Closed event of the main form
for the application.

Hope this helps.
 
That was the first thing I tried because I was already doing other work
there. I even took the example form from the docs and tried to use that just
to see if something was wrong with my code. I got the same thing. The dialog
flashes but does not stay long enough to be seen let alone respond to.

Thanks
 
I tried writting a message filter hoping to trap the WM_QUIT message. I
never get such a message. Is there something wrong with this filter?

class TracerMessageFilter : IMessageFilter
{ enum WindowsMessageNumbers
{ WM_NULL = 0X0000,
WM_CREATE = 0X0001,
WM_DESTROY = 0X0002,
WM_MOVE = 0X0003,
WM_SIZE = 0X0005,
WM_ACTIVATE = 0X0006,
WM_SETFOCUS = 0X0007,
WM_KILLFOCUS = 0X0008,
WM_ENABLE = 0X000A,
WM_SETREDRAW = 0X000B,
WM_SETTEXT = 0X000C,
WM_GETTEXT = 0X000D,
WM_GETTEXTLENGTH = 0X000E,
WM_PAINT = 0X000F,
WM_CLOSE = 0X0010,
WM_QUERYENDSESSION = 0X0011,
WM_QUERYOPEN = 0X0013,
WM_ENDSESSION = 0X0016,
WM_QUIT = 0X0012,
WM_ERASEBKGND = 0X0014,
WM_SYSCOLORCHANGE = 0X0015,
WM_SHOWWINDOW = 0X0018,
WM_WININICHANGE = 0X001A,
WM_DEVMODECHANGE = 0X001B,
WM_ACTIVATEAPP = 0X001C,
WM_FONTCHANGE = 0X001D,
WM_TIMECHANGE = 0X001E,
WM_CANCELMODE = 0X001F,
WM_SETCURSOR = 0X0020,
WM_MOUSEACTIVATE = 0X0021,
WM_CHILDACTIVATE = 0X0022,
WM_QUEUESYNC = 0X0023,
WM_GETMINMAXINFO = 0X0024,
WM_PAINTICON = 0X0026,
WM_ICONERASEBKGND = 0X0027,
WM_NEXTDLGCTL = 0X0028,
WM_SPOOLERSTATUS = 0X002A,
WM_DRAWITEM = 0X002B,
WM_MEASUREITEM = 0X002C,
WM_DELETEITEM = 0X002D,
WM_VKEYTOITEM = 0X002E,
WM_CHARTOITEM = 0X002F,
WM_SETFONT = 0X0030,
WM_GETFONT = 0X0031,
WM_SETHOTKEY = 0X0032,
WM_GETHOTKEY = 0X0033,
WM_QUERYDRAGICON = 0X0037,
WM_COMPAREITEM = 0X0039,
WM_GETOBJECT = 0X003D,
WM_COMPACTING = 0X0041,
WM_COMMNOTIFY = 0X0044,
WM_WINDOWPOSCHANGING = 0X0046,
WM_WINDOWPOSCHANGED = 0X0047,
WM_POWER = 0X0048,
WM_COPYDATA = 0X004A,
WM_CANCELJOURNAL = 0X004B,
WM_NOTIFY = 0X004E,
WM_INPUTLANGCHANGEREQUEST = 0X0050,
WM_INPUTLANGCHANGE = 0X0051,
WM_TCARD = 0X0052,
WM_HELP = 0X0053,
WM_USERCHANGED = 0X0054,
WM_NOTIFYFORMAT = 0X0055,
WM_CONTEXTMENU = 0X007B,
WM_STYLECHANGING = 0X007C,
WM_STYLECHANGED = 0X007D,
WM_DISPLAYCHANGE = 0X007E,
WM_GETICON = 0X007F,
WM_SETICON = 0X0080,
WM_NCCREATE = 0X0081,
WM_NCDESTROY = 0X0082,
WM_NCCALCSIZE = 0X0083,
WM_NCHITTEST = 0X0084,
WM_NCPAINT = 0X0085,
WM_NCACTIVATE = 0X0086,
WM_GETDLGCODE = 0X0087,
WM_SYNCPAINT = 0X0088,
WM_NCMOUSEMOVE = 0X00A0,
WM_NCLBUTTONDOWN = 0X00A1,
WM_NCLBUTTONUP = 0X00A2,
WM_NCLBUTTONDBLCLK = 0X00A3,
WM_NCRBUTTONDOWN = 0X00A4,
WM_NCRBUTTONUP = 0X00A5,
WM_NCRBUTTONDBLCLK = 0X00A6,
WM_NCMBUTTONDOWN = 0X00A7,
WM_NCMBUTTONUP = 0X00A8,
WM_NCMBUTTONDBLCLK = 0X00A9,
WM_NCXBUTTONDOWN = 0X00AB,
WM_NCXBUTTONUP = 0X00AC,
WM_NCXBUTTONDBLCLK = 0X00AD,
WM_INPUT = 0X00FF,
WM_KEYFIRST = 0X0100,
WM_KEYDOWN = 0X0100,
WM_KEYUP = 0X0101,
WM_CHAR = 0X0102,
WM_DEADCHAR = 0X0103,
WM_SYSKEYDOWN = 0X0104,
WM_SYSKEYUP = 0X0105,
WM_SYSCHAR = 0X0106,
WM_SYSDEADCHAR = 0X0107,
WM_UNICHAR = 0X0109,
WM_KEYLAST109 = 0X0109,
WM_KEYLAST108 = 0X0108,
WM_IME_STARTCOMPOSITION = 0X010D,
WM_IME_ENDCOMPOSITION = 0X010E,
WM_IME_COMPOSITION = 0X010F,
WM_IME_KEYLAST = 0X010F,
WM_INITDIALOG = 0X0110,
WM_COMMAND = 0X0111,
WM_SYSCOMMAND = 0X0112,
WM_TIMER = 0X0113,
WM_HSCROLL = 0X0114,
WM_VSCROLL = 0X0115,
WM_INITMENU = 0X0116,
WM_INITMENUPOPUP = 0X0117,
WM_MENUSELECT = 0X011F,
WM_MENUCHAR = 0X0120,
WM_ENTERIDLE = 0X0121,
WM_MENURBUTTONUP = 0X0122,
WM_MENUDRAG = 0X0123,
WM_MENUGETOBJECT = 0X0124,
WM_UNINITMENUPOPUP = 0X0125,
WM_MENUCOMMAND = 0X0126,
WM_CHANGEUISTATE = 0X0127,
WM_UPDATEUISTATE = 0X0128,
WM_QUERYUISTATE = 0X0129,
WM_CTLCOLORMSGBOX = 0X0132,
WM_CTLCOLOREDIT = 0X0133,
WM_CTLCOLORLISTBOX = 0X0134,
WM_CTLCOLORBTN = 0X0135,
WM_CTLCOLORDLG = 0X0136,
WM_CTLCOLORSCROLLBAR = 0X0137,
WM_CTLCOLORSTATIC = 0X0138,
WM_MOUSEFIRST = 0X0200,
WM_MOUSEMOVE = 0X0200,
WM_LBUTTONDOWN = 0X0201,
WM_LBUTTONUP = 0X0202,
WM_LBUTTONDBLCLK = 0X0203,
WM_RBUTTONDOWN = 0X0204,
WM_RBUTTONUP = 0X0205,
WM_RBUTTONDBLCLK = 0X0206,
WM_MBUTTONDOWN = 0X0207,
WM_MBUTTONUP = 0X0208,
WM_MBUTTONDBLCLK = 0X0209,
WM_XBUTTONDOWN = 0X020B,
WM_XBUTTONUP = 0X020C,
WM_XBUTTONDBLCLK = 0X020D,
WM_MOUSELAST = 0X020D,
WM_MOUSELAST20A = 0X020A,
WM_MOUSELAST209 = 0X0209,
WM_PARENTNOTIFY = 0X0210,
WM_ENTERMENULOOP = 0X0211,
WM_EXITMENULOOP = 0X0212,
WM_NEXTMENU = 0X0213,
WM_SIZING = 0X0214,
WM_CAPTURECHANGED = 0X0215,
WM_MOVING = 0X0216,
WM_POWERBROADCAST = 0X0218,
WM_DEVICECHANGE = 0X0219,
WM_MDICREATE = 0X0220,
WM_MDIDESTROY = 0X0221,
WM_MDIACTIVATE = 0X0222,
WM_MDIRESTORE = 0X0223,
WM_MDINEXT = 0X0224,
WM_MDIMAXIMIZE = 0X0225,
WM_MDITILE = 0X0226,
WM_MDICASCADE = 0X0227,
WM_MDIICONARRANGE = 0X0228,
WM_MDIGETACTIVE = 0X0229,
WM_MDISETMENU = 0X0230,
WM_ENTERSIZEMOVE = 0X0231,
WM_EXITSIZEMOVE = 0X0232,
WM_DROPFILES = 0X0233,
WM_MDIREFRESHMENU = 0X0234,
WM_IME_SETCONTEXT = 0X0281,
WM_IME_NOTIFY = 0X0282,
WM_IME_CONTROL = 0X0283,
WM_IME_COMPOSITIONFULL = 0X0284,
WM_IME_SELECT = 0X0285,
WM_IME_CHAR = 0X0286,
WM_IME_KEYDOWN = 0X0290,
WM_IME_KEYUP = 0X0291,
WM_MOUSEHOVER = 0X02A1,
WM_MOUSELEAVE = 0X02A3,
WM_NCMOUSEHOVER = 0X02A0,
WM_NCMOUSELEAVE = 0X02A2,
WM_WTSSESSION_CHANGE = 0X02B1,
WM_TABLET_FIRST = 0X02c0,
WM_TABLET_LAST = 0X02df,
WM_CUT = 0X0300,
WM_COPY = 0X0301,
WM_PASTE = 0X0302,
WM_CLEAR = 0X0303,
WM_UNDO = 0X0304,
WM_RENDERFORMAT = 0X0305,
WM_RENDERALLFORMATS = 0X0306,
WM_DESTROYCLIPBOARD = 0X0307,
WM_DRAWCLIPBOARD = 0X0308,
WM_PAINTCLIPBOARD = 0X0309,
WM_VSCROLLCLIPBOARD = 0X030A,
WM_SIZECLIPBOARD = 0X030B,
WM_ASKCBFORMATNAME = 0X030C,
WM_CHANGECBCHAIN = 0X030D,
WM_HSCROLLCLIPBOARD = 0X030E,
WM_QUERYNEWPALETTE = 0X030F,
WM_PALETTEISCHANGING = 0X0310,
WM_PALETTECHANGED = 0X0311,
WM_HOTKEY = 0X0312,
WM_PRINT = 0X0317,
WM_PRINTCLIENT = 0X0318,
WM_APPCOMMAND = 0X0319,
WM_THEMECHANGED = 0X031A,
WM_HANDHELDFIRST = 0X0358,
WM_HANDHELDLAST = 0X035F,
WM_AFXFIRST = 0X0360,
WM_AFXLAST = 0X037F,
WM_PENWINFIRST = 0X0380,
WM_PENWINLAST = 0X038F,
WM_APP = 0X8000,
}
public bool PreFilterMessage (ref Message m)
{ ShowWindowsMessage(m.Msg);
return false;
}
private void ShowWindowsMessage (int msgno)
{ WindowsMessageNumbers mymsg = (WindowsMessageNumbers) msgno;
Console.WriteLine("Message Number " + msgno.ToString()
+ "---" + string.Format("{0:X}", msgno)
+ "---" + mymsg.ToString());
}
}
 
It seems that an Application IFilter only gets a few of the messages. If I
get the messages at the WNDPROC for the form I am interested in I can do
what I want but this seems to be much more work than it should be.

protected override void WndProc(ref Message m)
{ MTGStatic.WindowsMessageNumbers mymsg = (MTGStatic.WindowsMessageNumbers)
m.Msg;
switch (mymsg)
{ case MTGStatic.WindowsMessageNumbers.WM_SYSCOMMAND:
MTGStatic.WindowsSystemCommands mycommand =
(MTGStatic.WindowsSystemCommands) m.WParam;
switch (mycommand)
{ case MTGStatic.WindowsSystemCommands.SC_CLOSE:
MTGTracer.AskStatusQuestions();
break;
}
break;
}
base.WndProc(ref m);
}
 
That was the first thing I tried because I was already doing other work
there. I even took the example form from the docs and tried to use that just
to see if something was wrong with my code. I got the same thing. The dialog
flashes but does not stay long enough to be seen let alone respond to.

Are you opening the other form with Show() or ShowDialog()? Calling
ShowDialog in OnClosing should work! If not, please post the shortest
possible working program to demonstrate the problem.

Max
 
I had tried both and neither worked.

If I make a simple application. Use the wizzard and then copy the example
dialog code from the docs it works as you said it would, so I must have
something else going on to mess things up.

It even works if I put the dialog after the run statment in the main
function.

I guess more digging is in order.

Thanks
Tom
 
Back
Top