How to show updated variable to a window?

I have window running that shows one variable. I would like to increase that variable every few seconds and update it on a window. I don't know how to do it. I can print that variable once using TextOut, but once it compiles, I can't update it. As you can see, I tried in APIENTRY WinMain, but it only goes once and doesn't loop and update it. Variable that I want to increase in a loop is "test". Can you please help me?

This is most of code. Only thing above this are two functions that I use and #include's.

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

	LRESULT CALLBACK WndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam);


	
	INT APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
	{
		for (int i = 0; i < 50; i++){
			inputDat();
			data();

			test += 1;
			Sleep(1000);

			WNDCLASSEX windowClass;
			windowClass.lpszClassName = "Main Class";
			windowClass.cbClsExtra = NULL;
			windowClass.cbWndExtra = NULL;
			windowClass.cbSize = sizeof(WNDCLASSEX);
			windowClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(247, 247, 247));
			windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
			windowClass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
			windowClass.hIconSm = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 16, 16, NULL);
			windowClass.hInstance = hInstance;
			windowClass.lpfnWndProc = WndProc;
			windowClass.lpszMenuName = NULL;
			windowClass.style = CS_VREDRAW | CS_HREDRAW;
			RegisterClassEx(&windowClass);

			HWND Main = CreateWindowEx(NULL, "Main Class", "Weather station report",
				WS_OVERLAPPEDWINDOW | WS_VISIBLE,
				100, 100, 500, 500,
				NULL, NULL, hInstance, NULL);


			if (!Main)
				return(0);


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

			return msg.wParam;
			
			UpdateWindow(Main);
			
		}
	}


	LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		
		char *mtr;
		mtr = new char[384];
		FILE *drd;
		FILE *bbd;

		drd = fopen("bin.txt", "r");
		fread(mtr, 1, 412, drd);
		fclose(drd);
		

		for (int g = 28; g < 412; g += 52){
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 0;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 1;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 2;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 3;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 4;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 5;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 6;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 7;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 8;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 9;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 0xA;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 0xB;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 0xC;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 0xD;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 0xE;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 0xF;
		}


		for (int g = 32; g < 412; g += 52){
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 0;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 1;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 2;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 3;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 4;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 5;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 6;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 7;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 8;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 9;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 0xA;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 0xB;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 0xC;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 0xD;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 0xE;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 0xF;
		}

		for (int g = 36; g < 412; g += 52){
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 0;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 1;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 2;
			if (mtr[g] == 48 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 3;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 4;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 5;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 6;
			if (mtr[g] == 48 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 7;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 8;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 9;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 0xA;
			if (mtr[g] == 49 && mtr[g + 1] == 48 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 0xB;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 48)
				fbd[g] = 0xC;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 48 && mtr[g + 3] == 49)
				fbd[g] = 0xD;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 48)
				fbd[g] = 0xE;
			if (mtr[g] == 49 && mtr[g + 1] == 49 && mtr[g + 2] == 49 && mtr[g + 3] == 49)
				fbd[g] = 0xF;
		}


		int temp = fbd[28] * 10 + fbd[32]-30;
		
		int hum = fbd[80] * 10 + fbd[84] ;
		
		

		TCHAR V1[100];
		TCHAR V2[100];
		TCHAR V3[100];

		TCHAR V4[100];
		TCHAR V5[100];
		TCHAR V6[100];

		TCHAR V7[100];
		TCHAR V8[100];
	

		switch (message)
		{
		case WM_CREATE:
		{

		}

		case WM_COMMAND:
		{

		}
		break;

	
		case WM_PAINT:
		{
			
			PAINTSTRUCT Ps;
			HDC hdc;
			hdc = BeginPaint(hwnd, &Ps);
			SetBkColor(hdc, RGB(255, 255, 255));
			SetTextColor(hdc, RGB(0, 0, 0));
		
			TextOut(hdc, 10, 10, "Temperatura",11);
			test += 1;
			TextOut(hdc, 200, 10, V3, wsprintf(V3, "%d", test));

			TextOut(hdc, 100, 10, V1, wsprintf(V1, "%d", temp));
		
			TextOut(hdc, 10, 30, "Vlaznost", 8);
			TextOut(hdc, 100, 30, V2, wsprintf(V2, "%d", hum));
			

			EndPaint(hwnd, &Ps);
		

		}
		break;
		case WM_CLOSE:
		{
			PostQuitMessage(WM_QUIT);
		}
		break;
		default:
			return(DefWindowProc(hwnd, message, wParam, lParam));
		}
		
		
		return(0);


	}
	
Last edited on
I figured out that it works as long as I am constantly resizing window. So I need to send a msg to tell it to update it.
You can use a timer with SetTimer and process WM_TIMER message. Also, your code is a bit inneficient as written.
That fixes that problem with no update on window, but I still have to put my functions inside WM_PAINT because they don't get updated in WinMain. Do I really have to do that because sometimes it encounters breakpoint? I would rather like to put my functions inside WinMain. But as I said the problem there is that I can't get any loop to work. They all go only once.
Call InvalidateRect to force WM_PAINT to be sent. You can't do what you want directly inside WinMain. No way.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd145002%28v=vs.85%29.aspx
Wow, and I was just about to post a thread asking the same question "How to display variables through the status bar?"

Scrap that, I misread. This is about updating a window, not the status bar.
Last edited on
Thank you modoran. That worked!
Topic archived. No new replies allowed.