Trying to change a window title in a loop as it is create

Hi, I am trying to create 5 windows from the same class for a homework assignment. I put in a loop that will loop 5 times and create my 5 windows but I can not figure out how to change the titles of the windows with a variable. I have tried a array of strings but that didn't work. Any help would be greatly appreciated.


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
//Opens a window in Windows
//Michael Whiteley

// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include <windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <stdio.h>     
#include <math.h>

// DEFINES ////////////////////////////////////////////////

// defines for windows 
#define WINDOW_CLASS_NAME "WINCLASS1"

// GLOBALS ////////////////////////////////////////////////
#define WIN_POS_X		0		//opening windows x position
#define WIN_POS_Y		0		//opening windows y position
#define WIN_OFFSET_X	300		//opening windows x offset
#define WIN_OFFSET_Y	100		//opening windows y offset

// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, 
						    UINT msg, 
                            WPARAM wparam, 
                            LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT		ps;		// used in WM_PAINT
HDC				hdc;	// handle to a device context

// what is the message 
switch(msg)
	{	
	case WM_CREATE: 
        {
		// do initialization stuff here

        // return success
		return(0);
		} break;

	case WM_PAINT: 
		{
		// simply validate the window
		hdc = BeginPaint(hwnd,&ps);	 
		// you would do all your painting here
        EndPaint(hwnd,&ps);

        // return success
		return(0);
   		} break;

	case WM_DESTROY: 
		{
		// kill the application, this sends a WM_QUIT message 
		PostQuitMessage(0);

        // return success
		return(0);
		} break;

	default:break;

    } // end switch

// process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain(	HINSTANCE hinstance,
					HINSTANCE hprevinstance,
					LPSTR lpcmdline,
					int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create
HWND	   hwnd;	 // generic window handle
MSG		   msg;		 // generic message

// first fill in the window class stucture
winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style			= CS_DBLCLKS | CS_OWNDC | 
                          CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc	= WindowProc;
winclass.cbClsExtra		= 0;
winclass.cbWndExtra		= 0;
winclass.hInstance		= hinstance;
winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName	= NULL;
winclass.lpszClassName	= WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

// register the window class
if (!RegisterClassEx(&winclass))
	return(0);

for(int i = 0; i < 5; i++)
{
	// create the window
	if (!(hwnd = CreateWindowEx(NULL, // extended style
								WINDOW_CLASS_NAME,   // class
								"Window Title", // title
								WS_OVERLAPPEDWINDOW | WS_VISIBLE,
					 			WIN_POS_X + (i * WIN_OFFSET_X),WIN_POS_Y + (i * WIN_OFFSET_Y),	    // initial x,y
								300,100,  // initial width, height
								NULL,	    // handle to parent 
								NULL,	    // handle to menu
								hinstance,// instance of this application
								NULL)))	// extra creation parms
	{
		return(0);
	}//end if
}

// enter main event loop
while(GetMessage(&msg,NULL,0,0))
 	 { 
	 // translate any accelerator keys
	 TranslateMessage(&msg);

	 // send the message to the window proc
	 DispatchMessage(&msg);
	 } // end while

// return to Windows like this
return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////
Are you trying to put a different title in the TitleBar of the Window, or in the Client area of the window?

If you just want to put a different title in the TitleBar of the window, you've walked a mile to go an inch.

All you have to do is create your string array and then loop through it with this single statement --

1
2
for(int i = 0; i < 5; i++)
   SetWindowText(hwnd, szTitle[i]);


You also have to make sure you get a different window handle each time.
Last edited on
After looking at your code more closely, you need to do this...

Instead of HWND hwnd; you should do HWND hwnd[5];

Then you set your loop and do this...

hwnd[i]=CreateWindowEx(NULL, WINDOW_CLASS_NAME, "",...

In other words, you leave the title blank because right after you create the window you're going to do this...

SetWindowText(hwnd[i], szTitle[i]);

And now you're good to go.
Last edited on
Topic archived. No new replies allowed.