Keep Window Open

Sid Kraft 96 Reputation points
2026-08-05T01:21:39.1133333+00:00

Running C++, OpenGL and graphics output with Visual Studio.

Running OpenGL with a graphics window, when I execute, the window immediately closes at the program end before I can see what is happening. How does one keep the window open that has been created until ready to close, Sid Kraft

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


2 answers

Sort by: Most helpful
  1. Sid Kraft 96 Reputation points
    2026-08-05T01:25:12.79+00:00

    Need to re-phrase, using the glutInit function in OpenGL, C++, Sid Kraft

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-08-05T01:21:58.12+00:00

    Keep the app alive by running the Windows message loop until the window is closed. In a Windows desktop app, the program stays in its while loop until the user closes the window and exits the application.

    For closing behavior:

    • When the user clicks Close or presses Alt+F4, the window receives WM_CLOSE.
    • If WM_CLOSE is not handled, DefWindowProc destroys the window by default.
    • When the window is being destroyed, it receives WM_DESTROY.
    • In the main window, handle WM_DESTROY by calling PostQuitMessage(0), which posts WM_QUIT and ends the message loop.

    Typical pattern:

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow)
    {
        // register class, create window, show window...
    
        MSG msg = {};
        while (GetMessage(&msg, nullptr, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return 0;
    }
    

    If confirmation is needed before closing, handle WM_CLOSE and call DestroyWindow(hwnd) only when ready:

    case WM_CLOSE:
        if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK)
        {
            DestroyWindow(hwnd);
        }
        return 0;
    

    For an OpenGL window specifically, create the rendering context during window creation and release it when the window is closed, after making the rendering context not current.


    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.