How do I create multiple windows to display different text.

Good Day Good Men and Women,

I was tasked with creating a main window and three other windows. The main window is kind of like a trigger to create the three other windows.
The first window should show the time.
The second window shows the result of a calculation.
The third shows the result or another calculation but using the FPU instruction set.
These three windows should loop and update the screen with the results, but really the only visible change would be the first window showing the time.

Now the problem I am having is that, the time shows on each window, despite using a separate SetWindowText() function for each window.

I Have already made some progress, the code is long but i will paste it all. I never know what the Guru's really need to look at in their investigation.

I used alot of code from a stack over solution

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
///////////////// Function Prototypes ////////////////////
HWND wnd1TextBox, wnd2TextBox, wnd3TextBox;

/////////////// Global Variables: //////////////////////////////
bool openMainWnd, openchildWnd1, openchildWnd2, openchildWnd3 = false;
bool windowclass1registeredbefore, windowclass2registeredbefore, windowclass3registeredbefore, windowclass4registeredbefore = false;

enum windowtoopenenumt {none, ChildWindow1, ChildWindow2, ChildWindow3};

windowtoopenenumt windowtoopenenum=none;


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nShowCmd )
{
	bool endprogram = false;	  
    WNDCLASSEXW childCls1;   
    WNDCLASSEXW childCls2;
    WNDCLASSEXW childCls3;
    
	HWND mainWndHnd;
    HWND childWndHnd1;
    HWND childWndHnd2;
    HWND childWndHnd3;

	// Creating Parent Window
	MSG msg;
	WNDCLASSEXW mainWnd;
	mainWnd.cbSize			;
	mainWnd.style													 
	mainWnd.hbrBackground 	
	mainWnd.hCursor 												 
	mainWnd.hInstance 																 
	mainWnd.lpszClassName 												 	
	mainWnd.lpszMenuName 	
	mainWnd.lpfnWndProc 	 												e	
	mainWnd.cbWndExtra 		= 0;
	mainWnd.cbClsExtra		= 0;
	mainWnd.hIcon			
	mainWnd.hIconSm			
	
	if(!RegisterClassExW(&mainWnd))
    {
    	int nResult=GetLastError();
        MessageBox(NULL, "Window class creation failed", "Window Class Failed", MB_ICONERROR);
        //return 0;
    }
	
	 
	RegisterDialogClass(hInst);				
	
	
	mainWndHnd = CreateWindowExW("Main Window); // removed to meet 9000 characters requirements
	
	
	if(!mainWndHnd)
    {
        int nResult=GetLastError();

        MessageBox(NULL, "Window creation failed", "Window Creation Failed", MB_ICONERROR);
    }
   	
	
	ShowWindow(mainWndHnd,nShowCmd);
    bool endloop=false;
    
    
    while (endloop == false) 
	{
        if (GetMessage(&msg,NULL,0,0));
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if (windowtoopenenum != none) 
		{
            switch (windowtoopenenum) 
			{
                case ChildWindow1:
                	// Open first windows
                    if (openchildWnd1 == false) 
					{                                                 
                    	createChildWindow1(childCls1,childWndHnd1,hInst,nShowCmd);
                    	SetWindowText(wnd1TextBox, loopStartTime());
                       	cout << "child window 1 created" << endl;
                    
                    	// Open Second windows
                    	if (openchildWnd2 == false) 
						{   
	                       	createChildWindow2(childCls2,childWndHnd2,hInst,nShowCmd);
	                       	SetWindowText(wnd2TextBox, integerCal());
	                       	cout << "child window 2 created" << endl;
	                       	
	                       		// Open third windows
	                       		if (openchildWnd3 == false) 
								{   
									//windowtoopenenum = ChildWindow3;
			                       	createChildWindow3(childCls3,childWndHnd3,hInst,nShowCmd);
			                       	SetWindowText(wnd3TextBox, integerCal());
			                    	cout << "child window 3 created" << endl;
			                    }
			                    else
								{
									SetWindowText(wnd3TextBox, integerCal());
								UpdateWindow(childWndHnd3);
								}
	                    }
	                    else
						{
							SetWindowText(wnd3TextBox, integerCal());
							UpdateWindow(childWndHnd2);
						}
                    	
                    }
                    else			// if window is already open, then just update
					{
						SetWindowText(wnd1TextBox, loopStartTime());
						UpdateWindow(childWndHnd1);
					}
		
                    break;
						

                    }
                    break;
                    
                case ChildWindow3:               
                    if (openchildWnd3==false) 
					{               
                        createChildWindow3(childCls3,childWndHnd3,hInst,nShowCmd);
                        cout << "child window 2 created" << endl;
                    }
                    break;
                    
            }
        	
			windowtoopenenum = none;
    	}
		if (openMainWnd==false && openchildWnd1==false && openchildWnd2==false && openchildWnd3==false )
        	endloop=true;

    } // end while loop
    
    MessageBox(NULL, "All Windows are closed. Program will now close.", "Message", MB_ICONINFORMATION);
    
    return 0;
}



