Valhalla Legends Forums Archive | C/C++ Programming | [Solved] How to go about this...

AuthorMessageTime
UserLoser
I have a class named Dialog and AboutDialog.  Dialog calls CreateDialogParam, has HandleMessage (DLGPROC, basically) in it, etc, AboutDialog inherits from Dialog.  Now, somewhere in my program I'm creating a new Dialog class.

[code]
LRESULT SystemTrayWindow::HandleTrayMessage(LPARAM lParam)
{
...
case WM_LBUTTONDBLCLK:
AboutDialog *About;
About = new AboutDialog(m_hInstance, ABOUTDIALOG);
About->MakeDialog();
return 0;
...
[/code]

Now, obviously the about dialog will be closed, which'll make the program do the following in it's HandleMessage:

[code]
BOOL AboutDialog::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
...
    case WM_CLOSE:
        DestroyWindow(m_hWnd);
        return FALSE;
...
}
[/code]

Now how can I delete that AboutDialog that I created?  Obviously I could do delete About; but how do I know when WM_DESTROY was sent so it's ok to delete it?  In my message loop, I tried catching WM_DESTROY but that doesn't work at all because the message is never WM_DESTROY:

[code]
BOOL WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
...
while(!StopLoop) {
if(MsgWaitForMultipleObjectsEx(0, 0, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE) == WAIT_OBJECT_0) {
while(PeekMessage(&Msg, 0, 0, 0, PM_REMOVE)) {
if(Msg.message == WM_QUIT)
StopLoop = true;
if(Msg.message == WM_DESTROY) {
Dialog *ToDelete = reinterpret_cast<Dialog*>(GetWindowLongPtr(Msg.hwnd, GWLP_USERDATA));
delete ToDelete;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);

}
}
}
...
[/code]
July 13, 2006, 6:00 AM
UserLoser
[code]
BOOL AboutDialog::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostMessage(NULL, WM_DESTROY, 0, 0);
return FALSE;
...
[/code]

In ProfileLauncher.cpp:

[code]
...
while(!StopLoop) {
if(MsgWaitForMultipleObjectsEx(0, 0, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE) == WAIT_OBJECT_0) {
while(PeekMessage(&Msg, 0, 0, 0, PM_REMOVE)) {
if(Msg.message == WM_QUIT)
StopLoop = true;
if(Msg.message == WM_DESTROY) {
Dialog *ToDelete = reinterpret_cast<Dialog*>(GetWindowLongPtr(Msg.hwnd, GWLP_USERDATA));
delete ToDelete;
}
...
[/code]

Special thanks: MyndFyre
July 13, 2006, 6:32 AM
Myndfyr
Note that I advocated that the call be:

[code]
PostMessage(NULL, WM_DESTROY, m_hWnd, 0);
[/code]

...and the processing of the message be...
[code]
if (Msg.message == WM_DESTROY)
{
    if ((HWND)Msg.wParam == m_aboutDlgHwnd)
    {
        delete m_aboutDialog;
        m_aboutDlgHwnd = NULL;
    }
}
[/code]
July 13, 2006, 8:56 AM
UserLoser
Another, more efficient (probably), less messy, alternative:

[code]
case WM_NCDESTROY:
  lres = DefWindowProc(m_hwnd, uMsg, wParam, lParam);
  SetWindowLongPtr(m_hwnd, GWLP_USERDATA, 0);
  delete this;
  return lres;
[/code]
July 14, 2006, 5:41 AM

Search