Windows API Reference
This section covers functions related to managing notifications in the system tray area of the Windows user interface. These functions allow applications to display icons, tooltips, and notifications to users, often used for status updates or alerts.
Shell_NotifyIcon function is the primary interface for adding, modifying, or removing tray icons and associated notifications.
Applications can display an icon in the notification area (system tray) to provide a visual presence and a way for users to interact with the application. This is typically done using the NIM_ADD flag with Shell_NotifyIcon.
Modern Windows versions support notification balloons (or pop-ups) that appear near the system tray icon. These balloons can display text, titles, and icons, and can be dismissed automatically or by user interaction. The NIIF_INFO, NIIF_WARNING, and NIIF_ERROR flags for the uVersion parameter in Shell_NotifyIcon control the balloon's appearance.
Applications need to handle mouse events (like clicks or hovers) on their tray icon to provide context menus or respond to user actions. This is achieved by specifying a window handle and message identifiers when adding the icon via Shell_NotifyIcon.
// Include necessary headers
#include <windows.h>
#include <shellapi.h>
// Define a unique message for tray icon notifications
#define WM_MY_TRAY_NOTIFICATION (WM_USER + 1)
// ... in your application's initialization ...
NOTIFYICONDATA nid;
ZeroMemory(&nid, sizeof(nid));
nid.cbSize = sizeof(nid);
nid.hWnd = your_window_handle; // Handle to your application's window
nid.uID = 1; // Unique identifier for the icon
nid.uFlags = NIF_ICON | NIF_TIP;
nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYAPPICON)); // Your application's icon resource
wcscpy_s(nid.szTip, L"My Application"); // Tooltip text
// Add the icon to the system tray
Shell_NotifyIcon(NIM_ADD, &nid);
// ... in your window's message loop ...
case WM_MY_TRAY_NOTIFICATION:
switch (LOWORD(lParam))
{
case WM_LBUTTONUP: // Left mouse button released
// Show application's main window or context menu
break;
case WM_RBUTTONUP: // Right mouse button released
// Show context menu for the tray icon
break;
// Handle other notification codes (e.g., balloon clicks)
}
break;
// ... in your application's cleanup ...
nid.uFlags = 0; // Clear flags to signal removal
Shell_NotifyIcon(NIM_DELETE, &nid);