Create a system tray icon using Win32 API

Source: Internet
Author: User

System pallets are common in our programs. Here is a good example. Using Win32 API is helpful for understanding system pallets.

# Include <windows. h> # define IDR_PAUSE 12 # define IDR_START 13/* # include <shellapi. h> # pragma comment (lib, "shell32.lib") */LPCTSTR szAppName = TEXT ("service program"); LPCTSTR szWndName = TEXT ("service program"); HMENU hmenu; // menu handle lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {policyicondata nid; UINT WM_TASKBARCREATED; POINT pt; // used to receive the mouse coordinate int xx; // receives the return value of the menu option. // do not modify TaskbarCreated, which is a custom message in the system taskbar. WM_TASKBARCREATED = RegisterWindowMessage (TEXT ("TaskbarCreated"); switch (message) {case WM_CREATE: // message generated when the window is created. 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_policyicon (NIM_ADD, & nid); hmenu = CreatePopupMenu (); // generate the menu AppendMenu (hmenu, MF_STRI NG, IDR_PAUSE, "suspend service"); // Add two options for the menu: AppendMenu (hmenu, MF_STRING, IDR_START, "restore service"); break; case WM_USER: // messages generated when the program is used continuously. if (lParam = WM_LBUTTONDOWN) MessageBox (hwnd, TEXT ("Win32 API Implementation System Tray program, double-click the tray to exit! "), SzAppName, MB_ OK); if (lParam = WM_LBUTTONDBLCLK) // double-click the message on the tray and exit. sendMessage (hwnd, WM_CLOSE, wParam, lParam); if (lParam = WM_RBUTTONDOWN) {GetCursorPos (& pt); // take the mouse coordinate: SetForegroundWindow (hwnd ); // EnableMenuItem (hmenu, IDR_PAUSE, MF_GRAYED) solves the problem that the left-click menu does not disappear. // gray xx = TrackPopupMenu (hmenu, TPM_RETURNCMD, pt. x, pt. y, NULL, hwnd, NULL); // display the menu and obtain the IDif (xx = IDR_PAUSE) MessageBox (hwnd, TEXT ("111"), szAppName, MB_ OK); if (xx = IDR_START) MessageBox (hwnd, TEXT ("222"), szAppName, MB_ OK); if (xx = 0) PostMessage (hwnd, WM_LBUTTONDOWN, NULL, NULL); // MessageBox (hwnd, TEXT ("right-click"), szAppName, MB_ OK) ;}break; case WM_DESTROY: // message when the window is destroyed. shell_policyicon (NIM_DELETE, & nid); PostQuitMessage (0); break; default:/** when assumer.exe crashes, the icons in the system tray disappear ** principle: after assumer.exe is reloaded, the system taskbar is rebuilt. When the system taskbar is set up, a message will be sent to all top-level windows in the system that * Register to receive TaskbarCreated messages. We only need to capture this message and re-create the * System Tray Icon. */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;} // here the WS_EX_TOOLWINDOW attribute is used to hide the window program button hwnd = createjavaswex (WS_EX_TOOLWINDOW, szAppName, szWndName, WS_POPUP, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage (& msg, NULL, 0, 0) {TranslateMessage (& msg); DispatchMessage (& msg);} return msg. wParam ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.