I've written a simple Windows program that will display a window. However, when I close the window it closes properly but the application is still running in the background.
Code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
bool done = false;
MSG msg;
LPCTSTR className = "ClassName";
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_CROSS);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL, IDI_WARNING);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error Registering Windows Class!", "Error!", MB_OK | MB_ICONERROR);
return 1;
}
HWND hwnd = CreateWindowEx(
0,
className,
"Window Title",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
300, 300,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while(!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
if(msg.message == WM_QUIT)
{
done = true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
Any insight on why this application doesn't close? I thought PostQuitMessage() would take care of this.