1

The default way of handling the lifetime of windows in Windows is listening for a WM_CLOSE message, then deciding whether to close the window or not and optionally processing the WM_DESTROY message (for cleanup etc.) afterwards.

Is there any scenario where WM_DESTROY could be sent without WM_CLOSE beforehand? I mean "by the system", not by manually calling DestroyWindow() or PostMessage(). Or is it safe to rely on WM_CLOSE always being sent before WM_DESTROY?

csk
  • 313
  • 2
  • 11
  • `WM_DESTROY` this is notify about that window is being destroyed. and you already can not cancel this process. you need simply free resources associated with window. and this is absolute not related to `WM_CLOSE` - request to destroy window, which you can process or cancel . for what you assume any relationship bettwen wm_close and wm_destroy ? – RbMm Jul 25 '17 at 21:14
  • The relationship is that processing the WM_CLOSE message via `DefWindowProc()` leads to destroying the window and thus sending the WM_DESTROY message. This is the only scenario I know where the window gets destroyed in an _implicit way_, but could there be any other scenario WM_DESTROY will be sent not explicitly by the programmer but by the API itself? – csk Jul 25 '17 at 21:28
  • `WM_DESTROY` send to your window when somebody call `DestroyWindow` or parent of your window is destroyed. but for what you need assume any relationship ? – RbMm Jul 25 '17 at 21:34
  • I was just wondering if handling WM_DESTROY is necessary at all, if you could do all cleanup stuff in WM_CLOSE, too. Provided that you won't call `DestroyWindow()` from any other place, of course... – csk Jul 25 '17 at 21:48
  • 1
    if you need cleanup - you need do this exactly at `WM_DESTROY` or on `WM_NCDESTROY`. never do this at `WM_CLOSE`. this absolute different messages by sense - `WM_CLOSE` is request to destroy window, when `WM_[NC]DESTROY` is notification about that window is being destroyed – RbMm Jul 25 '17 at 22:16

2 Answers2

1

Yes. If you create a MFC dialog based application and press "Cancel" button (IDCANCEL as the ID), it doesn't send WM_CLOSE event. Also if you press "Esc" key, same thing.

If you click "X" button on the upper-right corner of the dialog box, yes WM_CLOSE will be sent, though.

JazzSoft
  • 392
  • 2
  • 11
0

In the WM_COMMAND section of a Dialogex message loop it's possible to add a case for WM_DESTROY. This case captures an Escape keydown by default, so by adding an accompanying EndDialog statement in the VK_ESCAPE condition, we can then initiate a PostQuitMessage.

Edit: There's a clearer explanation here.

Laurie Stearn
  • 959
  • 1
  • 13
  • 34