Click here to Skip to main content
11,412,516 members (74,459 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ Win32
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 6-Jun-12 15:18pm
Edited 6-Jun-12 15:31pm
SAKryukov at 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...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
Mohibur Rashid at 6-Jun-12 22:11pm
Very useful
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you really want to do it through direct handling of WM_CLOSE and then cancel closing, look at the message description:[^].

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.


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

  Print Answers RSS
0 OriginalGriff 414
1 Sascha Lefévre 200
2 Maciej Los 155
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,247
2 Maciej Los 3,525
3 Abhinav S 3,248
4 Peter Leow 3,059

Advertise | Privacy | Mobile
Web04 | 2.8.150427.1 | Last Updated 6 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100