Fix system metric is double scaled

Fixes: #36

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-11-14 20:42:16 +08:00
parent 14556d8b2d
commit 9cb3392ce4
2 changed files with 30 additions and 31 deletions

View File

@ -144,13 +144,13 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
@ -169,7 +169,7 @@
</property> </property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
@ -185,13 +185,13 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
@ -211,7 +211,7 @@
</property> </property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
@ -230,13 +230,13 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>
@ -255,7 +255,7 @@
</property> </property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>45</width> <width>46</width>
<height>31</height> <height>31</height>
</size> </size>
</property> </property>

View File

@ -1046,20 +1046,23 @@ void UpdateFrameMarginsForWindow(const HWND handle, const bool resetToDefault =
} }
} }
int GetSystemMetricsForWindow(const HWND handle, const int index) int GetSystemMetricsForWindow(const HWND handle, const int index, const bool dpiAware = false)
{ {
Q_ASSERT(handle); Q_ASSERT(handle);
if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, handle)) { if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, handle)) {
if (coreData()->m_lpGetSystemMetricsForDpi) { if (coreData()->m_lpGetSystemMetricsForDpi) {
return coreData()->m_lpGetSystemMetricsForDpi(index, const UINT dpi = dpiAware ? qRound(GetPreferedNumber(GetDotsPerInchForWindow(handle)))
static_cast<UINT>(qRound(GetPreferedNumber( : m_defaultDotsPerInch;
GetDotsPerInchForWindow(handle))))); return coreData()->m_lpGetSystemMetricsForDpi(index, dpi);
} else { } else {
return qRound(WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, index) // Although Microsoft claims that GetSystemMetrics() is not DPI
* GetDevicePixelRatioForWindow(handle)); // aware, it still returns a scaled value on Win7, Win8.1 and
// Win10.
const qreal dpr = dpiAware ? 1.0 : GetDevicePixelRatioForWindow(handle);
return qRound(WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, index) / dpr);
} }
} }
return -1; return 0;
} }
void createUserData(const HWND handle, const WinNativeEventFilter::WINDOWDATA *data = nullptr) void createUserData(const HWND handle, const WinNativeEventFilter::WINDOWDATA *data = nullptr)
@ -1202,7 +1205,7 @@ void install()
// The standard values of border width, border height and title bar height // The standard values of border width, border height and title bar height
// when DPI is 96. // when DPI is 96.
const int m_defaultBorderWidth = 8, m_defaultBorderHeight = 8, m_defaultTitleBarHeight = 30; const int m_defaultBorderWidth = 8, m_defaultBorderHeight = 8, m_defaultTitleBarHeight = 31;
// The thickness of an auto-hide taskbar in pixels. // The thickness of an auto-hide taskbar in pixels.
const int kAutoHideTaskbarThicknessPx = 2; const int kAutoHideTaskbarThicknessPx = 2;
@ -2192,11 +2195,10 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
if (bw > 0) { if (bw > 0) {
ret = qRound(bw * dpr); ret = qRound(bw * dpr);
} else { } else {
const int result_nondpi const int result_nondpi = GetSystemMetricsForWindow(hwnd, SM_CXSIZEFRAME)
= WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, SM_CXSIZEFRAME) + GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER);
+ WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, SM_CXPADDEDBORDER); const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CXSIZEFRAME, true)
const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CXSIZEFRAME) + GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER, true);
+ GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER);
const int result = dpiAware ? result_dpi : result_nondpi; const int result = dpiAware ? result_dpi : result_nondpi;
ret = result > 0 ? result : qRound(m_defaultBorderWidth * dpr); ret = result > 0 ? result : qRound(m_defaultBorderWidth * dpr);
} }
@ -2206,11 +2208,10 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
if (bh > 0) { if (bh > 0) {
ret = qRound(bh * dpr); ret = qRound(bh * dpr);
} else { } else {
const int result_nondpi const int result_nondpi = GetSystemMetricsForWindow(hwnd, SM_CYSIZEFRAME)
= WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, SM_CYSIZEFRAME) + GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER);
+ WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, 0, SM_CXPADDEDBORDER); const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CYSIZEFRAME, true)
const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CYSIZEFRAME) + GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER, true);
+ GetSystemMetricsForWindow(hwnd, SM_CXPADDEDBORDER);
const int result = dpiAware ? result_dpi : result_nondpi; const int result = dpiAware ? result_dpi : result_nondpi;
ret = result > 0 ? result : qRound(m_defaultBorderHeight * dpr); ret = result > 0 ? result : qRound(m_defaultBorderHeight * dpr);
} }
@ -2220,10 +2221,8 @@ int WinNativeEventFilter::getSystemMetric(void *handle,
if (tbh > 0) { if (tbh > 0) {
ret = qRound(tbh * dpr); ret = qRound(tbh * dpr);
} else { } else {
const int result_nondpi = WNEF_EXECUTE_WINAPI_RETURN(GetSystemMetrics, const int result_nondpi = GetSystemMetricsForWindow(hwnd, SM_CYCAPTION);
0, const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CYCAPTION, true);
SM_CYCAPTION);
const int result_dpi = GetSystemMetricsForWindow(hwnd, SM_CYCAPTION);
const int result = dpiAware ? result_dpi : result_nondpi; const int result = dpiAware ? result_dpi : result_nondpi;
ret = result > 0 ? result : qRound(m_defaultTitleBarHeight * dpr); ret = result > 0 ? result : qRound(m_defaultTitleBarHeight * dpr);
} }