From 10fca0e8b1ec42785678f8d2d9a018917b0a620f Mon Sep 17 00:00:00 2001 From: Soph <236479497+LunarInvasion@users.noreply.github.com> Date: Thu, 20 Nov 2025 04:30:44 +0000 Subject: [PATCH] Examples: DPI awareness on Win32 OpenGL --- backends/imgui_impl_win32.cpp | 8 ++-- .../example_win32_opengl3.vcxproj | 38 ++++++++++++++++--- examples/example_win32_opengl3/main.cpp | 4 +- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/backends/imgui_impl_win32.cpp b/backends/imgui_impl_win32.cpp index dc7e66f02..df29090b0 100644 --- a/backends/imgui_impl_win32.cpp +++ b/backends/imgui_impl_win32.cpp @@ -858,17 +858,17 @@ DECLARE_HANDLE(DPI_AWARENESS_CONTEXT); #endif typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+ typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+ -typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update) - +typedef BOOL(WINAPI* PFN_SetProcessDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update) // Helper function to enable DPI awareness without setting up a manifest void ImGui_ImplWin32_EnableDpiAwareness() { if (_IsWindows10OrGreater()) { static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process - if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext")) + if (PFN_SetProcessDpiAwarenessContext SetProcessDpiAwarenessContext = (PFN_SetProcessDpiAwarenessContext)::GetProcAddress(user32_dll, "SetProcessDpiAwarenessContext")) { - SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiawarenesscontext + SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); return; } } diff --git a/examples/example_win32_opengl3/example_win32_opengl3.vcxproj b/examples/example_win32_opengl3/example_win32_opengl3.vcxproj index 98fc38fd3..711ad9bff 100644 --- a/examples/example_win32_opengl3/example_win32_opengl3.vcxproj +++ b/examples/example_win32_opengl3/example_win32_opengl3.vcxproj @@ -21,34 +21,34 @@ {C624E5FF-D4FE-4D35-9164-B8A91864F98E} example_win32_opengl2 - 8.1 + 10.0 Application true Unicode - v140 + v143 Application true Unicode - v140 + v143 Application false true Unicode - v140 + v143 Application false true Unicode - v140 + v143 @@ -69,18 +69,22 @@ $(ProjectDir)$(Configuration)\ $(ProjectDir)$(Configuration)\ + true $(ProjectDir)$(Configuration)\ $(ProjectDir)$(Configuration)\ + true $(ProjectDir)$(Configuration)\ $(ProjectDir)$(Configuration)\ + true $(ProjectDir)$(Configuration)\ $(ProjectDir)$(Configuration)\ + true @@ -95,6 +99,9 @@ opengl32.lib;%(AdditionalDependencies) Console + + PerMonitorHighDPIAware + @@ -109,6 +116,9 @@ opengl32.lib;%(AdditionalDependencies) Console + + PerMonitorHighDPIAware + @@ -128,6 +138,15 @@ opengl32.lib;%(AdditionalDependencies) Console + + PerMonitorHighDPIAware + + + + + + + @@ -147,6 +166,15 @@ opengl32.lib;%(AdditionalDependencies) Console + + PerMonitorHighDPIAware + + + + + + + diff --git a/examples/example_win32_opengl3/main.cpp b/examples/example_win32_opengl3/main.cpp index 9ad74d61f..169e56437 100644 --- a/examples/example_win32_opengl3/main.cpp +++ b/examples/example_win32_opengl3/main.cpp @@ -30,14 +30,14 @@ static int g_Height; // Forward declarations of helper functions bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data); void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data); -void ResetDeviceWGL(); LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); // Main code int main(int, char**) { // Make process DPI aware and obtain main monitor scale - //ImGui_ImplWin32_EnableDpiAwareness(); // FIXME: This somehow doesn't work in the Win32+OpenGL example. Why? + // https://learn.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process + // ImGui_ImplWin32_EnableDpiAwareness(); // This is already set in the manifest so the call will fail, but leaving it for completeness. float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY)); // Create application window