Reputation: 79299
I'm handling ESC
key in my application and when this key is received I wish to close the current window.
Should I simply call DestroyWindow(hWnd)
or should I SendMessage(WM_CLOSE, hWnd, 0, 0)
, or should I be closing the current window in some different way?
Upvotes: 18
Views: 19632
Reputation: 2748
It is up to you which you use. Should the Esc key act just like clicking the close button, or should it definitely destroy the window?
The default implementation of WM_CLOSE
(as found in DefWindowProc
) calls DestroyWindow
, so if you're not handling WM_CLOSE
specifically then one is as good as another. But WM_CLOSE
doesn't necessarily have to call DestroyWindow
, though, so if the window in question handles it then it could do something else. For example, it could pop up a "Are you sure?"-type message box, or simply do nothing. DestroyWindow
will bypass all of that.
Upvotes: 2
Reputation: 125620
You should PostMessage(hWnd, WM_CLOSE, 0, 0)
. It puts the WM_CLOSE
message into the window's message queue for processing, and the window can close properly as the message queue is cleared.
You should use PostMessage
instead of SendMessage
. The difference is that PostMessage
simply puts the message into the message queue and returns; SendMessage
waits for a response from the window, and you don't need to do that in the case of WM_CLOSE
.
Upvotes: 27