diff --git a/App.cpp b/App.cpp index b3e2309..8250315 100644 --- a/App.cpp +++ b/App.cpp @@ -83,8 +83,10 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int) wc.lpszClassName = L"TaskbarCalculatorOwner"; RegisterClassW(&wc); - HWND owner = CreateWindowW(wc.lpszClassName, L"", 0, 0, 0, 0, 0, - HWND_MESSAGE, nullptr, hInst, nullptr); + // Hidden top-level tool window (no taskbar button) so it still receives + // the broadcast "TaskbarCreated" message when Explorer restarts. + HWND owner = CreateWindowExW(WS_EX_TOOLWINDOW, wc.lpszClassName, L"", + WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, hInst, nullptr); g_tray.Add(owner, WM_TRAY_CALLBACK, TRAY_ICON_ID, L"Taskbar Calculator"); diff --git a/CalculatorWindow.cpp b/CalculatorWindow.cpp index c323d86..d62d30d 100644 --- a/CalculatorWindow.cpp +++ b/CalculatorWindow.cpp @@ -4,6 +4,7 @@ #include "VisualStyle.h" #include "Utils.h" #include "Calculator.h" +#include "Settings.h" //////////////////////////////////////////////////////////////////////////////// // @@ -146,9 +147,31 @@ BOOL CCalculatorWindow::CreateStandalone() POINTL ideal = CalcIdealSize(); SetWindowPos(NULL, 0, 0, ideal.x + 24, ideal.y + 16, SWP_NOMOVE | SWP_NOZORDER); + PlaceInitial(); return TRUE; } +void CCalculatorWindow::PlaceInitial() +{ + RECT wr; GetWindowRect(&wr); + int w = wr.right - wr.left, h = wr.bottom - wr.top; + + POINT pt; + if (Settings::LoadWindowPos(pt)) { + SetWindowPos(NULL, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); + return; + } + // First run: anchor bottom-right, just above the notification area. + APPBARDATA abd{}; abd.cbSize = sizeof(abd); + RECT work; SystemParametersInfo(SPI_GETWORKAREA, 0, &work, 0); + int x = work.right - w - 8; + int y = work.bottom - h - 8; + if (SHAppBarMessage(ABM_GETTASKBARPOS, &abd)) { + if (abd.rc.top > work.top) y = abd.rc.top - h - 8; + } + SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); +} + void CCalculatorWindow::ToggleVisible() { if (m_shown) { @@ -327,6 +350,13 @@ LRESULT CCalculatorWindow::OnLButtonDown(UINT, WPARAM, LPARAM, BOOL&) return 0; } +LRESULT CCalculatorWindow::OnExitSizeMove(UINT, WPARAM, LPARAM, BOOL&) +{ + RECT wr; GetWindowRect(&wr); + Settings::SaveWindowPos(POINT{ wr.left, wr.top }); + return 0; +} + void CCalculatorWindow::Paint(HDC hdc, const RECT& rcPaint) const { m_ptrVisualStyle->DrawBackground(m_hWnd, hdc, rcPaint); diff --git a/CalculatorWindow.h b/CalculatorWindow.h index 20acd7a..c020c35 100644 --- a/CalculatorWindow.h +++ b/CalculatorWindow.h @@ -16,6 +16,7 @@ public: BOOL CreateStandalone(); void ToggleVisible(); bool IsShown() const { return m_shown; } + void PlaceInitial(); POINTL CalcMinimalSize() const; POINTL CalcIdealSize() const; @@ -40,6 +41,7 @@ BEGIN_MSG_MAP(CCalculatorWindow) MESSAGE_HANDLER(WM_THEMECHANGED, OnThemeChanged) MESSAGE_HANDLER(WM_CLOSE, OnClose) MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown) + MESSAGE_HANDLER(WM_EXITSIZEMOVE, OnExitSizeMove) END_MSG_MAP() // Handler prototypes: @@ -56,6 +58,7 @@ private: LRESULT OnThemeChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); + LRESULT OnExitSizeMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); void Paint(HDC hdc, const RECT& rcPaint) const; // UINT_PTR SetUpdateTimer();