From 6b2d31247dc2c2804e571b31a71c8a423c1db9d4 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周二, 26 12月 2023 01:44:03 +0800
Subject: [PATCH] Totally fix top border issue on Win10 for QtWidgets

---
 src/core/shared/qwkwindowsextra_p.h |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/src/core/shared/qwkwindowsextra_p.h b/src/core/shared/qwkwindowsextra_p.h
index 15fe4e7..8ada487 100644
--- a/src/core/shared/qwkwindowsextra_p.h
+++ b/src/core/shared/qwkwindowsextra_p.h
@@ -441,6 +441,55 @@
 #endif
     }
 
+    static inline quint32 getDpiForWindow(HWND hwnd) {
+        const DynamicApis &apis = DynamicApis::instance();
+        if (apis.pGetDpiForWindow) {         // Win10
+            return apis.pGetDpiForWindow(hwnd);
+        } else if (apis.pGetDpiForMonitor) { // Win8.1
+            HMONITOR monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
+            UINT dpiX{0};
+            UINT dpiY{0};
+            apis.pGetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
+            return dpiX;
+        } else { // Win2K
+            HDC hdc = ::GetDC(nullptr);
+            const int dpiX = ::GetDeviceCaps(hdc, LOGPIXELSX);
+            // const int dpiY = ::GetDeviceCaps(hdc, LOGPIXELSY);
+            ::ReleaseDC(nullptr, hdc);
+            return quint32(dpiX);
+        }
+    }
+
+    static inline quint32 getSystemMetricsForDpi(int index, quint32 dpi) {
+        const DynamicApis &apis = DynamicApis::instance();
+        if (apis.pGetSystemMetricsForDpi) {
+            return ::GetSystemMetricsForDpi(index, dpi);
+        }
+        return ::GetSystemMetrics(index);
+    }
+
+    static inline quint32 getWindowFrameBorderThickness(HWND hwnd) {
+        const DynamicApis &apis = DynamicApis::instance();
+        if (UINT result = 0; SUCCEEDED(apis.pDwmGetWindowAttribute(
+                hwnd, _DWMWA_VISIBLE_FRAME_BORDER_THICKNESS, &result, sizeof(result)))) {
+            return result;
+        }
+        return getSystemMetricsForDpi(SM_CXBORDER, getDpiForWindow(hwnd));
+    }
+
+    static inline quint32 getResizeBorderThickness(HWND hwnd) {
+        const quint32 dpi = getDpiForWindow(hwnd);
+        return getSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) +
+               getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
+    }
+
+    static inline quint32 getTitleBarHeight(HWND hwnd) {
+        const quint32 dpi = getDpiForWindow(hwnd);
+        return getSystemMetricsForDpi(SM_CYCAPTION, dpi) +
+               getSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) +
+               getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
+    }
+
 }
 
 #endif // QWKWINDOWSEXTRA_P_H

--
Gitblit v1.9.1