-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeSwitcher.cpp
More file actions
115 lines (99 loc) · 3.47 KB
/
ThemeSwitcher.cpp
File metadata and controls
115 lines (99 loc) · 3.47 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
#include "ThemeSwitcher.h"
#include <tlhelp32.h>
#include <iostream>
ThemeChangeResult changeTheme(int theme) {
ThemeChangeResult result = { false, ThemeError::SUCCESS, 0 };
// テーマパラメータ検証
if (theme != LIGHT_THEME && theme != DARK_THEME) {
result.errorCode = ThemeError::INVALID_THEME;
return result;
}
HKEY hKey;
DWORD value = (theme == LIGHT_THEME) ? 1 : 0;
const wchar_t* regPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
// 適切なエラー処理でレジストリキーを開く
LONG openResult = RegOpenKeyExW(HKEY_CURRENT_USER, regPath, 0, KEY_SET_VALUE, &hKey);
if (openResult != ERROR_SUCCESS) {
result.errorCode = ThemeError::REGISTRY_OPEN_FAILED;
result.systemErrorCode = openResult;
return result;
}
// AppsUseLightTheme, SystemUsesLightTheme両方を設定
LONG setAppsResult = RegSetValueExW(hKey, L"AppsUseLightTheme", 0, REG_DWORD,
(BYTE*)&value, sizeof(value));
LONG setSystemResult = RegSetValueExW(hKey, L"SystemUsesLightTheme", 0, REG_DWORD,
(BYTE*)&value, sizeof(value));
RegCloseKey(hKey);
// 両方のレジストリ操作が成功したかどうかを確認
if (setAppsResult != ERROR_SUCCESS || setSystemResult != ERROR_SUCCESS) {
result.errorCode = ThemeError::REGISTRY_SET_FAILED;
result.systemErrorCode = (setAppsResult != ERROR_SUCCESS) ? setAppsResult : setSystemResult;
return result;
}
// テーマの変更をすべてのウィンドウにブロードキャスト
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM)L"ImmersiveColorSet",
SMTO_ABORTIFHUNG, 1000, NULL);
result.success = true;
return result;
}
bool restartExplorer() {
DWORD explorerPID = 0;
HANDLE hSnapshot = NULL;
PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);
// explorer.exeを探す
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return false;
}
bool found = false;
if (Process32FirstW(hSnapshot, &pe32)) {
do {
if (_wcsicmp(pe32.szExeFile, L"explorer.exe") == 0) {
explorerPID = pe32.th32ProcessID;
found = true;
break;
}
} while (Process32NextW(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
if (!found) {
return false;
}
// explorer.exe終了
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, explorerPID);
if (hProcess != NULL) {
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
Sleep(500); // プロセスが終了するまで少し待つ
}
else {
return false;
}
// explorer.exe再起動
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessW(L"C:\\Windows\\explorer.exe", NULL, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return true;
}
return false;
}
const char* getErrorMessage(const ThemeChangeResult& result) {
if (result.success) {
return "Theme changed successfully.";
}
switch (result.errorCode) {
case ThemeError::REGISTRY_OPEN_FAILED:
return "Failed to open registry key. You may need administrator privileges.";
case ThemeError::REGISTRY_SET_FAILED:
return "Failed to set registry value. Access denied.";
case ThemeError::INVALID_THEME:
return "Invalid theme selection.";
default:
return "Unknown error occurred.";
}
}