From 47d8577d6dc4620db3b16c9fd672e1ebcd03d0a9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 21 Feb 2024 23:32:22 +0100 Subject: [PATCH] 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. --- application/DebugViewpp/DebugView++.cpp | 2 +- application/DebugViewpp/MainFrame.cpp | 31 +++++++++++++++++++++++++ application/DebugViewpp/MainFrame.h | 5 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/application/DebugViewpp/DebugView++.cpp b/application/DebugViewpp/DebugView++.cpp index 5474b12b..1d739b45 100644 --- a/application/DebugViewpp/DebugView++.cpp +++ b/application/DebugViewpp/DebugView++.cpp @@ -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); diff --git a/application/DebugViewpp/MainFrame.cpp b/application/DebugViewpp/MainFrame.cpp index 5bdba4b7..452d0db5 100644 --- a/application/DebugViewpp/MainFrame.cpp +++ b/application/DebugViewpp/MainFrame.cpp @@ -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); @@ -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(m_linkViews)); reg.SetDWORDValue(L"AutoNewLine", static_cast(m_logSources.GetAutoNewLine())); reg.SetDWORDValue(L"AlwaysOnTop", static_cast(GetAlwaysOnTop())); diff --git a/application/DebugViewpp/MainFrame.h b/application/DebugViewpp/MainFrame.h index 8a79d79a..a7194024 100644 --- a/application/DebugViewpp/MainFrame.h +++ b/application/DebugViewpp/MainFrame.h @@ -93,6 +93,10 @@ class CMainFrame : public CTabbedFrameImpl m_incomingMessages; + int m_showCmd = SW_SHOWDEFAULT; }; } // namespace debugviewpp