From 9fac39c5e09cc6157fe77dca8810b329ad8e43f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Blissing?= Date: Thu, 30 Nov 2017 13:00:54 +0100 Subject: [PATCH] Applications declared DPI-aware in the Windows environment Applications that run on a Windows computer with desktop scaling enabled gets scaled incorrectly since windows assumes that applications are DPI-unaware unless declared otherwise. This change declares the application DPI-aware, thus not automatically scaled by the operating system. The corresponding library call requires Windows 8.1 or later. --- src/osgViewer/GraphicsWindowWin32.cpp | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/osgViewer/GraphicsWindowWin32.cpp b/src/osgViewer/GraphicsWindowWin32.cpp index b1cb6ce9d..6905a8856 100644 --- a/src/osgViewer/GraphicsWindowWin32.cpp +++ b/src/osgViewer/GraphicsWindowWin32.cpp @@ -144,6 +144,26 @@ static CloseTouchInputHandleFunc *closeTouchInputHandleFunc = NULL; static GetTouchInputInfoFunc *getTouchInputInfoFunc = NULL; static GetPointerTypeFunc *getPointerTypeFunc = NULL; +// DPI Awareness +#if(WINVER >= 0x0603) + +#ifndef DPI_ENUMS_DECLARED + +typedef enum PROCESS_DPI_AWARENESS { + PROCESS_DPI_UNAWARE = 0, + PROCESS_SYSTEM_DPI_AWARE = 1, + PROCESS_PER_MONITOR_DPI_AWARE = 2 +} PROCESS_DPI_AWARENESS; + +#endif // DPI_ENUMS_DECLARED + +typedef +BOOL +(WINAPI SetProcessDpiAwarenessFunc( + PROCESS_DPI_AWARENESS dpi_awareness)); + +static SetProcessDpiAwarenessFunc *setProcessDpiAwareness = NULL; +#endif @@ -765,6 +785,21 @@ Win32WindowingSystem::Win32WindowingSystem() FreeLibrary( hModule); } } + + +#if(WINVER >= 0x0603) + // For Windows 8.1 and higher + // + // Per monitor DPI aware.This app checks for the DPI when it is created and adjusts the scale factor + // whenever the DPI changes.These applications are not automatically scaled by the system. + HMODULE hModuleShore = LoadLibrary("Shcore"); + if (hModuleShore) { + setProcessDpiAwareness = (SetProcessDpiAwarenessFunc *) GetProcAddress(hModuleShore, "SetProcessDpiAwareness"); + if (setProcessDpiAwareness) { + (*setProcessDpiAwareness)(PROCESS_DPI_AWARENESS::PROCESS_PER_MONITOR_DPI_AWARE); + } + } +#endif } Win32WindowingSystem::~Win32WindowingSystem()