void createChildWindow1(WNDCLASSEXW& wc,HWND& hwnd,HINSTANCE hInst,int nShowCmd)
{
	MSG Msg;
	
	if (windowclass1registeredbefore == false) 
	{
		// Creating Parent Window
	MSG msg;
	WNDCLASSEXW wc;
	wc.cbSize			;
	wc.style													 
	wc.hbrBackground 	
	wc.hCursor 												 
	wc.hInstance 																 
	wc.lpszClassName 												 	
	wc.lpszMenuName 	
	wc.lpfnWndProc 	 												e	
	wc.cbWndExtra 		= 0;
	wc.cbClsExtra		= 0;
	wc.hIcon			
	wc.hIconSm	

		if(!RegisterClassExW(&wc))
		{
			int nResult=GetLastError();
			MessageBox();
		}
		else
			windowclass1registeredbefore=true;
	} 
		
	CreateWindowExW(Wnd 1" ); // removed to meet 9000 characters requirements

    if(!hwnd)
    {
        int nResult=GetLastError();

        MessageBox();
    }
	
	ShowWindow(hwnd,nShowCmd);
	
}


void createChildWindow2()
{
	// Basically the same code as in createChildWindow1, but replace all 1's with 2
}

void createChildWindow3()
{
	// Basically the same code as in createChildWindow1, but replace all 1's with 3
}


//////////////////////////////////////////////////// WINDOW PROCEDURES ////////////////////////////////////////////////

LRESULT CALLBACK WindowProcedureMain(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg)
	{

		case WM_COMMAND:
			switch(wp)
			{
				case IDA_MENU_EXIT:
					DestroyWindow(hWnd);		
					break;
			
				case IDA_MENU_ABOUT:
					displayDialog(hWnd);
					break;
				
				case ID_OPEN_WINDOWS_BUTTON1:
					windowtoopenenum = ChildWindow1;								
					break;
					
				case ID_OPEN_WINDOWS_BUTTON2:
						windowtoopenenum = ChildWindow2;			
					break;

			 
			} // end inner switch
	        
		break;
		

		case WM_CREATE:	
				openMainWnd = true;
				CreateWindowW(L"Part 1");		// removed to meet 9000 characters requirements	
				CreateWindowW(L"Part 2"); 		 // removed to meet 9000 characters requirements		
				addMenu(hWnd);				
				break;
			
		// Sent when a window is being destroyed.
		case WM_DESTROY:
			openMainWnd = false;
			PostQuitMessage(0);
			break;
			
		default:
		
		return DefWindowProcW(hWnd,msg,wp,lp);
	
	} // end outer switch

} // end 


