VC ++ minimizes pallets, restores, and vc pallets

Source: Internet
Author: User

VC ++ minimizes pallets, restores, and vc pallets

The so-called "Tray" refers to the part of the system time mark on the right of the following task bar on the Windows system interface. When the program is minimized or suspended, but you do not want to occupy the taskbar, you can put the program in the tray area.

I. functions related to pallet Programming 
The essence of putting a program on a tray is to first draw an icon in the tray area, then hide the program, and then process the message of the tray icon.
There is only one function for drawing the icon and determining the message transmitted by the icon:

Winshellapi bool winapi shell_policyicon (DWORD dwMessage, PNOTIFYICONDATA pnid );
This function is used to send messages to the system to add, modify, or delete icons in the tray area. The returned value is of the boolean type. That is to say, if 0 is returned, it is Cheng Ren. If not 0, it is successful. The dwMessage parameter indicates the application function of the function, whether to add, delete, or modify the icon. If it is added, its value is NIM_ADD; if it is deleted, it is NIM_DELETE; if it is deleted, it is changed to NIM_MODIFY. The pnid parameter is a specific structure related to the icon of the program in the tray area. It is defined as follows:

          typedef   struct   _NOTIFYICONDATA   {              DWORD   cbSize;              HWND   hWnd;              UINT   uID;              UINT   uFlags;              UINT   uCallbackMessage;              HICON   hIcon;              char   szTip[64];              }   NOTIFYICONDATA,   *PNOTIFYICONDATA;  


The following describes the parameters of the structure:
CbSize: the length of the structure. The unit is bit. In the program, we use (DWORD) sizeof (policyicondata) to assign values to it.
HWnd: a handle. If you operate the icon in the tray, the corresponding message is sent to the window represented by this handle. Naturally, in most cases it is this-> m_hWnd.
UID: the icon ID defined in the project.
UFlags: indicates which other Members have valid data, such as NIF_ICON, NIF_MESSAGE, and NIF_TIP. The valid data members are hIcon, uCallbackMessage, and szTip. Of course, the three values can be associated with "|. The following describes the involved members.
HIcon: the icon handle to be added, deleted, or modified. If only uID is known, the LoadIcon function may be used to obtain the handle. For example, LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDR_MAINFRAME )).
UCallbackMessage: this is an important data member in operations on the pallets. This is a message sign. When you use the mouse to operate the corresponding icon in the tray area, the message will be transmitted to the window represented by Hwnd. Therefore, in uFlags, it is generally valid. Generally, custom messages are used here.
SzTip: The text displayed when you move the mouse over the tray icon.

Ii. Principles
1. Principle of minimization: First hide the window, and then draw the icon in the lower right corner.
2. Restoration principle: display the window and delete the images in the tray.
3. Program Implementation
1. Custom message WM_SHOWTASK: # define WM_SHOWTASK (WM_USER + 1)
2. Add a command response in the: OnSysCommand (UINT nID, LPARAM lParam) function body of MFC.

If (nID = SC _MINIMIZE) ToTray (); // The function for minimizing to the tray
3. Add ON_MESSAGE (WM_SHOWTASK, OnShowTask) to message ing. Where WM_SHOWTASK is the message name, And OnShowTask is the message response function defined by myself.
Iv. Specific function content

Policyicondata nid; // It is defined as a member variable.
1. minimize to the tray Function

Void CMyDlg: ToTray () {nid. cbSize = (DWORD) sizeof (policyicondata); nid. hWnd = this-> m_hWnd; nid. uID = IDR_MAINFRAME; nid. uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid. uCallbackMessage = WM_SHOWTASK; // custom message name nid. hIcon = LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDR_MAINFRAME); strcpy (nid. szTip, "scheduled task reminder"); // The message prompt bar is "scheduled task reminder" shell_policyicon (NIM_ADD, & nid); // Add the icon ShowWindow (SW_HIDE) in the tray area ); // hide the main window}


2. Restore interface functions
Define the message response function afx_msg LRESULT OnShowTask (WPARAM wParam, LPARAMlParam) in the header file; // wParam receives the icon ID, while lParam receives the mouse action.

LRESULT CMyDlg: OnShowTask (WPARAM wParam, LPARAM lParam) {if (wParam! = IDR_MAINFRAME) return 1; switch (lParam) {case WM_RBUTTONUP: // The shortcut menu is displayed when you right-click it. There is only one "off" {LPPOINT lpoint = new tagPOINT ;:: getCursorPos (lpoint); // obtain the CMenu menu; menu at the mouse position. createPopupMenu (); // declare a pop-up menu // Add the menu item "close". Click it to send the message WM_DESTROY to the main window (already // hidden) and end the program. Menu. appendMenu (MF_STRING, WM_DESTROY, "close"); menu. appendMenu (MF_STRING, WM_DESTROY, "show"); // you can specify the position of the pop-up menu. trackPopupMenu (TPM_LEFTALIGN, lpoint-> x, lpoint-> y, this); // Resource Recycling HMENU hmenu = menu. detach (); menu. destroyMenu (); delete lpoint;} break; case WM_LBUTTONDBLCLK: // double-click the left button to process {this-> ShowWindow (SW_SHOWNORMAL); // simple display of the Main Window} break ;} return 0 ;}


3. to disappear the icon when the application exits, map the WM_DESTROY message and add the following to the OnDestroy () function:





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: 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.