Global (Windows): prefers NTAPI for querying registry values

This commit is contained in:
李通洲
2026-03-13 10:50:31 +08:00
parent 6ed8aac6bb
commit ea6ad4ac86
15 changed files with 575 additions and 201 deletions
+24 -17
View File
@@ -63,18 +63,18 @@ const char* colorHexToString(DWORD hex)
bool ffDetectWmTheme(FFstrbuf* themeOrError)
{
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes", &hKey, NULL))
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes", &hKey, NULL))
{
FF_STRBUF_AUTO_DESTROY theme = ffStrbufCreate();
if(ffRegReadStrbuf(hKey, L"CurrentTheme", &theme, NULL))
if (ffRegReadStrbuf(hKey, L"CurrentTheme", &theme, NULL))
{
ffStrbufSubstrBeforeLastC(&theme, '.');
ffStrbufSubstrAfterLastC(&theme, '\\');
if(isalpha(theme.chars[0]))
theme.chars[0] = (char)toupper(theme.chars[0]);
ffStrbufAppendF(themeOrError, "%s", theme.chars);
ffStrbufAppend(themeOrError, &theme);
}
}
}
@@ -82,28 +82,35 @@ bool ffDetectWmTheme(FFstrbuf* themeOrError)
do {
uint32_t rgbColor;
uint32_t bgrColor;
DWORD bufSize = sizeof(bgrColor);
if(RegGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", L"AccentColor", RRF_RT_REG_DWORD, NULL, &bgrColor, &bufSize) == ERROR_SUCCESS)
rgbColor = ((bgrColor & 0xFF) << 16) | (bgrColor & 0xFF00) | ((bgrColor >> 16) & 0xFF);
else if(RegGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", L"ColorizationColor", RRF_RT_REG_DWORD, NULL, &rgbColor, &bufSize) == ERROR_SUCCESS)
rgbColor &= 0xFFFFFF;
else
break;
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", &hKey, NULL))
{
if (ffRegReadUint(hKey, L"AccentColor", &bgrColor, NULL))
rgbColor = ((bgrColor & 0xFF) << 16) | (bgrColor & 0xFF00) | ((bgrColor >> 16) & 0xFF);
else if (ffRegReadUint(hKey, L"ColorizationColor", &rgbColor, NULL))
rgbColor &= 0xFFFFFF;
else
break;
}
else break;
if(themeOrError->length > 0) ffStrbufAppendS(themeOrError, " - ");
if (themeOrError->length > 0) ffStrbufAppendS(themeOrError, " - ");
const char* text = colorHexToString(rgbColor);
if(text)
if (text)
ffStrbufAppendS(themeOrError, text);
else
ffStrbufAppendF(themeOrError, "#%06lX", (long)rgbColor);
} while (false);
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", &hKey, NULL))
FF_AUTO_CLOSE_FD HANDLE hKey = NULL;
if (ffRegOpenKeyForRead(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", &hKey, NULL))
{
uint32_t system = 1, apps = 1;
if (ffRegReadUint(hKey, L"SystemUsesLightTheme", &system, NULL) && ffRegReadUint(hKey, L"AppsUseLightTheme", &apps, NULL))
if (ffRegReadValues(hKey, 2, (FFRegValueArg[]) {
FF_ARG(system, L"SystemUsesLightTheme"),
FF_ARG(apps, L"AppsUseLightTheme"),
}, NULL))
{
bool paren = themeOrError->length > 0;
if (paren)
@@ -117,7 +124,7 @@ bool ffDetectWmTheme(FFstrbuf* themeOrError)
if(themeOrError->length == 0)
{
ffStrbufAppendS(themeOrError, "Failed to find current theme");
ffStrbufSetStatic(themeOrError, "Failed to find current theme");
return false;
}
return true;