Is there anyway to detect when the user has clicked the red X button to exit an application (as opposed to triggering some other event which runs code which sends the Close() message to the form? I know I can use the CloseReason property of FormClosingEventArgs but that shows UserClosing whether they click the Red X or my Close button (which sends Close() to the form). Is there any way to determine that it was the red X button they clicked?
You'll have to create a class that inherits from the Form class and override WndProc like this: Protected Overrides Sub WndProc(ByRef m As Message) If ((m.Msg = WM_SYSCOMMAND) AndAlso _ (m.WParam.ToInt32 = SC_CLOSE)) Then 'Here is where you perform some action. End If MyBase.WndProc(m) End Sub 'WndProc where: Public Const WM_SYSCOMMAND As Integer = &H112 Public Const SC_CLOSE As Integer = &HF060 'The Close menu item. Tony "ssg31415926" wrote: > Is there anyway to detect when the user has clicked the red X button > to exit an application (as opposed to triggering some other event > which runs code which sends the Close() message to the form? > > I know I can use the CloseReason property of FormClosingEventArgs but > that shows UserClosing whether they click the Red X or my Close button > (which sends Close() to the form). > > Is there any way to determine that it was the red X button they > clicked? > >
"ssg31415926" wrote in message news:1187781816.819663.40430@e9g2000prf.googlegroups.com... > Is there any way to determine that it was the red X button they > clicked? There's no red X button on ANY of my windows.... For reference, this is called the Close button.
On 22 Aug, 14:02, tlkerns wrote: > You'll have to create a class that inherits from the Form class and override > WndProc like this: > > Protected Overrides Sub WndProc(ByRef m As Message) > If ((m.Msg = WM_SYSCOMMAND) AndAlso _ > (m.WParam.ToInt32 = SC_CLOSE)) Then > > 'Here is where you perform some action. > End If > > MyBase.WndProc(m) > End Sub 'WndProc > > where: > > Public Const WM_SYSCOMMAND As Integer = &H112 > Public Const SC_CLOSE As Integer = &HF060 'The Close menu item. > > Tony > > "ssg31415926" wrote: > > Is there anyway to detect when the user has clicked the red X button > > to exit an application (as opposed to triggering some other event > > which runs code which sends the Close() message to the form? > > > I know I can use the CloseReason property of FormClosingEventArgs but > > that shows UserClosing whether they click the Red X or my Close button > > (which sends Close() to the form). > > > Is there any way to determine that it was the red X button they > > clicked? Thanks very much for your help.