diff --git a/CalculatorWindow.cpp b/CalculatorWindow.cpp index b3228a8..6109ef1 100644 --- a/CalculatorWindow.cpp +++ b/CalculatorWindow.cpp @@ -198,6 +198,7 @@ void CCalculatorWindow::PlaceInitial() RECT full = { r.left, r.top, r.left + w, r.top + h }; if (::MonitorFromRect(&full, MONITOR_DEFAULTTONULL) != NULL) { SetWindowPos(NULL, r.left, r.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER); + ClampToWorkArea(); // pull it out of the taskbar if the saved pos overlapped return; } } @@ -212,6 +213,30 @@ void CCalculatorWindow::PlaceInitial() SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); } +void CCalculatorWindow::ClampToWorkArea() +{ + RECT wr; GetWindowRect(&wr); + const int w = wr.right - wr.left; + const int h = wr.bottom - wr.top; + + MONITORINFO mi = { sizeof(mi) }; + if (!::GetMonitorInfo(::MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST), &mi)) + return; + const RECT& work = mi.rcWork; + + // Keep the whole window inside the work area (which excludes the taskbar). + // Push in from the right/bottom first, then clamp to the top/left so a + // window larger than the work area still anchors at its top-left corner. + int x = wr.left, y = wr.top; + if (x + w > work.right) x = work.right - w; + if (y + h > work.bottom) y = work.bottom - h; + if (x < work.left) x = work.left; + if (y < work.top) y = work.top; + + if (x != wr.left || y != wr.top) + SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); +} + void CCalculatorWindow::ToggleVisible() { if (m_shown) { @@ -412,6 +437,10 @@ LRESULT CCalculatorWindow::OnLButtonDown(UINT, WPARAM, LPARAM, BOOL&) LRESULT CCalculatorWindow::OnExitSizeMove(UINT, WPARAM, LPARAM, BOOL&) { + // Keep the window within the monitor work area so it never sits over (and + // then behind) the taskbar. + ClampToWorkArea(); + // Persist the canonical position (client origin), independent of caption state. RECT rc; GetClientRect(&rc); ::MapWindowPoints(m_hWnd, NULL, (LPPOINT)&rc, 2); diff --git a/CalculatorWindow.h b/CalculatorWindow.h index 58e2f36..7931e7b 100644 --- a/CalculatorWindow.h +++ b/CalculatorWindow.h @@ -84,6 +84,7 @@ private: UINT m_dpi = 96; CAutoPtr m_ptrVisualStyle; void ApplyFrameStyle(bool visible); + void ClampToWorkArea(); bool m_captionVisible = true; // window is created with WS_CAPTION | WS_SYSMENU //const DateFormat* m_pDateFormat; diff --git a/version.h b/version.h index 5e2fa84..72a0f76 100644 --- a/version.h +++ b/version.h @@ -3,7 +3,7 @@ // Semantic version — edit these by hand (NOT in the VS resource editor). #define VER_MAJOR 3 #define VER_MINOR 0 -#define VER_FIX 3 +#define VER_FIX 4 // Per-build counter. version_build.h is regenerated on every build by the // IncrementBuildCounter MSBuild target (BeforeTargets="ResourceCompile"), so it