Skip to content

Commit 47d8577

Browse files
vadzjanwilmans
authored andcommitted
Fix restoring position of maximized and minimized window too
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.
1 parent b924e5c commit 47d8577

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

application/DebugViewpp/DebugView++.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ int Main(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR /*lpstrCmdLine
159159
Win32::ThrowLastError(L"Main window creation failed!");
160160
}
161161

162-
wndMain.ShowWindow(cmdShow);
162+
wndMain.ShowWindow(cmdShow == SW_SHOWDEFAULT ? wndMain.GetShowCommand() : cmdShow);
163163
if (boost::algorithm::iends_with(fileName, ".dbconf"))
164164
{
165165
wndMain.LoadConfiguration(fileName);

application/DebugViewpp/MainFrame.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,31 @@ bool CMainFrame::LoadSettings()
695695
placement.rcNormalPosition.right = x + cx;
696696
placement.rcNormalPosition.bottom = y + cy;
697697

698+
if (reg.QueryDWORDValue(L"MaxX", x) == ERROR_SUCCESS &&
699+
reg.QueryDWORDValue(L"MaxY", y) == ERROR_SUCCESS)
700+
{
701+
placement.ptMaxPosition.x = x;
702+
placement.ptMaxPosition.y = y;
703+
}
704+
705+
// This is tricky: we should _not_ use the show command corresponding
706+
// to the actual initial window state in WINDOWPLACEMENT because doing
707+
// this loses the normal geometry, i.e. if we pass SW_SHOWMAXIMIZED to
708+
// SetWindowPlacement(), the window appears maximized initially, but
709+
// restoring it doesn't restore its previous rectangle.
710+
//
711+
// Instead, we always use SW_SHOWNORMAL here, but use the appropriate
712+
// show command for the ShowWindow() call in Main(), as this ensures
713+
// both that the window starts initially maximized, if it was closed in
714+
// this state, and that it can be restored to its last normal position.
715+
DWORD on = FALSE;
716+
if (reg.QueryDWORDValue(L"Maximized", on) == ERROR_SUCCESS && on)
717+
m_showCmd = SW_SHOWMAXIMIZED;
718+
else if (reg.QueryDWORDValue(L"Minimized", on) == ERROR_SUCCESS && on)
719+
m_showCmd = SW_SHOWMINIMIZED;
720+
721+
placement.showCmd = SW_SHOWNORMAL;
722+
698723
// Ignore errors, the worst that can happen is that the window doesn't
699724
// appear at the correct position, but this is not the end of the world.
700725
::SetWindowPlacement(*this, &placement);
@@ -774,6 +799,12 @@ void CMainFrame::SaveSettings()
774799
reg.SetDWORDValue(L"Width", placement.rcNormalPosition.right - placement.rcNormalPosition.left);
775800
reg.SetDWORDValue(L"Height", placement.rcNormalPosition.bottom - placement.rcNormalPosition.top);
776801

802+
reg.SetDWORDValue(L"Maximized", placement.showCmd == SW_SHOWMAXIMIZED);
803+
reg.SetDWORDValue(L"Minimized", placement.showCmd == SW_SHOWMINIMIZED);
804+
805+
reg.SetDWORDValue(L"MaxX", placement.ptMaxPosition.x);
806+
reg.SetDWORDValue(L"MaxY", placement.ptMaxPosition.y);
807+
777808
reg.SetDWORDValue(L"LinkViews", static_cast<DWORD>(m_linkViews));
778809
reg.SetDWORDValue(L"AutoNewLine", static_cast<DWORD>(m_logSources.GetAutoNewLine()));
779810
reg.SetDWORDValue(L"AlwaysOnTop", static_cast<DWORD>(GetAlwaysOnTop()));

application/DebugViewpp/MainFrame.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class CMainFrame : public CTabbedFrameImpl<CMainFrame, CDotNetTabCtrl<SelectedTa
9393
void FindPrevious(const std::wstring& text);
9494
void OnDropped(std::wstring uri);
9595

96+
// Return the command which should be used to show the window, it is
97+
// restored from the registry when creating it.
98+
int GetShowCommand() const { return m_showCmd; }
99+
96100
private:
97101
enum
98102
{
@@ -218,6 +222,7 @@ class CMainFrame : public CTabbedFrameImpl<CMainFrame, CDotNetTabCtrl<SelectedTa
218222
Win32::JobObject m_jobs;
219223
Win32::Handle m_httpMonitorHandle;
220224
std::deque<Lines> m_incomingMessages;
225+
int m_showCmd = SW_SHOWDEFAULT;
221226
};
222227

223228
} // namespace debugviewpp

0 commit comments

Comments
 (0)