catch user clicking on the X button

  • Thread starter Thread starter Stephan Steiner
  • Start date Start date
S

Stephan Steiner

Hi

I need a special variant of catching the OnClosing event. Basically I want
OnClosing to not close the form if the X button has been pressed, but to
minimize it. So far, no problem. The problem is that I don't know a way to
figure out if the event is thrown because the user pressed the X button in
the upper right, or because he pressed Alt-F4, or because the application is
supposed to shut down because the user logs out. In the latter two cases,
I'm effectively preventing proper operation with my code now.

So, I need:
X button: minimize and hide form
Alt-F4 / User logoff /restarting PC: close application

I've already tried registering an eventhandler for the Closing event and
have a look at the sender, but sender is always my form so that didn't do me
any good. And the CancelEventArgs don't contain any information that would
be of use here, either.

Does anybody have an idea of how I can achieve this?

Regards
Stephan
 
If your willing to get dirty you can override the WndProc and watch for
WM_NCLBUTTONDOWN to see when the user presses the left mouse button in the
non-client area. The X close button is in the non-client area. So if you get
a closing event occur straight after that windows message you know it must
be a user action. Well, almost certainly anyway!

Phil Wright
Follow my C# microISV startup at...
http://componentfactory.blogspot.com
 
Phil
If your willing to get dirty you can override the WndProc and watch for
WM_NCLBUTTONDOWN to see when the user presses the left mouse button in the
non-client area.

Is there no way to specifically find out button X in outside the client area
has been pressen, and perhaps add an eventhandler to it?

Stephan
 
Back
Top