Something went wrong. Try again.
A simple, small calculator placed discreetly near the Windows Taskbar or anywhere on the screen taskbar-calculator.marcomaroni.it
Something went wrong. Try again.
taskbar-calculator App.cpp
4.4 kB · 123 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124#include "stdafx.h"#include <commctrl.h>#include <winrt/Windows.Foundation.h>#include "TrayIcon.h"#include "StartupManager.h"#include "CalculatorWindow.h"#include "Settings.h"#include "resource.h"
enum { IDM_SHOWHIDE = 1, IDM_STARTUP = 2, IDM_ABOUT = 3, IDM_EXIT = 4 };static const UINT WM_TRAY_CALLBACK = WM_APP + 1;static const UINT TRAY_ICON_ID = 1;static const int HOTKEY_TOGGLE = 1; // global Win+Alt+C -> show/hide
UINT g_wmTaskbarCreated = 0;static TrayIcon g_tray;static CCalculatorWindow* g_calc = nullptr;
INT_PTR CALLBACK AboutDialogProc(HWND, UINT, WPARAM, LPARAM); // AboutDialog.cpp
static void ToggleCalculator() { if (g_calc) g_calc->ToggleVisible(); }
static LRESULT CALLBACK OwnerWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){ if (msg == g_wmTaskbarCreated) { // Explorer restarted g_tray.Add(hWnd, WM_TRAY_CALLBACK, TRAY_ICON_ID, L"Taskbar Calculator"); return 0; } switch (msg) { case WM_HOTKEY: if (wParam == HOTKEY_TOGGLE) ToggleCalculator(); return 0; case WM_TRAY_CALLBACK: if (LOWORD(lParam) == WM_LBUTTONUP) { ToggleCalculator(); } else if (LOWORD(lParam) == WM_RBUTTONUP) { Startup::State s = Startup::Get(); g_tray.ShowContextMenu(hWnd, s == Startup::State::Enabled, s != Startup::State::Unavailable); } return 0; case WM_COMMAND: switch (LOWORD(wParam)) { case IDM_SHOWHIDE: ToggleCalculator(); return 0; case IDM_STARTUP: { Startup::State s = Startup::Get(); if (s != Startup::State::Unavailable) Startup::Set(s != Startup::State::Enabled); return 0; } case IDM_ABOUT: DialogBoxW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDD_ABOUT), hWnd, AboutDialogProc); return 0; case IDM_EXIT: DestroyWindow(hWnd); return 0; } return 0; case WM_DESTROY: UnregisterHotKey(hWnd, HOTKEY_TOGGLE); g_tray.Remove(); PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam);}
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int){ // Single instance: a second launch signals the first and exits. HANDLE mutex = CreateMutexW(nullptr, TRUE, L"TaskbarCalculator_SingleInstance"); if (GetLastError() == ERROR_ALREADY_EXISTS) { HWND existing = FindWindowW(L"TaskbarCalculatorOwner", nullptr); if (existing) PostMessageW(existing, WM_COMMAND, IDM_SHOWHIDE, 0); return 0; }
winrt::init_apartment(winrt::apartment_type::single_threaded);
INITCOMMONCONTROLSEX icc{ sizeof(icc), ICC_LINK_CLASS | ICC_STANDARD_CLASSES }; InitCommonControlsEx(&icc);
g_wmTaskbarCreated = RegisterWindowMessageW(L"TaskbarCreated");
WNDCLASSW wc{}; wc.lpfnWndProc = OwnerWndProc; wc.hInstance = hInst; wc.lpszClassName = L"TaskbarCalculatorOwner"; RegisterClassW(&wc);
// 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");
// Global hotkey Win+Alt+C toggles the calculator, exactly like the tray // click. MOD_NOREPEAT avoids repeated toggles while the keys stay held. // If the combination is already taken by another app the call just fails // silently and the tray click still works. RegisterHotKey(owner, HOTKEY_TOGGLE, MOD_WIN | MOD_ALT | MOD_NOREPEAT, 'C');
g_calc = new CCalculatorWindow(); g_calc->CreateStandalone(); if (Settings::LoadVisible(true)) // first run (no saved value): visible g_calc->ToggleVisible();
MSG m; while (GetMessage(&m, nullptr, 0, 0)) { TranslateMessage(&m); DispatchMessage(&m); } if (g_calc) { if (g_calc->IsWindow()) g_calc->DestroyWindow(); delete g_calc; g_calc = nullptr; } if (mutex) { ReleaseMutex(mutex); CloseHandle(mutex); } return (int)m.wParam;}