Thread: Creating a Window

  1. #1
    Banned
    Join Date
    Jun 2005
    Posts
    594

    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.
    Last edited by ILoveVectors; 07-13-2005 at 08:51 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    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?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a child window in a parent window
    By vopo in forum Windows Programming
    Replies: 8
    Last Post: 10-06-2007, 04:15 PM
  2. Creating a window causes a trackbar to stop working?
    By kidburla in forum Windows Programming
    Replies: 2
    Last Post: 09-20-2007, 05:44 AM
  3. help with creating window
    By Darkinyuasha1 in forum Windows Programming
    Replies: 6
    Last Post: 06-12-2007, 06:51 PM
  4. Creating a window through menu
    By Homunculus in forum Windows Programming
    Replies: 17
    Last Post: 02-20-2006, 06:56 PM
  5. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 02:10 PM