Skip to content

Instantly share code, notes, and snippets.

@hyrious
Last active June 7, 2025 12:22
Show Gist options
  • Select an option

  • Save hyrious/c922c818d33d1f090cc658380cc0d1f2 to your computer and use it in GitHub Desktop.

Select an option

Save hyrious/c922c818d33d1f090cc658380cc0d1f2 to your computer and use it in GitHub Desktop.

Revisions

  1. hyrious revised this gist Jun 7, 2025. 1 changed file with 14 additions and 36 deletions.
    50 changes: 14 additions & 36 deletions win32_scale.cpp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    #pragma comment(lib, "user32")
    #pragma comment(lib, "gdi32")
    #pragma comment(lib, "shcore")

    #define NOMINMAX
    #define WIN32_LEAN_AND_MEAN
    #include <ShellScalingApi.h>
    @@ -6,40 +10,14 @@

    auto main() -> int {
    SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
    EnumDisplayMonitors(
    nullptr, nullptr,
    [](HMONITOR mon, HDC /*hdc*/, LPRECT /*rect*/, LPARAM /*acc*/) {
    // DEVICE_SCALE_FACTOR scale = DEVICE_SCALE_FACTOR_INVALID;
    // if (SUCCEEDED(GetScaleFactorForMonitor(mon, &scale))) {
    // printf("%d\n", scale); // incorrect :/
    // }

    float factor = 1.0F;
    UINT dpi_x = 0;
    UINT dpi_y = 0;
    if (SUCCEEDED(GetDpiForMonitor(mon, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y)) && dpi_x) {
    factor = dpi_x / 96.0F;
    }

    MONITORINFOEX info = {};
    info.cbSize = sizeof(MONITORINFOEX);
    if (!GetMonitorInfo(mon, &info)) {
    printf("Failed to get monitor info\n");
    return TRUE;
    }

    DEVMODE devmode = {};
    devmode.dmSize = sizeof(DEVMODE);
    if (!EnumDisplaySettings(info.szDevice, ENUM_CURRENT_SETTINGS, &devmode)) {
    printf("Failed to get display settings\n");
    return TRUE;
    }
    printf("[%s] %lux%lu scale = %d%%\n",
    info.szDevice,
    devmode.dmPelsWidth, devmode.dmPelsHeight,
    static_cast<int>(factor * 100));

    return TRUE;
    },
    0);
    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
  2. hyrious created this gist May 31, 2025.
    45 changes: 45 additions & 0 deletions win32_scale.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #define NOMINMAX
    #define WIN32_LEAN_AND_MEAN
    #include <ShellScalingApi.h>
    #include <Windows.h>
    #include <cstdio>

    auto main() -> int {
    SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
    EnumDisplayMonitors(
    nullptr, nullptr,
    [](HMONITOR mon, HDC /*hdc*/, LPRECT /*rect*/, LPARAM /*acc*/) {
    // DEVICE_SCALE_FACTOR scale = DEVICE_SCALE_FACTOR_INVALID;
    // if (SUCCEEDED(GetScaleFactorForMonitor(mon, &scale))) {
    // printf("%d\n", scale); // incorrect :/
    // }

    float factor = 1.0F;
    UINT dpi_x = 0;
    UINT dpi_y = 0;
    if (SUCCEEDED(GetDpiForMonitor(mon, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y)) && dpi_x) {
    factor = dpi_x / 96.0F;
    }

    MONITORINFOEX info = {};
    info.cbSize = sizeof(MONITORINFOEX);
    if (!GetMonitorInfo(mon, &info)) {
    printf("Failed to get monitor info\n");
    return TRUE;
    }

    DEVMODE devmode = {};
    devmode.dmSize = sizeof(DEVMODE);
    if (!EnumDisplaySettings(info.szDevice, ENUM_CURRENT_SETTINGS, &devmode)) {
    printf("Failed to get display settings\n");
    return TRUE;
    }
    printf("[%s] %lux%lu scale = %d%%\n",
    info.szDevice,
    devmode.dmPelsWidth, devmode.dmPelsHeight,
    static_cast<int>(factor * 100));

    return TRUE;
    },
    0);
    }