-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeResolution.cpp
More file actions
343 lines (322 loc) · 11.7 KB
/
ChangeResolution.cpp
File metadata and controls
343 lines (322 loc) · 11.7 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
using namespace std;
static void ShowHelp(char **argv)
{
TCHAR szPath[_MAX_PATH];
TCHAR szDir[_MAX_DIR];
TCHAR szDrive[_MAX_DRIVE];
TCHAR szFName[_MAX_FNAME];
TCHAR szExt[_MAX_EXT];
GetModuleFileName(NULL, szPath, MAX_PATH);
_tsplitpath_s(szPath, szDrive, szDir, szFName, szExt);
::wcout << "Usage:" << ::endl;
::wcout << " " << szFName << " [-d N] [-list] | [-name] | [-current] | [-displays] | WIDTH HEIGHT | NAME" << ::endl;
::wcout << ::endl;
::wcout << "Options:" << ::endl;
::wcout << " -d N, --display N Specify display number N (0: main, 1: second, ...)" << ::endl;
::wcout << " -c, --current Show the current resolution of the specified display" << ::endl;
::wcout << " -l, --list Show available resolutions for the specified display" << ::endl;
::wcout << " -n, --name Show defined resolution names" << ::endl;
::wcout << " -D, --displays Show all display numbers, device names, and current resolutions" << ::endl;
::wcout << " WIDTH HEIGHT Change the resolution of the specified display to WIDTH x HEIGHT" << ::endl;
::wcout << " NAME Change the resolution of the specified display to NAME" << ::endl;
::wcout << ::endl;
::wcout << "Examples:" << ::endl;
::wcout << " " << szFName << " 1024 768 Change main display to 1024x768" << ::endl;
::wcout << " " << szFName << " SXGA Change main display to SXGA" << ::endl;
::wcout << " " << szFName << " -d 1 1920 1080 Change second display to 1920x1080" << ::endl;
::wcout << " " << szFName << " -d 2 --list Show available resolutions for third display" << ::endl;
::wcout << " " << szFName << " -d 1 --current Show current resolution of second display" << ::endl;
::wcout << " " << szFName << " --displays Show all display numbers, device names, and current resolutions" << ::endl;
}
void ShowDisplays()
{
int index = 0;
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
::wcout << "Index | DeviceName | DeviceString | CurrentResolution" << ::endl;
::wcout << "------|------------|--------------|------------------" << ::endl;
while (EnumDisplayDevices(NULL, index, &dd, 0)) {
std::wstring deviceName = dd.DeviceName;
DEVMODE dm = { 0 };
dm.dmSize = sizeof(dm);
std::wstring res = L"-";
if (EnumDisplaySettingsEx(deviceName.c_str(), ENUM_CURRENT_SETTINGS, &dm, 0)) {
res = std::to_wstring(dm.dmPelsWidth) + L"x" + std::to_wstring(dm.dmPelsHeight);
}
::wcout << index << " | " << deviceName
<< " | " << dd.DeviceString
<< " | " << res << ::endl;
index++;
dd.cb = sizeof(dd);
}
}
void ShowList()
{
DEVMODE ExistDisplayMode;
int i = 0;
int height = 0;
int width = 0;
int lastH = 0;
int lastW = 0;
while (EnumDisplaySettings(NULL, i, &ExistDisplayMode))
{
height = ExistDisplayMode.dmPelsHeight;
width = ExistDisplayMode.dmPelsWidth;
if ((lastH != height) && (lastW != width))
{
::wcout << width << "x" << height << ::endl;
lastW = width;
lastH = height;
}
i++;
}
}
void ShowName()
{
DEVMODE ExistDisplayMode;
/*
int i = 0;
while (EnumDisplaySettings(NULL, i, &ExistDisplayMode))
{
i++;
}
*/
EnumDisplaySettings(NULL, 0, &ExistDisplayMode);
int width = ExistDisplayMode.dmPelsWidth;
int height = ExistDisplayMode.dmPelsHeight;
::wcout << "Name Resolution" << ::endl;
::wcout << "====== ==========" << ::endl;
::wcout << "CGA 320x200" << ::endl;
::wcout << "EGA 640x350" << ::endl;
::wcout << "DCGA 640x400" << ::endl;
::wcout << "SVGA 800x600" << ::endl;
::wcout << "XGA 1024x768" << ::endl;
::wcout << "HD 1280x720" << ::endl;
::wcout << "FWXGA 1366x768" << ::endl;
::wcout << "SXGA 1280x1024" << ::endl;
::wcout << "XGA++ 1600x900" << ::endl;
::wcout << "UXGA 1600x1200" << ::endl;
::wcout << "FHD 1920x1080" << ::endl;
::wcout << "WUXGA 1920x1200" << ::endl;
::wcout << "WQHD 2560x1440" << ::endl;
::wcout << "WQXGA 2560x1600" << ::endl;
::wcout << "QSXGA 2560x2048" << ::endl;
::wcout << "QHD+ 3200x1800" << ::endl;
::wcout << "WQSXGA 3200x2048" << ::endl;
::wcout << "QUXGA 3200x2400" << ::endl;
::wcout << "4K 3840x2160" << ::endl;
::wcout << "WQUXGA 3840x2400" << ::endl;
::wcout << "5K 5120x2880" << ::endl;
::wcout << "8K 7680x4320" << ::endl;
::wcout << "16K 17280x4320" << ::endl;
::wcout << "------ ----------" << ::endl;
::wcout << "MAX " << width << "x" << height << ::endl;
}
bool ShowCurrent()
{
DEVMODE deviceMode = { 0 };
deviceMode.dmSize = sizeof(deviceMode);
deviceMode.dmDriverExtra = 0;
if (EnumDisplaySettingsEx(NULL, ENUM_CURRENT_SETTINGS, &deviceMode, 0))
{
::wcout << deviceMode.dmPelsWidth << "x" << deviceMode.dmPelsHeight << ::endl;
}
else
{
::wcerr << "Failed to get current resolution." << ::endl;
return false;
}
return true;
}
bool GetDisplayDeviceName(int displayIndex, std::wstring &deviceName)
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
if (EnumDisplayDevices(NULL, displayIndex, &dd, 0)) {
deviceName = dd.DeviceName;
return true;
}
return false;
}
void ShowList(const std::wstring& deviceName)
{
DEVMODE ExistDisplayMode;
int i = 0;
int height = 0;
int width = 0;
int lastH = 0;
int lastW = 0;
while (EnumDisplaySettings(deviceName.empty() ? NULL : deviceName.c_str(), i, &ExistDisplayMode))
{
height = ExistDisplayMode.dmPelsHeight;
width = ExistDisplayMode.dmPelsWidth;
if ((lastH != height) && (lastW != width))
{
::wcout << width << "x" << height << ::endl;
lastW = width;
lastH = height;
}
i++;
}
}
bool ShowCurrent(const std::wstring& deviceName)
{
DEVMODE deviceMode = { 0 };
deviceMode.dmSize = sizeof(deviceMode);
deviceMode.dmDriverExtra = 0;
if (EnumDisplaySettingsEx(deviceName.empty() ? NULL : deviceName.c_str(), ENUM_CURRENT_SETTINGS, &deviceMode, 0))
{
::wcout << deviceMode.dmPelsWidth << "x" << deviceMode.dmPelsHeight << ::endl;
}
else
{
::wcerr << "Failed to get current resolution." << ::endl;
return false;
}
return true;
}
static BOOL ParseArgs(int argc, char **argv, int *width, int *height, int *displayIndex)
{
char *arg = NULL;
int n = 0;
string str;
*displayIndex = 0;
int argOffset = 0;
if (argc > 2 && (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--display") == 0)) {
*displayIndex = atoi(argv[2]);
argOffset = 2;
}
if (width == NULL || height == NULL) {
return FALSE;
}
if (argc == 1 + argOffset) {
ShowHelp(argv);
exit(0);
}
else if (argc == 2 + argOffset) {
arg = argv[1 + argOffset];
str = arg;
string p = argv[1 + argOffset];
transform(p.begin(), p.end(), p.begin(), toupper);
std::wstring deviceName;
GetDisplayDeviceName(*displayIndex, deviceName);
if (p == "-?" || p == "/?" || p == "-H" || p == "/H" || p == "--HELP" || p == "/HELP") {
ShowHelp(argv);
exit(0);
}
else if (p == "-L" || p == "/L" || p == "--LIST" || p == "/LIST") {
ShowList(deviceName);
exit(0);
}
else if (p == "-N" || p == "/N" || p == "--NAME" || p == "/NAME") {
ShowName();
exit(0);
}
else if (p == "-C" || p == "/C" || p == "--CURRENT" || p == "/CURRENT" || p == "--NOW" || p == "/NOW") {
ShowCurrent(deviceName);
exit(0);
}
else if (p == "-D" || p == "--DISPLAYS") {
ShowDisplays();
exit(0);
}
else if (p == "CGA") { *width = 320; *height = 200; }
else if (p == "EGA") { *width = 640; *height = 350; }
else if (p == "DCGA") { *width = 640; *height = 400; }
else if (p == "VGA") { *width = 640; *height = 480; }
else if (p == "SVGA") { *width = 800; *height = 600; }
else if (p == "XGA") { *width = 1024; *height = 768; }
else if ((p == "HD") || (p == "HD720") || (p == "720P") || (p == "HD720P")) { *width = 1280; *height = 720; }
else if (p == "FWXGA") { *width = 1366; *height = 768; }
else if (p == "SXGA") { *width = 1280; *height = 1024; }
else if (p == "XGA++") { *width = 1600; *height = 900; }
else if (p == "UXGA") { *width = 1600; *height = 1200; }
else if ((p == "FHD") || (p == "FULLHD") || (p == "FULL-HD") || (p == "HD1080") || (p == "1080P")) { *width = 1920; *height = 1080; }
else if (p == "WUXGA") { *width = 1920; *height = 1200; }
else if (p == "WQHD") { *width = 2560; *height = 1440; }
else if (p == "WQXGA") { *width = 2560; *height = 1600; }
else if (p == "QSXGA") { *width = 2560; *height = 2048; }
else if (p == "QHD+") { *width = 3200; *height = 1800; }
else if (p == "WQSXGA") { *width = 3200; *height = 2048; }
else if (p == "QUXGA") { *width = 3200; *height = 2400; }
else if ((p == "4K") || (p == "QFHD")) { *width = 3840; *height = 2160; }
else if (p == "WQUXGA") { *width = 3840; *height = 2400; }
else if (p == "5K") { *width = 5120; *height = 2880; }
else if (p == "8K") { *width = 7680; *height = 4320; }
else if (p == "16K") { *width = 17280; *height = 4320; }
else if (p == "MAX") {
DEVMODE ExistDisplayMode;
int i = 0;
std::wstring deviceName;
if (!GetDisplayDeviceName(*displayIndex, deviceName)) return FALSE;
while (EnumDisplaySettings(deviceName.c_str(), i, &ExistDisplayMode)) {
i++;
}
*width = ExistDisplayMode.dmPelsWidth;
*height = ExistDisplayMode.dmPelsHeight;
}
else {
return FALSE;
}
}
else if (argc == 3 + argOffset) {
arg = argv[1 + argOffset];
n = atoi(arg);
if (n <= 0) return FALSE;
*width = n;
arg = argv[2 + argOffset];
n = atoi(arg);
if (n <= 0) return FALSE;
*height = n;
}
else {
return FALSE;
}
return TRUE;
}
int main(int argc, char **argv)
{
int newWidth = 0;
int newHeight = 0;
int displayIndex = 0;
if (!ParseArgs(argc, argv, &newWidth, &newHeight, &displayIndex)) {
::wcerr << "Invalid Argument(s)." << ::endl << ::endl;
return EXIT_FAILURE;
}
std::wstring deviceName;
if (!GetDisplayDeviceName(displayIndex, deviceName)) {
::wcerr << "Invalid display index." << ::endl;
return EXIT_FAILURE;
}
DEVMODE deviceMode = { 0 };
deviceMode.dmSize = sizeof(deviceMode);
deviceMode.dmDriverExtra = 0;
if (EnumDisplaySettingsEx(deviceName.c_str(), ENUM_CURRENT_SETTINGS, &deviceMode, 0)) {
if ((deviceMode.dmPelsWidth != newWidth) || (deviceMode.dmPelsHeight != newHeight)) {
deviceMode.dmPelsWidth = newWidth;
deviceMode.dmPelsHeight = newHeight;
if (ChangeDisplaySettingsEx(deviceName.c_str(), &deviceMode, NULL, 0, NULL) == DISP_CHANGE_SUCCESSFUL) {
::wcout << "Succeeded: " << newWidth << "x" << newHeight << ::endl;
}
else {
::wcerr << "Failed: " << newWidth << "x" << newHeight << ::endl;
return EXIT_FAILURE;
}
}
else {
::wcout << "Already Same Resolution: " << newWidth << "x" << newHeight << ::endl;
}
}
else {
::wcerr << "Failed to get current resolution." << ::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}