Skip to content

Commit

Permalink
Fix restoring position of maximized and minimized window too
Browse files Browse the repository at this point in the history
If the window was closed in the maximized (or, more rarely, but still
possible, minimized) state, restore it in the same state during the next
run.

Take care to use the appropriate show command for the ShowWindow() call
done in Main(), as using SW_SHOWMAXIMIZED with SetWindowPlacement()
doesn't work quite as expected.
  • Loading branch information
vadz authored and janwilmans committed Feb 22, 2024
1 parent b924e5c commit 47d8577
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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
31 changes: 31 additions & 0 deletions application/DebugViewpp/MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,31 @@ bool CMainFrame::LoadSettings()
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);
Expand Down Expand Up @@ -774,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

0 comments on commit 47d8577

Please sign in to comment.