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 TrayIcon.cpp
1.7 kB · 42 lines
C++
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243#include "stdafx.h"#include "TrayIcon.h"#include "resource.h"
// Menu command ids (kept in sync with App.cpp).enum { IDM_SHOWHIDE = 1, IDM_STARTUP = 2, IDM_ABOUT = 3, IDM_EXIT = 4 };
bool TrayIcon::Add(HWND owner, UINT cbMsg, UINT iconId, const wchar_t* tip) { m_nid = {}; m_nid.cbSize = sizeof(m_nid); m_nid.hWnd = owner; m_nid.uID = iconId; m_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; m_nid.uCallbackMessage = cbMsg; m_nid.hIcon = (HICON)LoadImage(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0); lstrcpynW(m_nid.szTip, tip, ARRAYSIZE(m_nid.szTip)); m_added = Shell_NotifyIconW(NIM_ADD, &m_nid) != FALSE; return m_added;}
void TrayIcon::Remove() { if (m_added) { Shell_NotifyIconW(NIM_DELETE, &m_nid); m_added = false; } if (m_nid.hIcon) { DestroyIcon(m_nid.hIcon); m_nid.hIcon = nullptr; }}
void TrayIcon::ShowContextMenu(HWND owner, bool startupChecked, bool startupAvailable) { HMENU menu = CreatePopupMenu(); AppendMenuW(menu, MF_STRING, IDM_SHOWHIDE, L"Show / Hide"); AppendMenuW(menu, MF_STRING | (startupChecked ? MF_CHECKED : 0) | (startupAvailable ? 0 : MF_GRAYED), IDM_STARTUP, L"Start at login"); AppendMenuW(menu, MF_SEPARATOR, 0, nullptr); AppendMenuW(menu, MF_STRING, IDM_ABOUT, L"About"); AppendMenuW(menu, MF_STRING, IDM_EXIT, L"Exit");
POINT pt; GetCursorPos(&pt); SetForegroundWindow(owner); // so the menu dismisses correctly TrackPopupMenu(menu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, owner, nullptr); DestroyMenu(menu);}