Skip to content

Commit

Permalink
Bug,GetSystemMetricsForDpi适配系统,DPI传递0时返回失败,并且修正SM_CYCAPTION、SM_CYMENU…
Browse files Browse the repository at this point in the history
…、SM_CYSMCAPTION、SM_CXFRAME、SM_CYFRAME误差
  • Loading branch information
mingkuang-Chuyu committed Jun 30, 2024
1 parent 29a93a8 commit bd617ef
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 37 deletions.
106 changes: 69 additions & 37 deletions src/Thunks/user32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions src/YY-Thunks.UnitTest/User32.UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
};
}

0 comments on commit bd617ef

Please sign in to comment.