form.closing

  • Thread starter Thread starter Mickey Swanson
  • Start date Start date
M

Mickey Swanson

Is there any way to determing why a form is closing? As in if the user
cliced the x button or if a system shutdown has started.

thanks for any help
Mickey Swanson
 
if a user clicks the x button I need to hide and minimize the form but I
can't just place this in the form.closing event because it will prevent
windows from shutting down. So I need to find out what is closing the form,
either a user or the operating system.

does that help describe what i'm looking for?
Mick
 
Hi Mickey,

No unforutnately not at least just using code in the Closing() event
handler. However, you can use WndProc to check for the system shutdown then
set a flag that the system is trying to shut down the app.

Something like this:

protected override void WndProc(ref Message m)
{
if (this.bNotifyIcon)
{
const int WM_QUERYENDSESSION = 0x011;
if (m.Msg == WM_QUERYENDSESSION)
this.bWindowsShuttingDown = true;
}
base.WndProc(ref m);
}

In this case I use to trap the ShutDown event when the app is running in
NotifyIcon mode because usually when you shut down the form in that mode you
don't want to shut it down but minimize to the tray. The shut down message
however should need to be handled.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/webblog/
 
I've never used the WndProc. do you know where I can look at a more indepth
code sample.
Mick
 
I think I understand. This is what I came up with.

protected override void WndProc(ref Message m)
{
const int WM_QUERYENDSESSION = 0x011;
switch(m.Msg)
{
case WM_QUERYENDSESSION:
this.bWindowsShuttingDown = true;
break;
}
base.WndProc(ref m);
}



private void frmMDI_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(this.bWindowsShuttingDown)
{
Application.Exit();
}
else
{
e.Cancel = true;
this.Hide();
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
}
 
Hi Mickey,

Yes, I think your code is correct. But I want to add more information to
you :) (For your original question about few information about WndProc)
WndProc encapsulated the win32 windows procedure. So you can override it
and handle certain message to do more indepth things.(When normal .Net
event does not meet your need)
To find the value of these message constant, you can look it up in the API
viewer followed with VB6.0.
To understand the message mechanism of windows application, I think the
best resource is some win32 SDK programming book. Such as "Programming
Windows" by Charles Petzold.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks, that helps alot.
I needed the tip on where to look up the constant values.
I also sent this to you directly by mistake. MS shouldn't put the Reply and
Reply Group button so close together. :-)
thanks,
Mick
 
Hi Mickey,

Thanks for your feedback.
I am glad my reply makes sense to you.
Beside the API viewer, there are some windows const that was not normal
used. They are defined in the *.h files in the C:\Program Files\Microsoft
Visual Studio\VC98\Include\ directory. So you can use the Edit|Find and
Replace|Find in Files menu in VS.net IDE.

Merry Christmas!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top