diff options
Diffstat (limited to 'src/dutil/dpiutil.cpp')
| -rw-r--r-- | src/dutil/dpiutil.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/dutil/dpiutil.cpp b/src/dutil/dpiutil.cpp index 02a9580c..4096c8d3 100644 --- a/src/dutil/dpiutil.cpp +++ b/src/dutil/dpiutil.cpp | |||
| @@ -18,6 +18,9 @@ | |||
| 18 | static PFN_ADJUSTWINDOWRECTEXFORDPI vpfnAdjustWindowRectExForDpi = NULL; | 18 | static PFN_ADJUSTWINDOWRECTEXFORDPI vpfnAdjustWindowRectExForDpi = NULL; |
| 19 | static PFN_GETDPIFORMONITOR vpfnGetDpiForMonitor = NULL; | 19 | static PFN_GETDPIFORMONITOR vpfnGetDpiForMonitor = NULL; |
| 20 | static PFN_GETDPIFORWINDOW vpfnGetDpiForWindow = NULL; | 20 | static PFN_GETDPIFORWINDOW vpfnGetDpiForWindow = NULL; |
| 21 | static PFN_SETPROCESSDPIAWARE vpfnSetProcessDPIAware = NULL; | ||
| 22 | static PFN_SETPROCESSDPIAWARENESS vpfnSetProcessDpiAwareness = NULL; | ||
| 23 | static PFN_SETPROCESSDPIAWARENESSCONTEXT vpfnSetProcessDpiAwarenessContext = NULL; | ||
| 21 | 24 | ||
| 22 | static HMODULE vhShcoreDll = NULL; | 25 | static HMODULE vhShcoreDll = NULL; |
| 23 | static HMODULE vhUser32Dll = NULL; | 26 | static HMODULE vhUser32Dll = NULL; |
| @@ -32,6 +35,7 @@ DAPI_(void) DpiuInitialize() | |||
| 32 | { | 35 | { |
| 33 | // Ignore failures. | 36 | // Ignore failures. |
| 34 | vpfnGetDpiForMonitor = reinterpret_cast<PFN_GETDPIFORMONITOR>(::GetProcAddress(vhShcoreDll, "GetDpiForMonitor")); | 37 | vpfnGetDpiForMonitor = reinterpret_cast<PFN_GETDPIFORMONITOR>(::GetProcAddress(vhShcoreDll, "GetDpiForMonitor")); |
| 38 | vpfnSetProcessDpiAwareness = reinterpret_cast<PFN_SETPROCESSDPIAWARENESS>(::GetProcAddress(vhShcoreDll, "SetProcessDpiAwareness")); | ||
| 35 | } | 39 | } |
| 36 | 40 | ||
| 37 | hr = LoadSystemLibrary(L"User32.dll", &vhUser32Dll); | 41 | hr = LoadSystemLibrary(L"User32.dll", &vhUser32Dll); |
| @@ -40,6 +44,8 @@ DAPI_(void) DpiuInitialize() | |||
| 40 | // Ignore failures. | 44 | // Ignore failures. |
| 41 | vpfnAdjustWindowRectExForDpi = reinterpret_cast<PFN_ADJUSTWINDOWRECTEXFORDPI>(::GetProcAddress(vhUser32Dll, "AdjustWindowRectExForDpi")); | 45 | vpfnAdjustWindowRectExForDpi = reinterpret_cast<PFN_ADJUSTWINDOWRECTEXFORDPI>(::GetProcAddress(vhUser32Dll, "AdjustWindowRectExForDpi")); |
| 42 | vpfnGetDpiForWindow = reinterpret_cast<PFN_GETDPIFORWINDOW>(::GetProcAddress(vhUser32Dll, "GetDpiForWindow")); | 46 | vpfnGetDpiForWindow = reinterpret_cast<PFN_GETDPIFORWINDOW>(::GetProcAddress(vhUser32Dll, "GetDpiForWindow")); |
| 47 | vpfnSetProcessDPIAware = reinterpret_cast<PFN_SETPROCESSDPIAWARE>(::GetProcAddress(vhUser32Dll, "SetProcessDPIAware")); | ||
| 48 | vpfnSetProcessDpiAwarenessContext = reinterpret_cast<PFN_SETPROCESSDPIAWARENESSCONTEXT>(::GetProcAddress(vhUser32Dll, "SetProcessDpiAwarenessContext")); | ||
| 43 | } | 49 | } |
| 44 | 50 | ||
| 45 | vfDpiuInitialized = TRUE; | 51 | vfDpiuInitialized = TRUE; |
| @@ -194,3 +200,75 @@ DAPI_(int) DpiuScaleValue( | |||
| 194 | { | 200 | { |
| 195 | return ::MulDiv(nDefaultDpiValue, nTargetDpi, USER_DEFAULT_SCREEN_DPI); | 201 | return ::MulDiv(nDefaultDpiValue, nTargetDpi, USER_DEFAULT_SCREEN_DPI); |
| 196 | } | 202 | } |
| 203 | |||
| 204 | DAPI_(HRESULT) DpiuSetProcessDpiAwareness( | ||
| 205 | __in DPIU_AWARENESS supportedAwareness, | ||
| 206 | __in_opt DPIU_AWARENESS* pSelectedAwareness | ||
| 207 | ) | ||
| 208 | { | ||
| 209 | HRESULT hr = S_OK; | ||
| 210 | DPIU_AWARENESS selectedAwareness = DPIU_AWARENESS_NONE; | ||
| 211 | DPI_AWARENESS_CONTEXT awarenessContext = DPI_AWARENESS_CONTEXT_UNAWARE; | ||
| 212 | PROCESS_DPI_AWARENESS awareness = PROCESS_DPI_UNAWARE; | ||
| 213 | |||
| 214 | if (vpfnSetProcessDpiAwarenessContext) | ||
| 215 | { | ||
| 216 | if (DPIU_AWARENESS_PERMONITORV2 & supportedAwareness) | ||
| 217 | { | ||
| 218 | awarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2; | ||
| 219 | selectedAwareness = DPIU_AWARENESS_PERMONITORV2; | ||
| 220 | } | ||
| 221 | else if (DPIU_AWARENESS_PERMONITOR & supportedAwareness) | ||
| 222 | { | ||
| 223 | awarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE; | ||
| 224 | selectedAwareness = DPIU_AWARENESS_PERMONITOR; | ||
| 225 | } | ||
| 226 | else if (DPIU_AWARENESS_SYSTEM & supportedAwareness) | ||
| 227 | { | ||
| 228 | awarenessContext = DPI_AWARENESS_CONTEXT_SYSTEM_AWARE; | ||
| 229 | selectedAwareness = DPIU_AWARENESS_SYSTEM; | ||
| 230 | } | ||
| 231 | else if (DPIU_AWARENESS_GDISCALED & supportedAwareness) | ||
| 232 | { | ||
| 233 | awarenessContext = DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED; | ||
| 234 | selectedAwareness = DPIU_AWARENESS_GDISCALED; | ||
| 235 | } | ||
| 236 | |||
| 237 | if (!vpfnSetProcessDpiAwarenessContext(awarenessContext)) | ||
| 238 | { | ||
| 239 | DpiuExitOnLastError(hr, "Failed to set process DPI awareness context."); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | else if (vpfnSetProcessDpiAwareness) | ||
| 243 | { | ||
| 244 | if (DPIU_AWARENESS_PERMONITOR & supportedAwareness) | ||
| 245 | { | ||
| 246 | awareness = PROCESS_PER_MONITOR_DPI_AWARE; | ||
| 247 | selectedAwareness = DPIU_AWARENESS_PERMONITOR; | ||
| 248 | } | ||
| 249 | else if (DPIU_AWARENESS_SYSTEM & supportedAwareness) | ||
| 250 | { | ||
| 251 | awareness = PROCESS_SYSTEM_DPI_AWARE; | ||
| 252 | selectedAwareness = DPIU_AWARENESS_SYSTEM; | ||
| 253 | } | ||
| 254 | |||
| 255 | hr = vpfnSetProcessDpiAwareness(awareness); | ||
| 256 | DpiuExitOnFailure(hr, "Failed to set process DPI awareness."); | ||
| 257 | } | ||
| 258 | else if (vpfnSetProcessDPIAware && (DPIU_AWARENESS_SYSTEM & supportedAwareness)) | ||
| 259 | { | ||
| 260 | selectedAwareness = DPIU_AWARENESS_SYSTEM; | ||
| 261 | if (!vpfnSetProcessDPIAware()) | ||
| 262 | { | ||
| 263 | DpiuExitOnLastError(hr, "Failed to set process DPI aware."); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | LExit: | ||
| 268 | if (pSelectedAwareness) | ||
| 269 | { | ||
| 270 | *pSelectedAwareness = selectedAwareness; | ||
| 271 | } | ||
| 272 | |||
| 273 | return hr; | ||
| 274 | } | ||
