From 42977738aba59150109fea3e9edd2b74643c1000 Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <zhaoyuhang@rankyee.com>
Date: 周一, 09 12月 2024 11:47:44 +0800
Subject: [PATCH] Minor improvements of Win32 code

---
 src/core/shared/qwkwindowsextra_p.h |   20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/core/shared/qwkwindowsextra_p.h b/src/core/shared/qwkwindowsextra_p.h
index f7be35e..036fe79 100644
--- a/src/core/shared/qwkwindowsextra_p.h
+++ b/src/core/shared/qwkwindowsextra_p.h
@@ -429,7 +429,14 @@
         if (apis.pGetSystemMetricsForDpi) {
             return apis.pGetSystemMetricsForDpi(index, dpi);
         }
-        return ::GetSystemMetrics(index);
+        const int result = ::GetSystemMetrics(index);
+        // GetSystemMetrics() always give you scaled value.
+        if (dpi != USER_DEFAULT_SCREEN_DPI) {
+            return result;
+        }
+        const qreal dpr = qreal(dpi) / qreal(USER_DEFAULT_SCREEN_DPI);
+        // ### Not sure how Windows itself rounds non-integer value.
+        return qFloor(qreal(result) * dpr);
     }
 
     inline quint32 getWindowFrameBorderThickness(HWND hwnd) {
@@ -442,19 +449,28 @@
             }
         }
         if (isWin10OrGreater()) {
-            return static_cast<quint32>(qreal(1) * qreal(getDpiForWindow(hwnd)) / qreal(USER_DEFAULT_SCREEN_DPI));
+            const quint32 dpi = getDpiForWindow(hwnd);
+            // When DPI is 96, it should be 1px.
+            return getSystemMetricsForDpi(SM_CXBORDER, dpi);
         }
+        // There's no such thing (a visible frame border line) before Win10.
         return 0;
     }
 
     inline quint32 getResizeBorderThickness(HWND hwnd) {
         const quint32 dpi = getDpiForWindow(hwnd);
+        // When DPI is 96, SM_CXSIZEFRAME is 4px, SM_CXPADDEDBORDER is also 4px,
+        // so the result should be 8px. This result won't be affected by OS version,
+        // it's 8px in Win7, and so in Win11.
         return getSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) +
                getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
     }
 
     inline quint32 getTitleBarHeight(HWND hwnd) {
         const quint32 dpi = getDpiForWindow(hwnd);
+        // When DPI is 96, SM_CYCAPTION is 23px, so the result should be 31px.
+        // However, according to latest MS design manual, the title bar height
+        // should be 32px, maybe there's some rounding issue.
         return getSystemMetricsForDpi(SM_CYCAPTION, dpi) +
                getSystemMetricsForDpi(SM_CYSIZEFRAME, dpi) +
                getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);

--
Gitblit v1.9.1