winAPI hello world won't quit !

OK 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??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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,
http://www.winprog.org/tutorial/simple_window.html

and it worked correctly... :(
Remove the "break" statement after WM_CLOSE so that it falls through to PostQuitMessage()
Topic archived. No new replies allowed.