Can't catch WM_DESTROY without DispatchMessage

How do you Catch the "WM_DESTROY" message from a user closing a window, without using DispatchMessage?

It seems that if I just read the MSG.message feild, WM_DESTROY is never recieved. Is this because there is a different message that DispatchMessage translates, and if so, how do I catch this through MSG.message?

-Pseudo

PS. Before you say "why not just use DispatchMessage", I'm using this for a Application class, and I see no way I would be able to make DispatchMessage call a member function of the class, so I'm just using my own DispatchMessage function.

Last edited on
In all likelyhood, WM_DESTROY is being sent with SendMessage() and not with PostMessage(), and therefore does not go through the normal message pump. One of the default handlers for another message is probably sending a whole bunch of other messages to properly shut everything down.

IIRC, the first "close me" style message that is sent is WM_SYSCOMMAND with the WPARAM == SC_CLOSE. If you catch that and cut it off so that it doesn't call DefWindowProc(), it prevents the window from closing (I used to do this to stop it from closing if the user chose "cancel" from a "do you want to save" style message box -- if you try to stop WM_DESTROY, it's already to late to prevent the window from closing).

ANYway. So yeah... if you want WM_DESTROY to run through the message pump, then try catching WM_SYSCOMMAND and have it PostMessage WM_DESTROY. Though beware that doing this might lead to possible memory leaks and/or program weirdness, because you're basically disrupting the entire shutdown process.

EDIT -- rather than write your own classes... have you looked into some crossplatform libs? wxWidgets does all this kind of thing already (application class, window management, OOP design, etc), and is portable to several platforms. It's worth looking into. Break away from Win-only design!


EDIT 2 -- apparently WM_SYSCOMMAND is a little funkier than that. Here's a snippit from my old code:

1
2
case WM_SYSCOMMAND:
  if((w & 0xFFF0) == SC_CLOSE)


'w' here is the WPARAM. I have no idea if that's right or not (maybe it's the entire LOWORD and not just &0xFFF0), but it always worked for me.
Last edited on
From MSDN on WM_CLOSE:


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.

By default, the DefWindowProc function calls the DestroyWindow function to destroy the window.


pseudo> I'm using this for a Application class, and I see no way I would be able to make
pseudo> DispatchMessage call a member function of the class

Why not? It could call a static member. What exactly do you mean?


Note: If we're mentioning wxWidgets as an alternative, then Qt should also be mentioned.
Topic archived. No new replies allowed.