Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi, I have a "Really quit?" dialog that pops up when the user presses the Close button on my application.

However, I _only_ want this dialog to appear when the user presses the close button or the ESC key, not when an external source (e.g. Windows Shutdown or the Uninstall tool) sends the WM_CLOSE message. I.e. it should close immediately on shutdown / logoff / when uninstalling and all other external cases.

What would be the correct way to check for this?
Posted
Updated 6-Jun-12 14:31pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-12 20:28pm    
Aha! You are going to close your application by a message from another process! It does not look like a good design...
--SA

you can spot the system wanting to shutdown with the System Shutdown messages[^]

As far as uninstall is concerned, it's probably easiest to create a Custom Action[^] in your installer that only occurs at REMOVE which sends a registered message to your application
 
Share this answer
 
Comments
Mohibur Rashid 6-Jun-12 22:11pm    
Very useful
If you really want to do it through direct handling of WM_CLOSE and then cancel closing, look at the message description:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632617%28v=vs.85%29.aspx[^].

As you can see, the message does not have parameters except message ID, so you don't have any information on the cause of the message.

If you wanted to prevent closing unconditionally, you would need in WM_CLOSE message handler, you would need to return TRUE from WinProc, without calling the DefWindowProc function, but if you need to do it on the condition related to the source of the message, you don't receive anything which gives you a clue.

You should do some work-around. Insider the application, devise a custom message like "WM_USER_CLOSING", send it inside the application process, handle it, and actually close the window or not, depending on the user's decision. It won't affect a handling of the message WM_CLOSE sent from external application, just as you wanted. But — please see my comment to the question; probably you can create better application design instead.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900