Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HiDPI support #4

Closed
Stehfyn opened this issue Nov 23, 2024 · 0 comments
Closed

HiDPI support #4

Stehfyn opened this issue Nov 23, 2024 · 0 comments

Comments

@Stehfyn
Copy link
Owner

Stehfyn commented Nov 23, 2024

Windows 10-post some build tries to preempt non-dpi-aware scaling issues for processes on HiDPI monitors, which results in pre-scaled values returned by various win32 functions which are otherwise unexpected by imgui. The solution is relatively simple, but is otherwise not solvable by simply using ImGui_ImplWin32_EnableDpiAwareness(), as on builds Windows10 or higher that function will use SetThreadDpiAwarenessContext() as opposed to SetProcessDpiAwareness()

void ImGui_ImplWin32_EnableDpiAwareness()
{
    // Make sure monitors will be updated with latest correct scaling
    if (ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData())
        bd->WantUpdateMonitors = true;


    if (_IsWindows10OrGreater())
    {
        static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process
        if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext"))
        {
            SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
            return;
        }
    }
    if (_IsWindows8Point1OrGreater())
    {
        static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
        if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
        {
            SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
            return;
        }
    }
#if _WIN32_WINNT >= 0x0600
    ::SetProcessDPIAware();
#endif
}

https://github.com/ocornut/imgui/blob/dad1047b04e335454d17cd546c29a44173da8eb8/backends/imgui_impl_win32.cpp#L930-L957

Using a combination of both SetProcessDpiAwareness() and ImGui_ImplWin32_EnableDpiAwareness() can do the trick, and disable Windows' preemptive scaling for HiDPI monitors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant