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.
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_CLOSEis not handled,DefWindowProcdestroys the window by default. - When the window is being destroyed, it receives
WM_DESTROY. - In the main window, handle
WM_DESTROYby callingPostQuitMessage(0), which postsWM_QUITand 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.