[UI] Use DPI APIs when available.

This commit is contained in:
gibbed 2018-11-22 08:30:30 -06:00
parent 5c473a5203
commit 9f895c3f14
2 changed files with 20 additions and 4 deletions

View File

@ -47,12 +47,20 @@ bool Win32Window::Initialize() { return OnCreate(); }
bool Win32Window::OnCreate() { bool Win32Window::OnCreate() {
HINSTANCE hInstance = GetModuleHandle(nullptr); 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; static bool has_registered_class = false;
if (!has_registered_class) { if (!has_registered_class) {
// Tell Windows that we're DPI aware. // Tell Windows that we're DPI aware.
auto spda = (decltype(&SetProcessDpiAwareness))GetProcAddress( if (SetProcessDpiAwareness_) {
GetModuleHandle(L"shcore.dll"), "SetProcessDpiAwareness"); auto spda = (decltype(&SetProcessDpiAwareness))SetProcessDpiAwareness_;
if (spda) {
auto res = spda(PROCESS_PER_MONITOR_DPI_AWARE); auto res = spda(PROCESS_PER_MONITOR_DPI_AWARE);
if (res != S_OK) { if (res != S_OK) {
XELOGE("Failed to set process DPI awareness. (code = 0x%.8X)", res); 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 { int Win32Window::get_dpi() const {
if (!GetDpiForMonitor_) {
return 96;
}
HMONITOR monitor = MonitorFromWindow(hwnd_, MONITOR_DEFAULTTOPRIMARY); HMONITOR monitor = MonitorFromWindow(hwnd_, MONITOR_DEFAULTTOPRIMARY);
// According to msdn, x and y are identical... // According to msdn, x and y are identical...
UINT dpi_x, dpi_y; 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; return dpi_x;
} }

View File

@ -85,6 +85,9 @@ class Win32Window : public Window {
WINDOWPLACEMENT windowed_pos_ = {0}; WINDOWPLACEMENT windowed_pos_ = {0};
POINT last_mouse_pos_ = {0}; POINT last_mouse_pos_ = {0};
void* SetProcessDpiAwareness_ = nullptr;
void* GetDpiForMonitor_ = nullptr;
}; };
class Win32MenuItem : public MenuItem { class Win32MenuItem : public MenuItem {