This blog mainly describes how to use Win32 API to compile a system tray. The following functions are implemented: Display prompt box, bubble, and context menu. Implementation System
The main function used in the system tray is shell_policyicon, And the struct used is policyicondata.
After running, we can see our tray in the taskbar.
When we move the mouse over the tray icon, right-click the context menu, which has two menu items: Exit and Hide. Click Exit to Exit the program.
Click Hide to Hide the window. When you click the tray icon, the hidden window is displayed. You can download the source file and run it yourself.
Look. The main code is as follows:
Lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {policyicondata nid; static HMENU hMenu; POINT point POINT; HINSTANCE hInstance; switch (message) {case WM_CREATE: hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE); hMenu = LoadMenu (hInstance, TEXT ("TuoPan"); hMenu = GetSubMenu (hMenu, 0); nid. cbSize = sizeof (policyicondata); nid. hWnd = hwnd; nid. uID = IDI_ICON1; nid. uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO; nid. hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON1); nid. uCallbackMessage = WMU_NOTIFY; // wcscpy_s (nid. szTip, szAppName); // bubble notification wcscpy_s (nid. szInfo, szAppName); // notification content wcscpy_s (nid. szInfoTitle, L ":)"); // bubble title nid. uTimeout = 10000; // notification retention time, in ms, nid. dwInfoFlags = NIIF_NONE; // The shell_policyicon (NIM_ADD, & nid); return 0; case WM_COMMAND: switch (LOWORD (wParam) {case IDM_EXIT: SendMessage (hwnd, WM_DESTROY, wParam, lParam); return 0; case IDM_HIDE: ShowWindow (hwnd, SW_HIDE); return 0;} return 0; case wmu_policy: if (lParam = WM_RBUTTONDOWN) // In the tray message, lParam accepts the mouse behavior {GetCursorPos (& point); SetForegroundWindow (hwnd); TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, point. x, point. y, 0, hwnd, NULL);} else if (lParam = WM_LBUTTONDOWN) {ShowWindow (hwnd, SW_SHOW);} return 0; case WM_DESTROY: nid. uID = IDI_ICON1; nid. hWnd = hwnd; shell_policyicon (NIM_DELETE, & nid); PostQuitMessage (0); return 0;} return DefWindowProc (hwnd, message, wParam, lParam );}