Understanding the Message Queue

I'm still trying to wrap my mind around how this message queue system works for windows.

Lets say I created this initial window using createwindowex() before starting the message loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	hwnd = CreateWindowEx(
		0,
		g_szClassName,
		"theForger's Tutorial Application",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 480, 320,
		NULL, NULL, hInstance, NULL);


	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}


My understanding is the createwindowex() function creates the window then sends the WM_CREATE message to the message queue to be retrieved and processed by the GetMessage(), TranslateMessage() and DispatchMessage() functions which send results to the windows procedure set up in the windows class structure you define. I'm pretty sure this is how it works but my point of confusion is how the windows procedure keeps from getting stuck in a recursive loop when the WM_CREATE message is sent to it when a createwindow() or createwindowex() function is called inside of a WM_CREATE message being handled; case WM_CREATE:.

A small portion of the windows procedure callback to illustrate what I mean:

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

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			HFONT hfDefault;
			HWND hEdit;

			hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
				WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
				0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
			if(hEdit == NULL)
				MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
		}
		break;


When the switch receives a WM_CREATE message the procedure enters case WM_CREATE: which sets up an edit control using createwindowex()... but doesn't that in turn create another WM_CREATE message to be added to the windows thread message queue?

There is something I'm not thinking about in the right way and I've been struggling trying to understand this.

Thanks for any help :)


PS. If you want a full program, which is from the winforgers tutorial, I can provide it.
Last edited on
Controls have window procedures provided by windows. They communicate with their parent window via notifications sent through WM_COMMAND and WM_NOTIFY messages.
That makes sense.

Thank you.
Registered users can post here. Sign in or register to post.