How to make multiple windows??

The excersise wants me to create three windows at the same time. How would i do this? Would i just copy and paste the code but replace WndProc with WndProc1, WndProc2, etc? Here is the code I have to make one window.

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

// Store handles to the main window and application
// instance globally.
HWND		ghMainWnd = 0;
HINSTANCE	ghAppInst = 0;

// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	//Handle left mouse button click message.
	case WM_LBUTTONDOWN:
		MessageBoxA(0, "WM_LBUTTONDOWN message", "Msg", MB_OK);
		return 0;
	// Handle key down message.
	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			if(MessageBoxA(0, "Do you want to exit?", "Msg", MB_YESNO) == IDYES)
			{
				DestroyWindow(ghMainWnd);
				return 0;
			} 

	// Handle destroy window message.
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}//	end switch

	// Forward any other messages we diddn;t handle to the
	// defualt window procedure.
	return DefWindowProc(hWnd, msg, wParam, lParam);
}// end WindProc

// WinMain: Entry point for a Windows Application.
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		PSTR cmdLine, int showCmd)
{
	// Save a handle to application instance.
	ghAppInst = hInstance;

	// Step 2: Fill out a WNDCLASS instance.
	WNDCLASS wc;
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= ghAppInst;
	wc.hIcon			= ::LoadIcon(0, IDI_APPLICATION);
	wc.hCursor			= ::LoadCursor(0, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)::GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName		= 0;
	wc.lpszClassName	= L"MyWndClassName";

	// Step 3: Register the WNDCLASS instance with Windows.
	RegisterClass( &wc );

	// Step 4: Create the window, and save handle in globle
	// window handle variable ghMainWnd
	ghMainWnd = ::CreateWindowA("MyWndClassName", "MyWindow",
					WS_VSCROLL | WS_OVERLAPPEDWINDOW | WS_HSCROLL, 0, 0, 500, 500, 0, 0,
					ghAppInst, 0);

	if(ghMainWnd == 0)
	{
		::MessageBoxA(0, "CreateWindow - Failed", 0, 0);
		return false;
	}

	// Step 5: Show and update the window
	ShowWindow(ghMainWnd, showCmd);
	UpdateWindow(ghMainWnd);

	//Step 6: Enter the message loop and don't quit until
	// a WM_QUIT message is received.
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while( GetMessage(&msg, 0, 0, 0) )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	// Return exit code back to operating system.
	return (int)msg.wParam;

}// End WinMain 
You don't need to repeat all of that code. All you have to do is create an array of 3 type HWND and then loop each like this --

1
2
3
4
for(int i = 0; i < 3; i++)
{
     hwnd[i]=CreateWindow(...)
}

Im REALLY new to Windows API programming so im not really sure where to write that for loop.

And also all three windows have to be slightly diffferent(background color, messagebox message) so will your idea still work?
That seems to be an overly ambitious project for someone who is "REALLY new to Windows API programming", no so much because of the three windows themselves, but because changing the background color will require other calls.

Creating the three windows is a breeze because all you have to do is set the loop, as I showed you. Do you see that it is IMMEDIATELY BEFORE the CreateWindow() call? That's exactly how you do it --

HWND hwnd[3];

You declare that at the top of your function. Then you loop through it just as I showed you, i.e., you make 3 calls to CreateWindow() with the int "i" operating as your iterator.

See if you can get that done for starters.

Is this a classroom assignment? Where are these instructions coming from?

Lolno im 14 im reading a book called "C++ Programming for Game Developers Module II".

Module I was all about console programming, and i just started Module II and its teaching Windows Programming now. What i was suppose to do was find the "Win32 Documentation" to teach me stuff but i cant find it :(. The book said it should be in my compilers help documentation but its not there.

Ill try doing what you said right now and if it doesnt work ill come back and tell you

EDIT: When i do what you said nothing happens. I click debug and no windows pop up, and there's no compile errors.
Last edited on
If you want to create multiple windows based on the same window procedure without static varialbes, e.g. separate background colors and so on, then you need to attach a class or a data structure to each instance of the window. You can do this by setting the User data of the window or by allocating extra window bytes for each window when registering the class.

Heres a good example in c++ of how to make an object oriented window procedure:

http://blogs.msdn.com/b/oldnewthing/archive/2005/04/22/410773.aspx
Topic archived. No new replies allowed.