Question

I just want to know what is the difference between these two messaging constants. Which one should I use in WndProc method when overriding, to handle close button message.

Was it helpful?

Solution

WM_CLOSE is sent as a window message whenever the window is requested to be closed, by any means.

SC_CLOSE is sent as a parameter of a WM_SYSCOMMAND message, when the user presses the Close button (or selects Close from the control menu of the window).

Which one you listen for is determined by which action(s) you attempting to intercept/deal with.

OTHER TIPS

According to msdn, SC_CLOSE is one of wParam values of WM_SYSCOMMAND message.

A window receives this message when the user chooses a command from the Window menu (formerly known as the system or control menu) or when the user chooses the maximize button, minimize button, restore button, or close button.

WM_CLOSE is a message itself.

Sent as a signal that a window or an application should terminate.

So, when window receives WM_SYSCOMMAND with parameter value SC_CLOSE, then you can close window (send WM_CLOSE message).

UPDATE (if you want to let user to close the window gracefully): An application can prompt the user for confirmation, prior to destroying a window, by processing the WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice.

BTW if you use C#, you can handle FormClosing event and do you work there. If you need to cancel closing, then just set e.Cancel = true for event argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top