Program doesn't close.

This is a discussion on Program doesn't close. within the Windows Programming forums, part of the Platform Specific Boards category; I'm just beginning in windows programming, so I'm sorry if this is a dumb question (or if I missed seeing ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Program doesn't close.

    I'm just beginning in windows programming, so I'm sorry if this is a dumb question (or if I missed seeing a post about it). I made a program that appears to work like I thought it would, but after closing it, it is still running in the background. I press Ctrl+Alt+Delete and it is still there. I made a few other programs, as well as copied some out of the book I am reading, and they don't do this. I haven't been able to figure out why this does.

    Here is the code:

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine,
    				   int iShowCmd)
    {
    	WNDCLASS wndclass;
    	static char szAppName[] = "Hello world";
    	MSG msg;
    	HWND hwnd;
    
    	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc	= WndProc;
    	wndclass.cbClsExtra		= 0;
    	wndclass.cbWndExtra		= 0;
    	wndclass.hInstance		= hInstance;
    	wndclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wndclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
    	wndclass.hbrBackground	= (HBRUSH) CreateHatchBrush (HS_DIAGCROSS, RGB(24, 155, 10));
    	wndclass.lpszMenuName	= NULL;
    	wndclass.lpszClassName	= szAppName;
    	
    	if ( !RegisterClass(&wndclass) )
    	{
    		MessageBox(NULL, "Could not register window class", szAppName, MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd = CreateWindow(szAppName,
    						"Hello",
    						WS_OVERLAPPEDWINDOW,
    						CW_USEDEFAULT,
    						CW_USEDEFAULT,
    						CW_USEDEFAULT,
    						CW_USEDEFAULT,
    						NULL,
    						NULL,
    						hInstance, NULL);
    
    	ShowWindow(hwnd, iShowCmd);
    	UpdateWindow(hwnd);
    
    	while ( GetMessage(&msg, hwnd, 0, 0) )
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,
    						 LPARAM lParam)
    {
    	HDC hdc;
    	PAINTSTRUCT ps;
    	static int cxClient, cyClient, color;
    
    	switch (message)
    	{
    		case WM_SIZE:
    			cxClient = LOWORD(lParam);
    			cyClient = HIWORD(lParam);
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    
    			SetBkMode (hdc, TRANSPARENT);
    			TextOut (hdc, cxClient/2, cyClient/2, "Hello", strlen("Hello"));
    
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    
    		default:
    			break;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

    I'm sorry about posting the entire thing, but I'm not sure where the problem is (if it is the code). I'm using VC++ 6 and Windows 98, if that helps. If anyone could explain to me what is going on, I would really appreciate it. Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay thats an easy fix!

    You take care of the WM_DESTROY message but you do not take care of the WM_CLOSE message which is sent when you hit the X on your application. Try doing this...

    Code:
    case WM_CLOSE:
    case WM_DESTROY:
      PostQuitMessage( 0 );
      return 0;
    That way when either message is encountered the program will exit correctly. Enjoy.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    It works.

    Thanks a lot. I really should have caught that though. I'd like to blame it on this horrible cold I have, and staying up for 48 hours straight...but to be honest, I have no cold and slept rather well last night.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  2. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 02:16 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21