From 9f895c3f14e7420cbbf354168f5ae48488ab1060 Mon Sep 17 00:00:00 2001 From: gibbed Date: Thu, 22 Nov 2018 08:30:30 -0600 Subject: [PATCH] [UI] Use DPI APIs when available. --- src/xenia/ui/window_win.cc | 21 +++++++++++++++++---- src/xenia/ui/window_win.h | 3 +++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/xenia/ui/window_win.cc b/src/xenia/ui/window_win.cc index 50216fc57..9941cb221 100644 --- a/src/xenia/ui/window_win.cc +++ b/src/xenia/ui/window_win.cc @@ -47,12 +47,20 @@ bool Win32Window::Initialize() { return OnCreate(); } bool Win32Window::OnCreate() { HINSTANCE hInstance = GetModuleHandle(nullptr); + if (!SetProcessDpiAwareness_ || !GetDpiForMonitor_) { + auto shcore = GetModuleHandle(L"shcore.dll"); + if (shcore) { + SetProcessDpiAwareness_ = + GetProcAddress(shcore, "SetProcessDpiAwareness"); + GetDpiForMonitor_ = GetProcAddress(shcore, "GetDpiForMonitor"); + } + } + static bool has_registered_class = false; if (!has_registered_class) { // Tell Windows that we're DPI aware. - auto spda = (decltype(&SetProcessDpiAwareness))GetProcAddress( - GetModuleHandle(L"shcore.dll"), "SetProcessDpiAwareness"); - if (spda) { + if (SetProcessDpiAwareness_) { + auto spda = (decltype(&SetProcessDpiAwareness))SetProcessDpiAwareness_; auto res = spda(PROCESS_PER_MONITOR_DPI_AWARE); if (res != S_OK) { XELOGE("Failed to set process DPI awareness. (code = 0x%.8X)", res); @@ -303,11 +311,16 @@ void Win32Window::set_bordered(bool enabled) { } int Win32Window::get_dpi() const { + if (!GetDpiForMonitor_) { + return 96; + } + HMONITOR monitor = MonitorFromWindow(hwnd_, MONITOR_DEFAULTTOPRIMARY); // According to msdn, x and y are identical... UINT dpi_x, dpi_y; - GetDpiForMonitor(monitor, MDT_DEFAULT, &dpi_x, &dpi_y); + auto gdfm = (decltype(&GetDpiForMonitor))GetDpiForMonitor_; + gdfm(monitor, MDT_DEFAULT, &dpi_x, &dpi_y); return dpi_x; } diff --git a/src/xenia/ui/window_win.h b/src/xenia/ui/window_win.h index 1c6373124..d885042ea 100644 --- a/src/xenia/ui/window_win.h +++ b/src/xenia/ui/window_win.h @@ -85,6 +85,9 @@ class Win32Window : public Window { WINDOWPLACEMENT windowed_pos_ = {0}; POINT last_mouse_pos_ = {0}; + + void* SetProcessDpiAwareness_ = nullptr; + void* GetDpiForMonitor_ = nullptr; }; class Win32MenuItem : public MenuItem {