Win32 API Implementation System Tray

Source: Internet
Author: User

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 );}

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.