Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restoring window geometry #392

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/DebugViewpp/DebugView++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine
Win32::ThrowLastError(L"Main window creation failed!");
}

wndMain.ShowWindow(cmdShow);
wndMain.ShowWindow(cmdShow == SW_SHOWDEFAULT ? wndMain.GetShowCommand() : cmdShow);
if (boost::algorithm::iends_with(fileName, ".dbconf"))
{
wndMain.LoadConfiguration(fileName);
Expand Down
51 changes: 46 additions & 5 deletions application/DebugViewpp/MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,47 @@ 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))
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)
{
SetWindowPos(nullptr, x, y, cx, cy, SWP_NOZORDER);
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;

if (reg.QueryDWORDValue(L"MaxX", x) == ERROR_SUCCESS &&
reg.QueryDWORDValue(L"MaxY", y) == ERROR_SUCCESS)
{
placement.ptMaxPosition.x = x;
placement.ptMaxPosition.y = y;
}

// This is tricky: we should _not_ use the show command corresponding
// to the actual initial window state in WINDOWPLACEMENT because doing
// this loses the normal geometry, i.e. if we pass SW_SHOWMAXIMIZED to
// SetWindowPlacement(), the window appears maximized initially, but
// restoring it doesn't restore its previous rectangle.
//
// Instead, we always use SW_SHOWNORMAL here, but use the appropriate
// show command for the ShowWindow() call in Main(), as this ensures
// both that the window starts initially maximized, if it was closed in
// this state, and that it can be restored to its last normal position.
DWORD on = FALSE;
if (reg.QueryDWORDValue(L"Maximized", on) == ERROR_SUCCESS && on)
m_showCmd = SW_SHOWMAXIMIZED;
else if (reg.QueryDWORDValue(L"Minimized", on) == ERROR_SUCCESS && on)
m_showCmd = SW_SHOWMINIMIZED;

placement.showCmd = SW_SHOWNORMAL;

// 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 Expand Up @@ -764,6 +799,12 @@ void CMainFrame::SaveSettings()
reg.SetDWORDValue(L"Width", placement.rcNormalPosition.right - placement.rcNormalPosition.left);
reg.SetDWORDValue(L"Height", placement.rcNormalPosition.bottom - placement.rcNormalPosition.top);

reg.SetDWORDValue(L"Maximized", placement.showCmd == SW_SHOWMAXIMIZED);
reg.SetDWORDValue(L"Minimized", placement.showCmd == SW_SHOWMINIMIZED);

reg.SetDWORDValue(L"MaxX", placement.ptMaxPosition.x);
reg.SetDWORDValue(L"MaxY", placement.ptMaxPosition.y);

reg.SetDWORDValue(L"LinkViews", static_cast<DWORD>(m_linkViews));
reg.SetDWORDValue(L"AutoNewLine", static_cast<DWORD>(m_logSources.GetAutoNewLine()));
reg.SetDWORDValue(L"AlwaysOnTop", static_cast<DWORD>(GetAlwaysOnTop()));
Expand Down
5 changes: 5 additions & 0 deletions application/DebugViewpp/MainFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class CMainFrame : public CTabbedFrameImpl<CMainFrame, CDotNetTabCtrl<SelectedTa
void FindPrevious(const std::wstring& text);
void OnDropped(std::wstring uri);

// Return the command which should be used to show the window, it is
// restored from the registry when creating it.
int GetShowCommand() const { return m_showCmd; }

private:
enum
{
Expand Down Expand Up @@ -218,6 +222,7 @@ class CMainFrame : public CTabbedFrameImpl<CMainFrame, CDotNetTabCtrl<SelectedTa
Win32::JobObject m_jobs;
Win32::Handle m_httpMonitorHandle;
std::deque<Lines> m_incomingMessages;
int m_showCmd = SW_SHOWDEFAULT;
};

} // namespace debugviewpp
Expand Down
Loading