Question in Hello World C++ WinAPI

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
#include <windows.h>

LPCTSTR ClsName = L"BasicApp";
LPCTSTR WndName = L"A Simple Window";

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
			      WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
	MSG        Msg;
	HWND       hWnd;
	WNDCLASSEX WndClsEx;

	// Create the application 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_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.lpszMenuName  = NULL;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	// Register the application
	RegisterClassEx(&WndClsEx);

	// Create the window object
	hWnd = CreateWindow(ClsName,
			  WndName,
			  WS_OVERLAPPEDWINDOW,
			  CW_USEDEFAULT,
			  CW_USEDEFAULT,
			  CW_USEDEFAULT,
			  CW_USEDEFAULT,
			  NULL,
			  NULL,
			  hInstance,
			  NULL);
	
	// Find out if the window was created
	if( !hWnd ) // If the window was not created,
		return 0; // stop the application

	// Display the window to the user
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	// Decode and treat the messages
	// as long as the application is running
	while( GetMessage(&Msg, NULL, 0, 0) )
	{
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
	}

	return Msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
			   WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    // If the user wants to close the application
    case WM_DESTROY:
        // then close it
        PostQuitMessage(WM_QUIT);
        break;
    default:
        // Process the left-over messages
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    // If something was not done, let it go
    return 0;
}


I've some points that I feel lost.Please help.I've written this like 5 times straight.

1.
1
2
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
			      WPARAM wParam, LPARAM lParam);


Why should I care of wParam and lParam...is there any simple demonstration of it ?

2.
1
2
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)


Same question for the 3rd and 4th parameter.

3.
while( GetMessage(&Msg, NULL, 0, 0) )

Why the 2nd parameter should be NULL in order to close the program correctly ?
I wrote HWnd and when I click X (close) button it quitted but the process still exists in task manager.

4.
return Msg.wParam;

Why do I have to return this one ?

5.
return DefWindowProc(hWnd, Msg, wParam, lParam);

Why do I need this,why shouldn't I wrote nothing as a sign of ignorance ?

6.
And also : WndClsEx.cbSize = sizeof(WNDCLASSEX);

I though when I first declared the class it allocated the memory enough for a WNDCLASSEX object ? Why I must specify this line.

I guess that's everything.Thanks in advance.I'm waiting for your useful replies.
Last edited on
R/

1. There are lots of samples. You'll see as you start doing useful things. Here's the WM_KEYDOWN documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280(v=vs.85).aspx .

2. lpCmdLine: Is the command line the program is started. MS Excel can be started in Safe Mode using excel.exe -s. Excel knows the user wants safe mode by examining lpCmdlLine.

You should always respect nCmdShow. When you show your main window, you should use this value in the call to ShowWindow().

3. Read the GetMessage() documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx . It should clear things up.

4. After you read about GetMessage(), read about PostQuitMessage() (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644945(v=vs.85).aspx ). It should then become evident. Post a note here if it is not.

5. The window procedure is in charge of LOTS of things. It is actually kind of a pest: You must write a lot of code just to get simple things done. So Microsoft provided a "stock" behavior for a lot of window messages. This stock behavior is found in this DefWindowProc() function. So just call this function when you want the stock behavior; don't use it if you are programming a special behavior of your own for a particular window message or messages.

BUT: Always call DefWindowProc() if you DON'T process the message yourself. Never leave a message unprocessed.

6. That line is not a memory allocation. You are simply saving the number of bytes of the class in the cbSize member. In order to allocate memory you need to use new or malloc(), and you are not using these.
Thanks for your detailed advices. Thread solved.
Topic archived. No new replies allowed.