mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
82 lines
2.6 KiB
C++
82 lines
2.6 KiB
C++
|
|
#include <windows.h>
|
||
|
|
#include "ThemeHelper.h"
|
||
|
|
|
||
|
|
// Controls changing the themes.
|
||
|
|
|
||
|
|
void SetAppsTheme(bool mode)
|
||
|
|
{
|
||
|
|
HKEY hKey;
|
||
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER,
|
||
|
|
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
|
||
|
|
0,
|
||
|
|
KEY_SET_VALUE,
|
||
|
|
&hKey) == ERROR_SUCCESS)
|
||
|
|
{
|
||
|
|
DWORD value = mode;
|
||
|
|
RegSetValueEx(hKey, L"AppsUseLightTheme", 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(value));
|
||
|
|
RegCloseKey(hKey);
|
||
|
|
|
||
|
|
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, reinterpret_cast<LPARAM>(L"ImmersiveColorSet"), SMTO_ABORTIFHUNG, 5000, nullptr);
|
||
|
|
|
||
|
|
SendMessageTimeout(HWND_BROADCAST, WM_THEMECHANGED, 0, 0, SMTO_ABORTIFHUNG, 5000, nullptr);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SetSystemTheme(bool mode)
|
||
|
|
{
|
||
|
|
HKEY hKey;
|
||
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER,
|
||
|
|
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
|
||
|
|
0,
|
||
|
|
KEY_SET_VALUE,
|
||
|
|
&hKey) == ERROR_SUCCESS)
|
||
|
|
{
|
||
|
|
DWORD value = mode;
|
||
|
|
RegSetValueEx(hKey, L"SystemUsesLightTheme", 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(value));
|
||
|
|
RegCloseKey(hKey);
|
||
|
|
|
||
|
|
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, reinterpret_cast<LPARAM>(L"ImmersiveColorSet"), SMTO_ABORTIFHUNG, 5000, nullptr);
|
||
|
|
|
||
|
|
SendMessageTimeout(HWND_BROADCAST, WM_THEMECHANGED, 0, 0, SMTO_ABORTIFHUNG, 5000, nullptr);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Can think of this as "is the current theme light?"
|
||
|
|
bool GetCurrentSystemTheme()
|
||
|
|
{
|
||
|
|
HKEY hKey;
|
||
|
|
DWORD value = 1; // default = light
|
||
|
|
DWORD size = sizeof(value);
|
||
|
|
|
||
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER,
|
||
|
|
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
|
||
|
|
0,
|
||
|
|
KEY_READ,
|
||
|
|
&hKey) == ERROR_SUCCESS)
|
||
|
|
{
|
||
|
|
RegQueryValueEx(hKey, L"SystemUsesLightTheme", nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &size);
|
||
|
|
RegCloseKey(hKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
return value == 1; // true = light, false = dark
|
||
|
|
}
|
||
|
|
|
||
|
|
bool GetCurrentAppsTheme()
|
||
|
|
{
|
||
|
|
HKEY hKey;
|
||
|
|
DWORD value = 1;
|
||
|
|
DWORD size = sizeof(value);
|
||
|
|
|
||
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER,
|
||
|
|
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
|
||
|
|
0,
|
||
|
|
KEY_READ,
|
||
|
|
&hKey) == ERROR_SUCCESS)
|
||
|
|
{
|
||
|
|
RegQueryValueEx(hKey, L"AppsUseLightTheme", nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &size);
|
||
|
|
RegCloseKey(hKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
return value == 1; // true = light, false = dark
|
||
|
|
}
|