-
Creating a Window
Ok i have decent experience with c++, however
it was consoel use, i finally decided that i understood enough
to start a win32 windowed application. So i got a tutorial
and got started, a little into the tutorial everything going
great and it running like i expected, however the problem
comes in when i close the program. The window
disappears when i press the X, but the process remains
in the task manager? Based on the following code
any idea why it doesnt quit running even though
the window disappears?
Code:
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstace, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
MSG Msg;
WNDCLASSEX WndClsEx;
const char ClsName[] = "BasicApp";
const char WndName[] = "A Simple Window!";
WndClsEx.cbSize = (sizeof(WNDCLASSEX));
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_INFORMATION);
WndClsEx.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR1));
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_INFORMATION);
RegisterClassEx(&WndClsEx);
hWnd = CreateWindow(ClsName, WndName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(!hWnd)
{
return 0;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
while( GetMessage(&Msg, hWnd, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
Sleep(1);
}
return 0;
}
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY :
{
PostQuitMessage(WM_QUIT);
break;
}
default :
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
return 0;
}
EDIT : I didn't add the soruce for resource.h because the
problem was there before i added it.
-
Funny thing about GetMessage(), it retuns a BOOL or -1.
http://msdn.microsoft.com/library/de...getmessage.asp
Use the message loop construct shown in that link.
gg
-
Thanks that worked, just out of curiosity, if the if statment
when i check for the error, fi the error comes up,
should i just call GetMessage again, or should i do something else?
-
I've always treated -1 and FALSE the same - the message loop stops.
In your case, GetMessage() was returning -1 once the hWnd parameter was no longer valid. In other words, the window handle became invalid before GetMessage() had a chance to process WM_QUIT (and return FALSE).
Also, the parameter to PostQuitMessage() is the wParam of the WM_QUIT message. PostQuitMessage() will always post a WM_QUIT, regardless of the parameter you give it.
This is the message loop I typically use:
Code:
int MessageLoop(HWND hwnd)
{
MSG msg;
BOOL bRet;
while (1)
{
bRet = GetMessage(&msg, NULL, 0, 0);
if ((bRet == 0) || (bRet == -1))
break;
if (!IsWindow(hwnd) || !IsDialogMessage(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//if
}//while
if (msg.message == WM_QUIT)
return msg.wParam;
return 0;
}//MessageLoop
gg
-
>> if (!IsWindow(hwnd) || !IsDialogMessage(hwnd, &msg))
You can take that out if you don't need "dialog box navigation" or WS_TABSTOP support. Then MessageLoop() wouldn't need the HWND parameter.
gg