From 9cd7e368deb44d93d02646cfb12d2c4b4dc1ec5b Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <2546789017@qq.com>
Date: Sat, 14 Nov 2020 16:27:20 +0800
Subject: [PATCH] Minor improvement.
It turns out that the final title bar height contains
the border height as well.
Signed-off-by: Yuhang Zhao <2546789017@qq.com>
---
examples/QMainWindow/TitleBar.ui | 40 ++++++++++++++++-------
examples/Win32Demo/widget.cpp | 2 +-
winnativeeventfilter.cpp | 55 +++++++++++++++++++++-----------
3 files changed, 66 insertions(+), 31 deletions(-)
diff --git a/examples/QMainWindow/TitleBar.ui b/examples/QMainWindow/TitleBar.ui
index a763cec..109bf3e 100644
--- a/examples/QMainWindow/TitleBar.ui
+++ b/examples/QMainWindow/TitleBar.ui
@@ -7,9 +7,27 @@
0
0
423
- 26
+ 31
+
+
+ 0
+ 0
+
+
+
+
+ 0
+ 31
+
+
+
+
+ 16777215
+ 31
+
+
#iconButton, #minimizeButton, #maximizeButton, #closeButton {
background-color: transparent;
@@ -44,7 +62,7 @@
0
- 1
+ 0
0
@@ -127,13 +145,13 @@
45
- 26
+ 31
45
- 26
+ 31
@@ -152,7 +170,7 @@
45
- 30
+ 31
@@ -168,13 +186,13 @@
45
- 26
+ 31
45
- 26
+ 31
@@ -194,7 +212,7 @@
45
- 30
+ 31
@@ -213,13 +231,13 @@
45
- 26
+ 31
45
- 26
+ 31
@@ -238,7 +256,7 @@
45
- 30
+ 31
diff --git a/examples/Win32Demo/widget.cpp b/examples/Win32Demo/widget.cpp
index a8988e9..a3ff285 100644
--- a/examples/Win32Demo/widget.cpp
+++ b/examples/Win32Demo/widget.cpp
@@ -199,7 +199,7 @@ void Widget::setupUi()
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(minimizeButton->sizePolicy().hasHeightForWidth());
minimizeButton->setSizePolicy(sizePolicy1);
- const QSize systemButtonSize = {45, 30};
+ const QSize systemButtonSize = {qRound(titleBarHeight * 1.5), titleBarHeight};
minimizeButton->setMinimumSize(systemButtonSize);
minimizeButton->setMaximumSize(systemButtonSize);
QIcon icon;
diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp
index a58e92d..db42df4 100644
--- a/winnativeeventfilter.cpp
+++ b/winnativeeventfilter.cpp
@@ -2181,6 +2181,7 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
Q_ASSERT(handle);
const auto hwnd = reinterpret_cast(handle);
const qreal dpr = dpiAware ? GetDevicePixelRatioForWindow(hwnd) : m_defaultDevicePixelRatio;
+ int ret = 0;
if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, hwnd)) {
createUserData(hwnd);
const auto userData = reinterpret_cast(
@@ -2189,7 +2190,7 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
case SystemMetric::BorderWidth: {
const int bw = userData->borderWidth;
if (bw > 0) {
- return qRound(bw * dpr);
+ ret = qRound(bw * dpr);
} else {
const int result_nondpi
= WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, SM_CXSIZEFRAME)
@@ -2197,13 +2198,13 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CXSIZEFRAME)
+ GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER);
const int result = dpiAware ? result_dpi : result_nondpi;
- return result > 0 ? result : qRound(m_defaultBorderWidth * dpr);
+ ret = result > 0 ? result : qRound(m_defaultBorderWidth * dpr);
}
- }
+ } break;
case SystemMetric::BorderHeight: {
const int bh = userData->borderHeight;
if (bh > 0) {
- return qRound(bh * dpr);
+ ret = qRound(bh * dpr);
} else {
const int result_nondpi
= WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, SM_CYSIZEFRAME)
@@ -2211,45 +2212,61 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CYSIZEFRAME)
+ GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER);
const int result = dpiAware ? result_dpi : result_nondpi;
- return result > 0 ? result : qRound(m_defaultBorderHeight * dpr);
+ ret = result > 0 ? result : qRound(m_defaultBorderHeight * dpr);
}
- }
+ } break;
case SystemMetric::TitleBarHeight: {
const int tbh = userData->titleBarHeight;
if (tbh > 0) {
- return qRound(tbh * dpr);
+ ret = qRound(tbh * dpr);
} else {
const int result_nondpi = WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics,
0,
SM_CYCAPTION);
const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CYCAPTION);
const int result = dpiAware ? result_dpi : result_nondpi;
- return result > 0 ? result : qRound(m_defaultTitleBarHeight * dpr);
+ ret = result > 0 ? result : qRound(m_defaultTitleBarHeight * dpr);
}
+ } break;
}
- }
+ // When dpr = 1.0 (DPI = 96):
+ // SM_CXSIZEFRAME = SM_CYSIZEFRAME = 4px
+ // SM_CXPADDEDBORDER = 4px
+ // SM_CYCAPTION = 23px
+ // Border Width = Border Height = SM_C(X|Y)SIZEFRAME + SM_CXPADDEDBORDER = 8px
+ // Title Bar Height = Border Height + SM_CYCAPTION = 31px
+ // dpr = 1.25 --> Title Bar Height = 38px
+ // dpr = 1.5 --> Title Bar Height = 45px
+ // dpr = 1.75 --> Title Bar Height = 51px
+ ret += (metric == SystemMetric::TitleBarHeight)
+ ? getSystemMetric(handle, SystemMetric::BorderHeight, dpiAware)
+ : 0;
+ return ret;
}
switch (metric) {
- case SystemMetric::BorderWidth:
+ case SystemMetric::BorderWidth: {
if (coreData()->m_borderWidth > 0) {
- return qRound(coreData()->m_borderWidth * dpr);
+ ret = qRound(coreData()->m_borderWidth * dpr);
} else {
- return qRound(m_defaultBorderWidth * dpr);
+ ret = qRound(m_defaultBorderWidth * dpr);
}
- case SystemMetric::BorderHeight:
+ } break;
+ case SystemMetric::BorderHeight: {
if (coreData()->m_borderHeight > 0) {
- return qRound(coreData()->m_borderHeight * dpr);
+ ret = qRound(coreData()->m_borderHeight * dpr);
} else {
- return qRound(m_defaultBorderHeight * dpr);
+ ret = qRound(m_defaultBorderHeight * dpr);
}
- case SystemMetric::TitleBarHeight:
+ } break;
+ case SystemMetric::TitleBarHeight: {
if (coreData()->m_titleBarHeight > 0) {
- return qRound(coreData()->m_titleBarHeight * dpr);
+ ret = qRound(coreData()->m_titleBarHeight * dpr);
} else {
- return qRound(m_defaultTitleBarHeight * dpr);
+ ret = qRound(m_defaultTitleBarHeight * dpr);
}
+ } break;
}
- return -1;
+ return ret;
}
void WinNativeEventFilter::setWindowGeometry(