catch user clicking on the X button

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
 
P

Phil Wright

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
 
S

Stephan Steiner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top