Thread: winAPI hello world won't quit !need help to solve it

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    winAPI hello world won't quit !need help to solve it

    I just started learning the Win API,
    this is the first code i wrote by referring some various tutorials i found in the internet...

    i have a problem, the program won't close. when i click the X on the corner the window is destroyed, but the process still keeps running ( i can see it on the Task Manager )...

    how do i make the process end/quit... dosen't WM_QUIT do that??

    Code:
    #include <Windows.h>
    
    // message handler
    LRESULT CALLBACK WndProc(HWND hWindow, UINT iMessage,WPARAM wParam, LPARAM lParam);
    
    
    
    int WINAPI WinMain(
    		HINSTANCE hInstance, // Handle to the current instance
    		HINSTANCE hPrevInst, // Handle to the previous instance
    		LPSTR lpCmdLine,		// Pointer to the command line
    		int nShowCmd			// Shows the state of the window
    		)
    {
    	WNDCLASS kWndClass;	// window object
    
    	// set visual properties of the window
    	kWndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	kWndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	kWndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    
    	// set system properties of the window
    	kWndClass.hInstance = hInstance;
    	kWndClass.lpfnWndProc = (WNDPROC)WndProc;
    	kWndClass.lpszClassName = TEXT("01 Basic Window");
    
    	// set extra properties
    	kWndClass.lpszMenuName = NULL;
    	kWndClass.cbClsExtra = NULL;
    	kWndClass.cbWndExtra = NULL;
    	kWndClass.style = NULL;
    
    	// register the window object
    	if(!RegisterClass(&kWndClass))
    	{
    		MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),
                MB_ICONEXCLAMATION | MB_OK);
    		return -1;
    	}
    
    	HWND hWindow;	// The window
    
    	hWindow = CreateWindow(
    				TEXT("01 Basic Window"), // Registered class name
    				TEXT("A Blank Window"),	// Application window name
    				WS_OVERLAPPEDWINDOW | WS_VISIBLE,	// Window style
    				CW_USEDEFAULT,  // Horizontal position of the window
    				CW_USEDEFAULT,  // Vertical position of the window
    				CW_USEDEFAULT,  // Window width
    				CW_USEDEFAULT,  // Window height
    				NULL,           // Handle to the parent window
    				NULL,           // Handle to the menu the identifier
      			        hInstance,      // Handle to the application instance
    				NULL            // Pointer to the window-creation data
    				);
    
    	// Check if it failed to create the window successfully
    	if(!hWindow)
    		return FALSE;
    
    	ShowWindow(hWindow,nShowCmd);
    	UpdateWindow(hWindow);
    
    	MSG kMessage;	// Message
    
    	// Message Loop
    	while( GetMessage(&kMessage,hWindow,0,0)!= FALSE )
    	{
    		TranslateMessage(&kMessage);
    		DispatchMessage(&kMessage);
    	}
    
    	return kMessage.wParam;
    }
    
    
    LRESULT CALLBACK WndProc(HWND hWindow, UINT iMessage,WPARAM wParam, LPARAM lParam)
    {
    	switch(iMessage)
        {
            case WM_CLOSE:
                DestroyWindow(hWindow);
            break;
    
    	case WM_DESTROY:
                PostQuitMessage(0);
            break;
    
            default:
                return DefWindowProc(hWindow,iMessage,wParam,lParam);
        }
    
        return 0;
    }
    --------------------------------------------------------------------------------------------------------------------------------------
    by the way,
    i am using windows 7 32 bit.

    do i have to use WNDCLASSEX other than WNDCLASS in win 7 ??

    i now tried the code from this site,
    Tutorial: A Simple Window

    and it worked correctly...

    That code is using WNDCLASSEX and relevent "Ex" functions....
    --------------------------------------------------------------------------------------------------------------------------------------
    Last edited by Deamonpog; 05-14-2011 at 06:18 AM. Reason: added to the end some more info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q to quit help
    By macman in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2005, 03:30 PM
  2. I quit!
    By Luigi in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2002, 09:30 AM
  3. Are Your Sure You Want To Quit? Help Please
    By paulroseby in forum C Programming
    Replies: 4
    Last Post: 10-23-2002, 04:34 PM
  4. Last try, then i quit...
    By Silentsharp in forum C++ Programming
    Replies: 15
    Last Post: 04-06-2002, 11:06 PM

Tags for this Thread