LRESULT CALLBACK WindowProcedureChild1(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ 
	 
	 switch(msg) {
        case WM_CREATE:
            openchildWnd1 = true;
            addMenu(hwnd);
			wnd1TextBox = CreateWindowW(); 		 // removed to meet 9000 characters requirements
            break;
            
        case WM_DESTROY:
            openchildWnd1 = false;
            PostQuitMessage(0);
            break;
            
                
        case WM_COMMAND:
            switch LOWORD(wParam) 
			{
                case IDA_MENU_EXIT:
					DestroyWindow(hwnd);		
					break;
			
				case IDA_MENU_ABOUT:
					displayDialog(hwnd);
					break;
            }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK WindowProcedureChild2(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ 	
		 // Basically the same code as in WindowProcedureChild1, but replace all 1's with 3  
}

LRESULT CALLBACK WindowProcedureChild3(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
             // Basically the same code as in WindowProcedureChild1, but replace all 1's with 3   
}


Thanks for helping I appreciate it.
why don't your create the other windows as children of the main window by specifying WM_CHILD flag as follow
1
2
HWND window1 = CreateWindow(L"static", L"", WS_CHILD | WM_VISIBLE, posX, posY, 
    width, height, mainWindow, nullptr, nullptr, nullptr);


In the app loop yo ucan do something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (WM_QUIT != msg.message)
{
    if (PeekMessage())
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else
    {
        updateWindow(window1, deltaTime);
        updateWindow(window2, deltaTime);
        updateWindow(window3, deltaTime);
    }
}


updateWindow would look something like this
1
2
3
4
5
6
7
void updateWindow(HWND hWnd, float dt)
{
    CHAR time[32];
    // . . .

    SetWindowText(hWnd, time);
}
Last edited on
I'm gonna try this now... if it works I'm SCREAM!!!... Thanks much though for replying.. I'll Let you know if it works
So I tried it, not sure how the windows should come up though.

Can you please guide me on this?

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
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nShowCmd)
{
	HWND mainWndHnd;
	HWND childWndHnd1{};
	HWND childWndHnd2{};
	HWND childWndHnd3{};

	// Creating Parent Window
	MSG msg = {};
	WNDCLASSEXW mainWnd;
	mainWnd.cbSize = sizeof(WNDCLASSEX);
	mainWnd.style = CS_HREDRAW | CS_VREDRAW;;
	//wc.hbrBackground 		= (HBRUSH)(COLOR_WINDOW+1) ;										 // A handle to the class background brush
	mainWnd.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
	mainWnd.hCursor = LoadCursor(NULL, IDC_ARROW); 										 // A handle to the class cursor. This member must be a handle to a cursor resource. 
	mainWnd.hInstance = hInst;															 // A handle to the instance that contains the window procedure for the class.
	mainWnd.lpszClassName = L"Main Window";												 	// A pointer to a null-terminated string or is an atom
	mainWnd.lpszMenuName = MAKEINTRESOURCEW(IDR_SimpleMENU);
	mainWnd.lpfnWndProc = (WNDPROC)WindowProcedureMain; 												 // A pointer to the window procedure	
	mainWnd.cbWndExtra = 0;
	mainWnd.cbClsExtra = 0;
	mainWnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	mainWnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if (!RegisterClassExW(&mainWnd))
	{
		int nResult = GetLastError();
		MessageBox(NULL, L"Window class creation failed", L"Window Class Failed", MB_ICONERROR);
		//MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		//return 0;
	}


	RegisterDialogClass(hInst);														 // Registers any nonstandard window classes required by a screen saver's configuration dialog box.

	
	mainWndHnd = CreateWindowExW(WS_EX_CLIENTEDGE, mainWnd.lpszClassName, L"Main Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 2, 5, 700, 400, NULL, NULL, hInst, NULL);


	if (!mainWndHnd)
	{
		int nResult = GetLastError();

		MessageBox(NULL, L"Window creation failed", L"Window Creation Failed", MB_ICONERROR);
	}


	childWndHnd1 = CreateWindowExW(WS_EX_CLIENTEDGE, L"Child WIndow 1", L"Win 1", WS_VISIBLE | WS_CHILD, 800, 5, 700, 400, mainWndHnd, NULL, hInst, NULL);
	childWndHnd2 = CreateWindowExW(WS_EX_CLIENTEDGE, L"Child WIndow 2", L"Win 2", WS_VISIBLE | WS_CHILD, 2, 450, 700, 400, mainWndHnd, NULL, hInst, NULL);
	childWndHnd3 = CreateWindowExW(WS_EX_CLIENTEDGE, L"Child WIndow 3", L"Win 3", WS_VISIBLE | WS_CHILD, 800, 450, 700, 400, mainWndHnd, NULL, hInst, NULL);

	ShowWindow(mainWndHnd, nShowCmd);
	bool endloop = false;

	while (WM_QUIT != msg.message)
	{
		if (PeekMessage(&msg, mainWndHnd, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			updateWindow(childWndHnd1, loopStartTime());
			updateWindow(childWndHnd2, integerCal());
			updateWindow(childWndHnd3, loopStartTime());
		}
	}

	return 0;
}
Topic archived. No new replies allowed.