VC ++ minimizes the window MFC

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.

If (nid = SC _minimize) totray (); // The function for minimizing to the tray

3. Add the following code between begin_message_map (cmy2009dlg, cdialog) 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 cmydlg: totray ()
{
Notifyicondata NID;
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, "program name"); // message tip
Shell_policyicon (nim_add, & nid); // Add an icon in the tray Area
Showwindow (sw_hide); // hide the Main Window
}

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 cmydlg: onshowtask (wparam, 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); // get the cursor position
Cmenu menu;
Menu. createpopupmenu (); // declare a pop-up menu

// Add the menu item "close" and click it to send the message wm_destroy to the main window to end the program.

Menu. appendmenu (mf_string, wm_destroy, "close"); // you can specify the position of the pop-up menu.

Menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this); // reclaim Resources

Hmenu = menu. Detach ();
Menu. destroymenu ();
Delete lpoint;
} Break;
Case wm_lbuttondblclk: // double-click the left button.
{This-> showwindow (sw_show); // display the Main Window

Deletetray ();
} Break;
Default: break;
}
Return 0;
}

6. Delete the tray icon Function
Void cmydlg: deletetray ()
{
Notifyicondata NID;
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, "program name"); // The message is scheduled task reminder"
Shell_policyicon (nim_delete, & nid); // Delete the icon in the tray Area
}

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.