[Sorting] how to put the window in the system tray when you click minimize

Source: Internet
Author: User

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

Ii. Code Implementation
1. Custom message wm_showtask: # define wm_showtask (wm_user + 1)/* Ms defines a macro wm_user to prevent conflict between user-defined message IDs and system message IDs, IDS smaller than wm_user are used by the system, and IDs larger than wm_user are used by users. So it is wm_user + 1 */

2. Add a command response in the: onsyscommand (uint NID, lparam) function body of MFC.
Void cmyframe: onsyscommand (uint NID, lparam) <br/>{< br/> // todo: zookeeper has been added when zookeeper has been added, and the established billing mechanism has been called for outgoing zookeeper. <Br/> If (SC _minimize = NID) {// minimized <br/> totray (); <br/>}< br/> else <br/>{< br/> using ichildwnd: onsyscommand (NID, lparam ); <br/>}< br/> trace ("--- cmyframe: onsyscommand/N"); <br/>}

3. Add the following code between begin_message_map (cmyframe, cmdichildwnd) and end_message_map ():
On_message (wm_showtask, onshowtask)
Here, wm_showtask is the message name, And onshowtask is the defined message response function, which is described later.

 

4. minimize to the tray Function
Void cmyframe: totray () <br/>{< br/> policyicondata NID; <br/> NID. cbsize = (DWORD) sizeof (policyicondata); <br/> NID. hwnd = This-> m_hwnd; <br/> NID. uid = idr_mainframe; <br/> NID. uflags = nif_icon | nif_message | nif_tip; <br/> NID. ucallbackmessage = wm_showtask; <br/> NID. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe); <br/> strcpy (NID. sztip, "measure... "); <br/> shell_policyicon (nim_add, & nid); <br/> showwindow (sw_hide); <br/>}

 

5. Restore interface functions
Define the message response function afx_msg lresult onshowtask (wparam, lparam) in the header file );
// Wparam receives the icon ID, while lparam receives the mouse action.

Lresult cmyframe: onshowtask (wparam, lparam) <br/>{< br/> If (wparam! = Idr_mainframe) <br/> return 1; <br/> switch (lparam) <br/>{< br/> case wm_rbuttonup: // <br/>{< br/> lppoint lpoint = new tagpoint; <br/>: getcursorpos (lpoint); // <br/> cmenu menu; <br/> menu. createpopupmenu (); // <br/> menu. appendmenu (mf_string, wm_destroy, "close"); // <br/> menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this); // <br/> hmenu = menu. detach (); <br/> menu. destroymenu (); <br/> Delete lpoint; <br/>}< br/> break; <br/> case wm_lbuttondblclk: // <br/>{< br/> This-> showwindow (sw_show); // <br/> deletetray (); <br/>}< br/> break; <br/> default: <br/> break; <br/>}< br/> return 0; <br/>}

 

6. Delete the tray icon Function
Void cmyframe: deletetray () <br/>{< br/> policyicondata NID; <br/> NID. cbsize = (DWORD) sizeof (policyicondata); <br/> NID. hwnd = This-> m_hwnd; <br/> NID. uid = idr_mainframe; <br/> NID. uflags = nif_icon | nif_message | nif_tip; <br/> NID. ucallbackmessage = wm_showtask; // <br/> NID. hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe); <br/> strcpy (NID. sztip, "measure... "); // <br/> shell_policyicon (nim_delete, & nid); // <br/>}

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.