I do not think that you solve this with a single message.
When closing the application, you must also distinguish between mouse action and Alt + F4. If you close the application using Alt + F4, the message looks the same as closing it from the taskbar (see lParam value)
You can see the last message that was received using GetMessage (the last input message). If the message appears on the taskbar, this message is WM_SYSCOMMAND. If the message comes from within, you get WM_SYSCOMMAND as SendMessage.
You can use AfxGetCurrentMessage to determine what was the last input message. If you find WM_SYSCOMMAND here, then the closure comes from the taskbar. If you find here a message with a keyboard or mouse, the message comes from user input.
Tip. Use Spy ++ to learn this behavior.
source share