2
2
#include <commctrl.h>
3
3
#include <tchar.h>
4
4
#include <winreg.h>
5
+ #include <stdbool.h>
6
+ #include <stdio.h>
5
7
6
8
// IDs dos controles
7
9
#define IDC_COMBOBOX 101
8
10
#define IDC_APPLY_BUTTON 102
9
11
#define IDC_DELETE_BUTTON 103
10
12
#define IDC_CANCEL_BUTTON 104
11
13
12
- // Opções do dropdown
14
+ // Opções do dropdown
13
15
const TCHAR * versions [] = {
14
16
_T ("Windows 2000" ),
15
17
_T ("Windows XP SP3" ),
@@ -25,7 +27,7 @@ const TCHAR* versions[] = {
25
27
};
26
28
27
29
28
- // Valores associados às opções
30
+ // Valores associados às opções
29
31
const TCHAR * versionValues [] = {
30
32
_T ("5.0.2195" ),
31
33
_T ("5.1.2600" ),
@@ -40,12 +42,101 @@ const TCHAR* versionValues[] = {
40
42
_T ("10.0.22600" )
41
43
};
42
44
43
- // Prototipo das funções
45
+ // Prototipo das funções
44
46
LRESULT CALLBACK WndProc (HWND , UINT , WPARAM , LPARAM );
45
- void WriteToRegistry (const TCHAR * );
46
- void DeleteRegistryKey ();
47
+ void WriteToRegistry (const TCHAR * , bool );
48
+ void DeleteRegistryKey (bool );
47
49
48
50
int WINAPI WinMain (HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow ) {
51
+ LPTSTR cmdLine = GetCommandLine (); // Gets the full command line
52
+
53
+ // Skip the program's own name
54
+ if (cmdLine [0 ] == _T ('"' ))
55
+ {
56
+ // If the program name is quoted, skip until the closing quote
57
+ cmdLine = _tcschr (cmdLine + 1 , _T ('"' ));
58
+ if (cmdLine )
59
+ cmdLine ++ ;
60
+ }
61
+ else
62
+ {
63
+ // Otherwise, skip until the first space
64
+ cmdLine = _tcschr (cmdLine , _T (' ' ));
65
+ }
66
+
67
+ // Skip any spaces after the program name
68
+ if (cmdLine )
69
+ while (* cmdLine == _T (' ' ))
70
+ cmdLine ++ ;
71
+
72
+ // Check if there's anything left in the command line
73
+ if (cmdLine && * cmdLine )
74
+ {
75
+ if (_tcsicmp (cmdLine , _T ("/win2000" )) == 0 )
76
+ {
77
+ WriteToRegistry (versionValues [0 ], false); // Windows 2000
78
+ return 0 ;
79
+ }
80
+ else if (_tcsicmp (cmdLine , _T ("/winxp" )) == 0 )
81
+ {
82
+ WriteToRegistry (versionValues [1 ], false); // Windows XP SP3
83
+ return 0 ;
84
+ }
85
+ else if (_tcsicmp (cmdLine , _T ("/win2003" )) == 0 )
86
+ {
87
+ WriteToRegistry (versionValues [2 ], false); // Windows Server 2003 SP2
88
+ return 0 ;
89
+ }
90
+ else if (_tcsicmp (cmdLine , _T ("/winvista" )) == 0 )
91
+ {
92
+ WriteToRegistry (versionValues [3 ], false); // Windows Vista SP2
93
+ return 0 ;
94
+ }
95
+ else if (_tcsicmp (cmdLine , _T ("/win7" )) == 0 )
96
+ {
97
+ WriteToRegistry (versionValues [4 ], false); // Windows 7 SP1
98
+ return 0 ;
99
+ }
100
+ else if (_tcsicmp (cmdLine , _T ("/win8" )) == 0 )
101
+ {
102
+ WriteToRegistry (versionValues [5 ], false); // Windows 8.1
103
+ return 0 ;
104
+ }
105
+ else if (_tcsicmp (cmdLine , _T ("/win10" )) == 0 )
106
+ {
107
+ WriteToRegistry (versionValues [9 ], false); // Windows 10 22H2
108
+ return 0 ;
109
+ }
110
+ else if (_tcsicmp (cmdLine , _T ("/win11" )) == 0 )
111
+ {
112
+ WriteToRegistry (versionValues [10 ], false); // Windows 11 24H2
113
+ return 0 ;
114
+ }
115
+ else if (_tcsicmp (cmdLine , _T ("/delete" )) == 0 )
116
+ {
117
+ DeleteRegistryKey (false);
118
+ return 0 ;
119
+ }
120
+ else
121
+ {
122
+ MessageBox (
123
+ NULL ,
124
+ _T ("Usage: wct.exe [options]\n\n"
125
+ "Options:\n"
126
+ "/win2000 - Set Windows 2000 compatibility\n"
127
+ "/winxp - Set Windows XP SP3 compatibility\n"
128
+ "/win2003 - Set Windows Server 2003 SP2 compatibility\n"
129
+ "/winvista - Set Windows Vista SP2 compatibility\n"
130
+ "/win7 - Set Windows 7 SP1 compatibility\n"
131
+ "/win8 - Set Windows 8.1 compatibility\n"
132
+ "/win10 - Set Windows 10 22H2 compatibility\n"
133
+ "/win11 - Set Windows 11 24H2 compatibility\n"
134
+ "/delete - Remove the compatibility" ),
135
+ _T ("Help" ), MB_OK | MB_ICONINFORMATION );
136
+ return 1 ;
137
+ }
138
+ }
139
+
49
140
WNDCLASS wc = {0 };
50
141
HWND hwnd ;
51
142
MSG msg = {0 };
@@ -57,12 +148,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
57
148
58
149
if (!RegisterClass (& wc )) return -1 ;
59
150
60
- hwnd = CreateWindow (
151
+ hwnd = CreateWindowExW (
152
+ WS_EX_APPWINDOW | WS_EX_WINDOWEDGE | WS_EX_DLGMODALFRAME ,
61
153
_T ("DropdownApp" ),
62
154
_T ("Windows Compatibility Tool" ),
63
- WS_OVERLAPPEDWINDOW | WS_VISIBLE ,
155
+ WS_VISIBLE | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX ,
64
156
CW_USEDEFAULT , CW_USEDEFAULT ,
65
- 400 , 200 ,
157
+ 398 , 165 ,
66
158
NULL , NULL , hInstance , NULL
67
159
);
68
160
@@ -74,6 +166,23 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
74
166
return 0 ;
75
167
}
76
168
169
+ // Function to enable or disable the window and its controls
170
+ void HandleWindow (bool enable , HWND hComboBox , HWND hApplyButton , HWND hDeleteButton , HWND hCancelButton , HWND hwnd )
171
+ {
172
+ EnableWindow (hwnd , enable );
173
+ EnableWindow (hComboBox , enable );
174
+ EnableWindow (hApplyButton , enable );
175
+ EnableWindow (hDeleteButton , enable );
176
+ EnableWindow (hCancelButton , enable );
177
+ EnableMenuItem (
178
+ GetSystemMenu (hwnd , FALSE), SC_CLOSE ,
179
+ enable ? MF_BYCOMMAND | MF_ENABLED : MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
180
+ EnableMenuItem (
181
+ GetSystemMenu (hwnd , FALSE), SC_MINIMIZE ,
182
+ enable ? MF_BYCOMMAND | MF_ENABLED : MF_BYCOMMAND | MF_DISABLED | MF_GRAYED );
183
+ }
184
+
185
+
77
186
LRESULT CALLBACK WndProc (HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam ) {
78
187
static HWND hComboBox , hApplyButton , hDeleteButton , hCancelButton ;
79
188
static HFONT hFont ;
@@ -97,14 +206,14 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
97
206
GetModuleHandle (NULL ), NULL
98
207
);
99
208
100
- // Adiciona as opções ao ComboBox
209
+ // Adiciona as opções ao ComboBox
101
210
for (i = 0 ; i < sizeof (versions ) / sizeof (versions [0 ]); ++ i ) {
102
211
SendMessage (hComboBox , CB_ADDSTRING , 0 , (LPARAM )versions [i ]);
103
212
}
104
213
105
- // Definir o índice padrão (por exemplo, 1 - "Item 2")
214
+ // Definir o índice padrão (por exemplo, 1 - "Item 2")
106
215
SendMessage (hComboBox , CB_SETCURSEL , 0 , 0 );
107
- // Botão Aplicar
216
+ // Botão Aplicar
108
217
hApplyButton = CreateWindow (
109
218
_T ("BUTTON" ), _T ("Apply" ),
110
219
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON ,
@@ -113,7 +222,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
113
222
GetModuleHandle (NULL ), NULL
114
223
);
115
224
116
- // Botão Deletar
225
+ // Botão Deletar
117
226
hDeleteButton = CreateWindow (
118
227
_T ("BUTTON" ), _T ("Delete" ),
119
228
WS_TABSTOP | WS_VISIBLE | WS_CHILD ,
@@ -122,7 +231,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
122
231
GetModuleHandle (NULL ), NULL
123
232
);
124
233
125
- // Botão Cancelar
234
+ // Botão Cancelar
126
235
hCancelButton = CreateWindow (
127
236
_T ("BUTTON" ), _T ("Cancel" ),
128
237
WS_TABSTOP | WS_VISIBLE | WS_CHILD ,
@@ -141,26 +250,34 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
141
250
142
251
case WM_COMMAND : {
143
252
if (HIWORD (wParam ) == CBN_SELCHANGE && LOWORD (wParam ) == IDC_COMBOBOX ) {
144
- // Seleção mudou no ComboBox
253
+ // Seleção mudou no ComboBox
145
254
}
146
255
147
256
switch (LOWORD (wParam )) {
148
257
case IDC_APPLY_BUTTON : {
149
- // Obtém a seleção do ComboBox
258
+ // Obtém a seleção do ComboBox
150
259
int idx = SendMessage (hComboBox , CB_GETCURSEL , 0 , 0 );
260
+ // Disable the window
261
+ HandleWindow (FALSE, hComboBox , hApplyButton , hDeleteButton , hCancelButton , hwnd );
151
262
if (idx != CB_ERR ) {
152
263
const TCHAR * selectedValue = versionValues [idx ];
153
- WriteToRegistry (selectedValue );
264
+ WriteToRegistry (selectedValue , true );
154
265
} else {
155
- MessageBox (hwnd , _T ("Please, select a version." ), _T ("Error" ), MB_OK | MB_ICONERROR );
266
+ MessageBox (hwnd , _T ("Please select a version." ), _T ("Error" ), MB_OK | MB_ICONERROR );
156
267
}
268
+ // Enable the window
269
+ HandleWindow (TRUE, hComboBox , hApplyButton , hDeleteButton , hCancelButton , hwnd );
157
270
break ;
158
271
}
159
272
160
273
case IDC_DELETE_BUTTON : {
161
274
SendMessage (hComboBox , CB_SETCURSEL , -1 , 0 );
162
- DeleteRegistryKey ();
163
- //MessageBox(hwnd, _T("Seleção deletada e chave de registro removida!"), _T("Deletar"), MB_OK | MB_ICONINFORMATION);
275
+ // Disable the window
276
+ HandleWindow (FALSE, hComboBox , hApplyButton , hDeleteButton , hCancelButton , hwnd );
277
+ DeleteRegistryKey (true);
278
+ //MessageBox(hwnd, _T("Seleção deletada e chave de registro removida!"), _T("Deletar"), MB_OK | MB_ICONINFORMATION);
279
+ // Enable the window
280
+ HandleWindow (TRUE, hComboBox , hApplyButton , hDeleteButton , hCancelButton , hwnd );
164
281
break ;
165
282
}
166
283
@@ -183,7 +300,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
183
300
return 0 ;
184
301
}
185
302
186
- void WriteToRegistry (const TCHAR * value ) {
303
+ void WriteToRegistry (const TCHAR * value , bool useMessageBox ) {
187
304
HKEY hKey ;
188
305
int msgboxID ;
189
306
LONG result ;
@@ -214,16 +331,32 @@ void WriteToRegistry(const TCHAR* value) {
214
331
}
215
332
#endif
216
333
RegCloseKey (hKey );
217
- msgboxID = MessageBox (NULL , _T ("Value saved with success on registry!" ), _T ("Success" ), MB_OK | MB_ICONINFORMATION );
218
- if (msgboxID == IDOK ){
219
- PostQuitMessage (0 );
220
- }
334
+ if (useMessageBox )
335
+ {
336
+ msgboxID = MessageBox (NULL , _T ("Value saved with success on registry!" ), _T ("Success" ), MB_OK | MB_ICONINFORMATION );
337
+
338
+ if (msgboxID == IDOK )
339
+ {
340
+ PostQuitMessage (0 );
341
+ }
342
+ }
343
+ else
344
+ {
345
+ PostQuitMessage (0 );
346
+ }
221
347
} else {
222
- MessageBox (NULL , _T ("Error while trying access the registry key." ), _T ("Error" ), MB_OK | MB_ICONERROR );
348
+ if (useMessageBox )
349
+ {
350
+ MessageBox (NULL , _T ("Error while trying access the registry key." ), _T ("Error" ), MB_OK | MB_ICONERROR );
351
+ }
352
+ else
353
+ {
354
+ PostQuitMessage (1 );
355
+ }
223
356
}
224
357
}
225
358
226
- void DeleteRegistryKey () {
359
+ void DeleteRegistryKey (bool useMessageBox ) {
227
360
HKEY hKey ;
228
361
LONG result ;
229
362
int msgboxID ;
@@ -259,15 +392,37 @@ void DeleteRegistryKey() {
259
392
#endif
260
393
261
394
if (result == ERROR_SUCCESS ) {
262
- msgboxID = MessageBox (NULL , _T ("Registry Key removed with success!" ), _T ("Success" ), MB_OK | MB_ICONINFORMATION );
263
- if (msgboxID == IDOK ){
264
- PostQuitMessage (0 );
265
- }
395
+ if (useMessageBox )
396
+ {
397
+ msgboxID = MessageBox (NULL , _T ("Registry Key removed with success!" ), _T ("Success" ), MB_OK | MB_ICONINFORMATION );
398
+ if (msgboxID == IDOK )
399
+ {
400
+ PostQuitMessage (0 );
401
+ }
402
+ }
403
+ else
404
+ {
405
+ PostQuitMessage (0 );
406
+ }
266
407
} else {
267
- MessageBox (NULL , _T ("Error while trying remove the registry key value." ), _T ("Error" ), MB_OK | MB_ICONERROR );
408
+ if (useMessageBox )
409
+ {
410
+ MessageBox (NULL , _T ("Error while trying remove the registry key value." ), _T ("Error" ), MB_OK | MB_ICONERROR );
411
+ }
412
+ else
413
+ {
414
+ PostQuitMessage (1 );
415
+ }
268
416
}
269
417
} else {
270
- MessageBox (NULL , _T ("Error trying open the registry key." ), _T ("Erro" ), MB_OK | MB_ICONERROR );
418
+ if (useMessageBox )
419
+ {
420
+ MessageBox (NULL , _T ("Error trying open the registry key." ), _T ("Error" ), MB_OK | MB_ICONERROR );
421
+ }
422
+ else
423
+ {
424
+ PostQuitMessage (1 );
425
+ }
271
426
}
272
427
}
273
428
0 commit comments