|
| 1 | +#include "dpi_utils.h" |
| 2 | + |
| 3 | +namespace flutter { |
| 4 | + |
| 5 | +namespace { |
| 6 | + |
| 7 | +constexpr UINT kDefaultDpi = 96; |
| 8 | + |
| 9 | +// This is the MDT_EFFECTIVE_DPI value from MONITOR_DPI_TYPE, an enum declared |
| 10 | +// in ShellScalingApi.h. Replicating here to avoid importing the library |
| 11 | +// directly. |
| 12 | +constexpr UINT kEffectiveDpiMonitorType = 0; |
| 13 | + |
| 14 | +template <typename T> |
| 15 | + |
| 16 | +/// Retrieves a function |name| from a given |comBaseModule| into |outProc|. |
| 17 | +/// Returns a bool indicating whether the function was found. |
| 18 | +bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) { |
| 19 | + outProc = reinterpret_cast<T*>(GetProcAddress(comBaseModule, name)); |
| 20 | + return *outProc != nullptr; |
| 21 | +} |
| 22 | + |
| 23 | +/// A helper class for abstracting various Windows DPI related functions across |
| 24 | +/// Windows OS versions. |
| 25 | +class Win32DpiHelper { |
| 26 | + public: |
| 27 | + Win32DpiHelper(); |
| 28 | + |
| 29 | + ~Win32DpiHelper(); |
| 30 | + |
| 31 | + /// Returns the DPI for |hwnd|. Supports all DPI awareness modes, and is |
| 32 | + /// backward compatible down to Windows Vista. If |hwnd| is nullptr, returns |
| 33 | + /// the DPI for the primary monitor. If Per-Monitor DPI awareness is not |
| 34 | + /// available, returns the system's DPI. |
| 35 | + UINT GetDpiForWindow(HWND); |
| 36 | + |
| 37 | + /// Returns the DPI of a given monitor. Defaults to 96 if the API is not |
| 38 | + /// available. |
| 39 | + UINT GetDpiForMonitor(HMONITOR); |
| 40 | + |
| 41 | + private: |
| 42 | + using GetDpiForWindow_ = UINT __stdcall(HWND); |
| 43 | + using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, |
| 44 | + UINT dpiType, |
| 45 | + UINT* dpiX, |
| 46 | + UINT* dpiY); |
| 47 | + using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); |
| 48 | + |
| 49 | + GetDpiForWindow_* get_dpi_for_window_ = nullptr; |
| 50 | + GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; |
| 51 | + EnableNonClientDpiScaling_* enable_non_client_dpi_scaling_ = nullptr; |
| 52 | + |
| 53 | + HMODULE user32_module_ = nullptr; |
| 54 | + HMODULE shlib_module_ = nullptr; |
| 55 | + bool dpi_for_window_supported_ = false; |
| 56 | + bool dpi_for_monitor_supported_ = false; |
| 57 | +}; |
| 58 | + |
| 59 | +Win32DpiHelper::Win32DpiHelper() { |
| 60 | + if ((user32_module_ = LoadLibraryA("User32.dll")) != nullptr) { |
| 61 | + dpi_for_window_supported_ = (AssignProcAddress( |
| 62 | + user32_module_, "GetDpiForWindow", get_dpi_for_window_)); |
| 63 | + } |
| 64 | + if ((shlib_module_ = LoadLibraryA("Shcore.dll")) != nullptr) { |
| 65 | + dpi_for_monitor_supported_ = AssignProcAddress( |
| 66 | + shlib_module_, "GetDpiForMonitor", get_dpi_for_monitor_); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +Win32DpiHelper::~Win32DpiHelper() { |
| 71 | + if (user32_module_ != nullptr) { |
| 72 | + FreeLibrary(user32_module_); |
| 73 | + } |
| 74 | + if (shlib_module_ != nullptr) { |
| 75 | + FreeLibrary(shlib_module_); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { |
| 80 | + // GetDpiForWindow returns the DPI for any awareness mode. If not available, |
| 81 | + // or no |hwnd| is provided, fallback to a per monitor, system, or default |
| 82 | + // DPI. |
| 83 | + if (dpi_for_window_supported_ && hwnd != nullptr) { |
| 84 | + return get_dpi_for_window_(hwnd); |
| 85 | + } |
| 86 | + |
| 87 | + if (dpi_for_monitor_supported_) { |
| 88 | + HMONITOR monitor = nullptr; |
| 89 | + if (hwnd != nullptr) { |
| 90 | + monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); |
| 91 | + } |
| 92 | + return GetDpiForMonitor(monitor); |
| 93 | + } |
| 94 | + HDC hdc = GetDC(hwnd); |
| 95 | + UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); |
| 96 | + ReleaseDC(hwnd, hdc); |
| 97 | + return dpi; |
| 98 | +} |
| 99 | + |
| 100 | +UINT Win32DpiHelper::GetDpiForMonitor(HMONITOR monitor) { |
| 101 | + if (dpi_for_monitor_supported_) { |
| 102 | + if (monitor == nullptr) { |
| 103 | + const POINT target_point = {0, 0}; |
| 104 | + monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); |
| 105 | + } |
| 106 | + UINT dpi_x = 0, dpi_y = 0; |
| 107 | + HRESULT result = |
| 108 | + get_dpi_for_monitor_(monitor, kEffectiveDpiMonitorType, &dpi_x, &dpi_y); |
| 109 | + if (SUCCEEDED(result)) { |
| 110 | + return dpi_x; |
| 111 | + } |
| 112 | + } |
| 113 | + return kDefaultDpi; |
| 114 | +} // namespace |
| 115 | + |
| 116 | +Win32DpiHelper* GetHelper() { |
| 117 | + static Win32DpiHelper* dpi_helper = new Win32DpiHelper(); |
| 118 | + return dpi_helper; |
| 119 | +} |
| 120 | +} // namespace |
| 121 | + |
| 122 | +UINT GetDpiForHWND(HWND hwnd) { |
| 123 | + return GetHelper()->GetDpiForWindow(hwnd); |
| 124 | +} |
| 125 | + |
| 126 | +UINT GetDpiForMonitor(HMONITOR monitor) { |
| 127 | + return GetHelper()->GetDpiForMonitor(monitor); |
| 128 | +} |
| 129 | +} // namespace flutter |
0 commit comments