-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnhancedDisplaySwitch.cpp
More file actions
82 lines (67 loc) · 2.53 KB
/
EnhancedDisplaySwitch.cpp
File metadata and controls
82 lines (67 loc) · 2.53 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
#include "EnhancedDisplaySwitch.h"
#include <windows.h>
#include <iostream>
#include <string>
#include <shlobj.h>
void saveLastMode(const std::wstring& mode) {
HKEY hKey;
std::wstring subKey = L"SOFTWARE\\EnhancedDisplaySwitch";
if (RegCreateKeyExW(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
RegSetValueExW(hKey, L"LastMode", 0, REG_SZ, (const BYTE*)mode.c_str(), static_cast<DWORD>((mode.size() + 1) * sizeof(wchar_t)));
RegCloseKey(hKey);
}
}
void EDS::displayHelp() {
std::wcout << L"Usage: EnhancedDisplaySwitch.exe [option]\n";
std::wcout << L"Options:\n";
std::wcout << L" /internal or 1 Set display mode to internal\n";
std::wcout << L" /clone or 2 Set display mode to clone\n";
std::wcout << L" /external or 3 Set display mode to external\n";
std::wcout << L" /extend or 4 Set display mode to extend\n";
std::wcout << L" /lastmode or 5 Show the last used mode\n";
std::wcout << L" -h, --help, /? Display this help message\n";
}
std::wstring EDS::getLastMode() {
HKEY hKey;
std::wstring subKey = L"SOFTWARE\\EnhancedDisplaySwitch";
std::wstring mode;
if (RegOpenKeyExW(HKEY_CURRENT_USER, subKey.c_str(), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
wchar_t buffer[256];
DWORD bufferSize = sizeof(buffer);
if (RegQueryValueExW(hKey, L"LastMode", NULL, NULL, (LPBYTE)buffer, &bufferSize) == ERROR_SUCCESS) {
mode = buffer;
}
RegCloseKey(hKey);
}
return mode.empty() ? L"No last mode found." : mode;
}
void EDS::runDisplaySwitch(int mode) {
LONG result = ERROR_SUCCESS;
switch (mode) {
case 1:
result = SetDisplayConfig(0, NULL, 0, NULL, SDC_TOPOLOGY_INTERNAL | SDC_APPLY);
saveLastMode(L"internal");
break;
case 2:
result = SetDisplayConfig(0, NULL, 0, NULL, SDC_TOPOLOGY_CLONE | SDC_APPLY);
saveLastMode(L"clone");
break;
case 3:
result = SetDisplayConfig(0, NULL, 0, NULL, SDC_TOPOLOGY_EXTEND | SDC_APPLY);
saveLastMode(L"extend");
break;
case 4:
result = SetDisplayConfig(0, NULL, 0, NULL, SDC_TOPOLOGY_EXTERNAL | SDC_APPLY);
saveLastMode(L"external");
break;
default:
std::cerr << "Invalid mode selected.\n";
return;
}
if (result == ERROR_SUCCESS) {
return;
} else {
std::cerr << "Failed to set display configuration. Error: " << result << "\n";
return;
}
}