Win32 API implements system tray programs
#include
LPCTSTST SZAPPNAME = TEXT ("TRAYHELPER"); lpctstr szWndname = text ("trayicon");
Lresult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {Notifyicondata Nid; UINT WM_TASKBARCREATED
// Do not modify TaskbarCreated, this is the message WM_TASKBARCREATED system tray custom = RegisterWindowMessage (TEXT ( "TaskbarCreated")); switch (message) {case WM_CREATE: nid.cbSize = sizeof (nid); nid.hWnd = hwnd; nid.uID = 0; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uCallbackMessage = WM_USER; nid.hIcon = LoadIcon (NULL, IDI_APPLICATION); lstrcpy (nid.szTip, szAppName); Shell_NotifyIcon (NIM_ADD, & nid); break ; case WM_USER: if (lParam == WM_LBUTTONDOWN) MessageBox (hwnd, TEXT ( "Win32 API for system tray program!"), szAppName, MB_OK); if (lParam == WM_LBUTTONDBLCLK) SendMessage (hwnd, WM_CLOSE, wParam, lParam) Break; Case WM_DESTROY: Shell_NotifyiCon; PostquitMessage (0); Break; Default: / * * Prevents the icon in the system system trays disappear when Explorer.exe crashes, * * Principle: Explorer.exe The system task bar will be rebuilt after reloading. When the system taskbar is created, send a message to all * registration reception taskbarcreated messages in the system, we only need to capture this message and rebuild the icon of the system *. * / If (Message == WM_TASKBarcreated) SendMessage (HWND, WM_CREATE, WPARAM, LPARAM); Break;} Return DefWindowProc (HWND, Message, WPARAM, LPARAM);
Int WinAPI Winmain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR SZCMDLINE, INT ICMDSHOW) {HWND HWND; MSG MSG; WNDCLASS WNDCLASS;
HWnd Handle = FindWindow (NULL, SZWNDNAME); if (Handle! = NULL) {MessageBox (NULL, TEXT ("Application IS Already Running"), Szappname, MB_ICONERROR); RETURN 0;}
wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL , IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName;! if (RegisterClass (& wndclass)) {MessageBox (NULL, TEXT ( "This program requires Windows NT! "), szappname, MB_ICONERROR); RETURN 0;}
// use here WS_EX_TOOLWINDOW property to hide the display window of the program button on the taskbar hwnd = CreateWindowEx (WS_EX_TOOLWINDOW, szAppName, szWndName, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, icmdshow); UpdateWindow (HWND);
While (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);
Return msg.wparam;}