Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3044

How to close the main application by clicking on the console?

$
0
0
I spawned a console from the main application as a log window.
But when I clicked on the x of the main app, the application just won't stop execution.
When I clicked on the x of the console, it does stop, but memory leak remains.
I rechecked and discovered the destructor of the main application never got called
when I clicked on the close button of the console window.
how do I make sure I call on that no matter which window I close from?

I open a console like this

Code:

#include "CMyApp.h"
#include <crtdbg.h>

FILE* consoleWin = NULL;
 

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
        // check for memory leak
        int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
        flag |= _CRTDBG_LEAK_CHECK_DF;
        _CrtSetDbgFlag(flag);

       
        AllocConsole();
        consoleWin = freopen("CONOUT$", "w", stdout);
       

        CMyApp SimApp;       
       
        if (!SimApp.Create(hInst))
                return false;

        if (!SimApp.Run())
                return false;
       
        fclose(consoleWin);
       
       
        return 0;
}

LRESULT CALLBACK CSimApplication::WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
        switch (uMsg)
        {
        case WM_CLOSE:               
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        case WM_KEYDOWN:
                switch (wParam)
                {
                case VK_F11: // error on F12???
                        PostQuitMessage(0);
                        break;
                }
                break;
        case WM_SIZE:
                // Check to see if we are losing our window...
                if (wParam == SIZE_MAXHIDE || wParam == SIZE_MINIMIZED)
                        CSimApplication::m_pGameApp->SetGameActive(false);
                else
                        CSimApplication::m_pGameApp->SetGameActive(true);
                break;
        case WM_ACTIVATE:
                if (LOWORD(wParam) == WA_INACTIVE)
                        CSimApplication::m_pGameApp->SetGameActive(false);
                else if (LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE)
                        CSimApplication::m_pGameApp->SetGameActive(true);
                break;
        case WM_ENTERSIZEMOVE:
                CSimApplication::m_pGameApp->SetGameActive(false);
                break;
        case WM_EXITSIZEMOVE:
                CSimApplication::m_pGameApp->SetGameActive(true);
                break;
        default:
                return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        return 0;
}

After that, there is a log singleton to hold onto the log messages.
Thanks
Jack

Viewing all articles
Browse latest Browse all 3044

Trending Articles