Last active
June 7, 2025 12:22
-
-
Save hyrious/c922c818d33d1f090cc658380cc0d1f2 to your computer and use it in GitHub Desktop.
Get Windows scale correctly (like 150%).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma comment(lib, "user32") | |
#pragma comment(lib, "gdi32") | |
#pragma comment(lib, "shcore") | |
#define NOMINMAX | |
#define WIN32_LEAN_AND_MEAN | |
#include <ShellScalingApi.h> | |
#include <Windows.h> | |
#include <cstdio> | |
auto main() -> int { | |
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); | |
HDC hdc = GetDC(HWND_DESKTOP); | |
int dpi = GetDeviceCaps(hdc, LOGPIXELSY); | |
ReleaseDC(HWND_DESKTOP, hdc); | |
float scale = dpi * 1.0f / USER_DEFAULT_SCREEN_DPI; | |
printf("%f\n", scale); | |
} | |
// This file uses #pragma comment(lib), it needs MSVC's cl.exe to compile | |
// cl /nologo win32_scale.cpp | |
// If you want to listen to DPI changes, see the WM_DPICHANGED event | |
// https://learn.microsoft.com/en-us/windows/win32/hidpi/wm-dpichanged |
To enable dark mode, at least for the title bar and the system menu (right click on title bar), without self rendering those things, follow these steps:
// Enable dark mode system menu.
#pragma comment(lib, "uxtheme")
enum class PreferredAppMode {
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};
HMODULE hUxtheme = LoadLibrary("UxTheme.dll");
using TYPE_SetPreferredAppMode = PreferredAppMode(WINAPI *)(PreferredAppMode appMode);
static const TYPE_SetPreferredAppMode SetPreferredAppMode = (TYPE_SetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
SetPreferredAppMode(PreferredAppMode::AllowDark);
// Enable dark title bar.
#pragma comment(lib, "dwmapi")
BOOL enabled = TRUE;
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &enabled, sizeof(enabled));
Reference: https://source.chromium.org/chromium/chromium/src/+/main:base/win/dark_mode_support.cc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To render a window at high dpi, follow these steps: