- 1 year ago
How to detect Windows 10 light/dark mode in Win32 application?
Comment
1 / 1
To detect Windows 10 light/dark mode in a Win32 application, you can use the IsDarkModeEnabled
function from the Windows 10 API. Here's how you can do it:
- Include the necessary Windows headers in your code:
cpp#include <Windows.h>
#include <Uxtheme.h>
#pragma comment(lib, "UxTheme.lib")
- Use the
IsDarkModeEnabled
function to check if dark mode is enabled:
cppbool IsDarkModeEnabled()
{
HMODULE hModule = LoadLibraryW(L"uxtheme.dll");
if (hModule)
{
FARPROC pfnIsDarkModeEnabled = GetProcAddress(hModule, "IsDarkModeEnabled");
if (pfnIsDarkModeEnabled)
{
return ((BOOL(WINAPI*)())pfnIsDarkModeEnabled)();
}
}
// Fallback if the function is not available
return false;
}
- Call the
IsDarkModeEnabled
function in your code to check if dark mode is enabled:
cppint main()
{
bool isDarkMode = IsDarkModeEnabled();
if (isDarkMode)
{
// Dark mode is enabled
// Do something for dark mode
}
else
{
// Light mode is enabled
// Do something for light mode
}
return 0;
}
Note: The IsDarkModeEnabled
function is available starting from Windows 10 version 1809 (October 2018 Update). If you need to support older versions of Windows 10, you may need to use other methods, such as reading the registry or using other Windows API functions. Additionally, keep in mind that using undocumented or unsupported functions may have compatibility and stability risks, so it's essential to test thoroughly and consider the implications for your specific use case.