In MFC, how does one minimize the window to the tray?

Source: Internet
Author: User

(1) 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.
(2) Program Implementation
1. Custom message wm_showtask: # define wm_showtask (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 on_message (wm_showtask, onshowtask) to message ing, where wm_showtask is the message name,
Onshowtask is a self-defined message response function, which is described later.
(3) specific function content
1. minimize to the tray Function
Void cmydlg: totray () {policyicondata 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 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, 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); // 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"); // you can specify the position of the pop-up menu. trackpopupmenu (tpm_leftalign, lpoint-> X, lpoint-> Y, this); // Resource Recycling hmenu = menu. detach (); menu. destroymenu (); Delete lpoint;} break; Case wm_lbuttondblclk: // double-click the left button to process {This-> showwindow (sw_show ); // deletetray ();} break; default: break;} return 0 ;}

3. Delete the tray icon Function
Void cmydlg: deletetray () {policyicondata 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 prompt bar is "scheduled task reminder" shell_policyicon (nim_delete, & nid); // Delete the icon in the tray area}

 

Additional article:

VC minimization program to tray

1. notifyicondata Structure

The notifyicondata structure contains the information used by the system to process the tray icons, including the selected icons, callback messages, prompt messages, and corresponding windows of the icons. It is defined:

Typedef struct _ policyicondata {
DWORD cbsize;
// The size of the structure in bytes
Hwnd;
// Handle of the window for receiving Tray Icon notification messages
Uint uid; // ID of the icon defined by the application
Uint uflags;
// Set attributes of the icon
Uint ucallbackmessage;
// Message ID defined by the application. The message is sent to hwnd.
Hicon;
// Handle of the icon
Char sztip [64];
// The prompt message displayed when you move the cursor over the icon
} Notifyicondata, * ppolicyicondata;
In this structure, the uflags member can make one or more of the following combinations:
The specified hicon member is valid.
The value of the nif_message setting Member ucallbackmessage is valid.
The error message returned when the member sztip is set to valid.

Ii. shell_policyicon Function
The global function shell_policyicon () is used to add, delete, or modify icons on the tray. Its prototype is:

Winshellapi bool winapi shell_policyicon (DWORD dwmessage, pnotifyicondata pnid );

Pnid is the pointer of the above policyicondata structure; dwmessage is the passed message, which can be one of the following messages:
Nim_add add icon
Nim_delete Delete icon
Nim_modify

Iii. Tray Icon Program Design Example
First, we use Appwizard to create a dialog box-based application cuserdlg. M_nid is a class member variable.

Void cuserdlg: totray ()
{
M_nid.cbsize = (DWORD) sizeof (policyicondata );
M_nid.hwnd = This-> m_hwnd;
M_nid.uid = idr_mainframe;
M_nid.uflags = nif_icon | nif_message | nif_tip;
M_nid.ucallbackmessage = wm_showtask;

// The custom message name wm_showtask header function is defined as wm_user + 1
M_nid.hicon = loadicon (AfxGetInstanceHandle (), makeintresource (idr_mainframe ));
Strcpy (m_nid.sztip, "tray icon"); // The content displayed when you place the cursor over it
Shell_policyicon (nim_add, & m_nid); // Add an icon in the tray Area
}

4. Add the response function afx_msg lresult onshowtask (wparam, lparam) for the message wm_showtask );

Add code between begin_message_map (cuserdlg, cdialog) and end_message_map:

On_message (wm_showtask, onshowtask)

Compile this function:

Lresult cuserdlg: onshowtask (wparam, lparam) // wparam receives the icon ID and lparam receives the mouse action.
{
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 (already
// Hide) to end the program.
Menu. appendmenu (mf_string, wm_destroy, "quit ");
// Determine 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_shownormal); // simple display of the Main Window
}
Break;
}
Return 0;
}

Then, call totray () in the oninitdialog function ();

5. When the application exits, the icon disappears, maps the wm_destroy message, and adds the following to the ondestroy () function:
: Shell_policyicon (nim_delete, & m_tnid );

Sat. Now, we have implemented the common functions of the tray icon program. We can also use the shell_policyicon () function to call different status indicators, just as Kingsoft's icon changes when the main window is opened and when the word is paused. It is often used to display the status of some systems. For example, system time, volume control, and other icons

This is the general idea.

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.