Skip to content

Commit

Permalink
Restore window geometry correctly, even in high DPI
Browse files Browse the repository at this point in the history
The coordinates returned by GetWindowPlacement() must be passed to
SetWindowPlacement() and not SetWindowPos() as the former functions
account correctly for non-standard DPI, while the latter one does not.

Using SetWindowPlacement() also allows to remove the checks for the
coordinates being valid because the function already checks for this and
will always position the window inside the visible area anyhow.

Fixes #370.
  • Loading branch information
vadz authored and janwilmans committed Feb 22, 2024
1 parent fd80e5c commit b924e5c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions application/DebugViewpp/MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,22 @@ bool CMainFrame::LoadSettings()
DWORD cy = 0;
CRegKey reg;
reg.Create(HKEY_CURRENT_USER, RegistryPath);
if (reg.QueryDWORDValue(L"X", x) == ERROR_SUCCESS && static_cast<int>(x) >= GetSystemMetrics(SM_XVIRTUALSCREEN) &&
reg.QueryDWORDValue(L"Y", y) == ERROR_SUCCESS && static_cast<int>(y) >= GetSystemMetrics(SM_YVIRTUALSCREEN) &&
reg.QueryDWORDValue(L"Width", cx) == ERROR_SUCCESS && static_cast<int>(x + cx) <= GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN) &&
reg.QueryDWORDValue(L"Height", cy) == ERROR_SUCCESS && static_cast<int>(y + cy) <= GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN))
{
SetWindowPos(nullptr, x, y, cx, cy, SWP_NOZORDER);
if (reg.QueryDWORDValue(L"X", x) == ERROR_SUCCESS &&
reg.QueryDWORDValue(L"Y", y) == ERROR_SUCCESS &&
reg.QueryDWORDValue(L"Width", cx) == ERROR_SUCCESS &&
reg.QueryDWORDValue(L"Height", cy) == ERROR_SUCCESS)
{
WINDOWPLACEMENT placement = {0};
placement.length = sizeof(placement);

placement.rcNormalPosition.left = x;
placement.rcNormalPosition.top = y;
placement.rcNormalPosition.right = x + cx;
placement.rcNormalPosition.bottom = y + cy;

// Ignore errors, the worst that can happen is that the window doesn't
// appear at the correct position, but this is not the end of the world.
::SetWindowPlacement(*this, &placement);
}

m_linkViews = Win32::RegGetDWORDValue(reg, L"LinkViews", 0) != 0;
Expand Down

0 comments on commit b924e5c

Please sign in to comment.