From bd617ef9eff0848a7a1a781217cde23c0bf752b7 Mon Sep 17 00:00:00 2001 From: mingkuang Date: Sun, 30 Jun 2024 18:15:48 +0800 Subject: [PATCH] =?UTF-8?q?Bug=EF=BC=8CGetSystemMetricsForDpi=E9=80=82?= =?UTF-8?q?=E9=85=8D=E7=B3=BB=E7=BB=9F=EF=BC=8CDPI=E4=BC=A0=E9=80=920?= =?UTF-8?q?=E6=97=B6=E8=BF=94=E5=9B=9E=E5=A4=B1=E8=B4=A5=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E4=BF=AE=E6=AD=A3SM=5FCYCAPTION=E3=80=81SM=5FCYMENU?= =?UTF-8?q?=E3=80=81SM=5FCYSMCAPTION=E3=80=81SM=5FCXFRAME=E3=80=81SM=5FCYF?= =?UTF-8?q?RAME=E8=AF=AF=E5=B7=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Thunks/user32.hpp | 106 ++++++++++++++------- src/YY-Thunks.UnitTest/User32.UnitTest.cpp | 32 +++++++ 2 files changed, 101 insertions(+), 37 deletions(-) diff --git a/src/Thunks/user32.hpp b/src/Thunks/user32.hpp index a0887e2..569627e 100644 --- a/src/Thunks/user32.hpp +++ b/src/Thunks/user32.hpp @@ -258,70 +258,102 @@ namespace YY::Thunks int, WINAPI, GetSystemMetricsForDpi, - _In_ int nIndex, - _In_ UINT dpi + _In_ int _nIndex, + _In_ UINT _uDpi ) { - if (auto const pGetSystemMetricsForDpi = try_get_GetSystemMetricsForDpi()) + if (auto const _pfnGetSystemMetricsForDpi = try_get_GetSystemMetricsForDpi()) { - return pGetSystemMetricsForDpi(nIndex, dpi); + return _pfnGetSystemMetricsForDpi(_nIndex, _uDpi); } - - auto nValue = GetSystemMetrics(nIndex); - if (nValue != 0) + if (_uDpi == 0ul) { - switch (nIndex) + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } + + auto _nValue = GetSystemMetrics(_nIndex); + if (_nValue != 0) + { + switch (_nIndex) { - case SM_CYSIZE: case SM_CXVSCROLL: - case SM_CYSMICON: - case SM_CYVSCROLL: - case SM_CXPADDEDBORDER: - case SM_CXSMICON: - case SM_CYSMSIZE: - case SM_CYICON: case SM_CYHSCROLL: - case SM_CYMENUCHECK: - case SM_CYCAPTION: - case SM_CXHSCROLL: - case SM_CXFRAME: - case SM_CYMENUSIZE: - case SM_CYFRAME: - case SM_CYMENU: - case SM_CXICON: - case SM_CXICONSPACING: - case SM_CYICONSPACING: - case SM_CYVTHUMB: + case SM_CYCAPTION: // 4 + + case SM_CYVTHUMB: // 9 case SM_CXHTHUMB: + case SM_CXICON: + case SM_CYICON: case SM_CXCURSOR: case SM_CYCURSOR: - case SM_CXMIN: - case SM_CXMINTRACK: + case SM_CYMENU: // 15 + + case SM_CYVSCROLL: // 20 + case SM_CXHSCROLL: + + case SM_CXMIN: // 28 case SM_CYMIN: - case SM_CYMINTRACK: case SM_CXSIZE: + case SM_CYSIZE: + case SM_CXMINTRACK: + case SM_CYMINTRACK: //35 + + case SM_CXICONSPACING: //38 + case SM_CYICONSPACING: // 39 + + case SM_CXSMICON: // 49 + case SM_CYSMICON: case SM_CYSMCAPTION: case SM_CXSMSIZE: + case SM_CYSMSIZE: case SM_CXMENUSIZE: - case SM_CXMENUCHECK: + case SM_CYMENUSIZE: // 55 + + case SM_CXMENUCHECK: // 71 + case SM_CYMENUCHECK: // 72 + + case SM_CXPADDEDBORDER: // 92 { - auto nDpiX = internal::GetDpiForSystemDownlevel(); - if (nDpiX != dpi) + const auto _uBaseDpi = internal::GetDpiForSystemDownlevel(); + if (_uBaseDpi != _uDpi) { - nValue *= dpi; - nValue += nDpiX / 2; - nValue /= nDpiX; + int _nDelta = 0; + switch (_nIndex) + { + case SM_CYCAPTION: // 4 + case SM_CYMENU: // 15 + case SM_CYSMCAPTION: // 51 + _nDelta = GetSystemMetrics(SM_CYBORDER); + break; + default: + __WarningMessage__("SM_CXMIN、SM_CYMIN、SM_CXMINTRACK、SM_CYMINTRACK、SM_CXMENUCHECK、SM_CYMENUCHECK 存在一定偏差。"); + break; + } + _nValue = MulDiv(_nValue - _nDelta, _uDpi, _uBaseDpi) + _nDelta; } break; } - default: + case SM_CXFRAME: + { + const auto _nBorreder = GetSystemMetrics(SM_CXBORDER); + _nValue -= 2 * _nBorreder; + _nValue = MulDiv(_nValue, _uDpi, USER_DEFAULT_SCREEN_DPI) + _nBorreder; break; } + case SM_CYFRAME: + { + const auto _nBorreder = GetSystemMetrics(SM_CYBORDER); + _nValue -= 2 * _nBorreder; + _nValue = MulDiv(_nValue, _uDpi, USER_DEFAULT_SCREEN_DPI) + _nBorreder; + break; + } + } } - return nValue; + return _nValue; } #endif diff --git a/src/YY-Thunks.UnitTest/User32.UnitTest.cpp b/src/YY-Thunks.UnitTest/User32.UnitTest.cpp index ce0b79c..caec221 100644 --- a/src/YY-Thunks.UnitTest/User32.UnitTest.cpp +++ b/src/YY-Thunks.UnitTest/User32.UnitTest.cpp @@ -126,4 +126,36 @@ namespace User32 } } }; + + TEST_CLASS(GetSystemMetricsForDpi) + { + public: + GetSystemMetricsForDpi() + { + } + + TEST_METHOD(常规测试) + { + for (int _uIndex = 0; _uIndex != 100; ++_uIndex) + { + if (SM_CXMIN == _uIndex || SM_CYMIN == _uIndex + || SM_CXMINTRACK == _uIndex || SM_CYMINTRACK == _uIndex + || SM_CXMENUCHECK == _uIndex || SM_CYMENUCHECK == _uIndex) + { + // 暂时无法做到完全一致 + continue; + } + + const auto _nTarget = ::GetSystemMetricsForDpi(_uIndex, USER_DEFAULT_SCREEN_DPI * 2); + YY::Thunks::aways_null_try_get_GetSystemMetricsForDpi = true; + const auto _nCurrent = ::GetSystemMetricsForDpi(_uIndex, USER_DEFAULT_SCREEN_DPI * 2); + YY::Thunks::aways_null_try_get_GetSystemMetricsForDpi = false; + + CStringW _szMessage; + _szMessage.Format(L"nIndex = %d", _uIndex); + + Assert::AreEqual(_nTarget, _nCurrent, _szMessage.GetString()); + } + } + }; }