MFC adds a tray for the application (right-click the tray and the pop-up menu)

Source: Internet
Author: User

Source code: http://download.csdn.net/detail/nuptboyzhb/4137784

1. Import the resource (. ico) format of a tray icon; resource ID is idi_icon1

2. In the initialization function of the Framework Program, initialize a struct of a tray icon

Notifyicondata
Yyicondata; // notifyicondata Structure


Notifyicondata. cbsize = sizeof (notifyicondata); // allocate space for the notifyicondata struct

// Assign values to parameters of policyicondata struct


Policyicondata. hwnd = m_hwnd; // window handle


Policyicondata. uid = idi_policyicon; // ID


Yyicondata. uflags = nif_message | nif_icon | nif_tip;


Yyicondata. ucallbackmessage = wm_addtrayicon; // the ID of the callback message. We need to add a user message ing for the ID of the callback message.


Notifyicondata. hicon = afxgetapp ()-> loadicon (idi_icon1); // load the tray icon


Wcscpy (policyicondata. sztip,
_ T ("Create a tray icon for the program "));

Shell_policyicon (nim_add, & policyicondata); // Load Tray Icon

Note: There are several variables worth attention in this step:

A. m_hwnd is the handle of the entire application window.

B. idi_policyicon is a constant defined by us. Add the following code to the header file:

# Define idi_policyicon 100 // Tray Icon ID

C. wm_addtrayicon is the identifier of the custom message and a constant;

# Define wm_addtrayicon wm_user + 101 // custom message

3. Add User message ing and message processing functions

1. Add a member function:

Afx_msg lresult onaddtrayicon (wparam, lparam );

2. Add message ing

On_message (wm_addtrayicon, onaddtrayicon) // Add message ing

Note: The first parameter is the user-defined message identifier, and the second parameter is the message response processing function.

3. onaddtrayicon

Lresult Class Name: onaddtrayicon (wparam, lparam)

{

If (wparam = idi_policyicon)// Create a tray icon

{

Switch (lparam) // Message Type

{


Case wm_lbuttondblclk: // double-click the left button

// Hide or display the internship window

// Afxgetapp ()-> m_pmainwnd-> showwindow (sw_hide); // hide the window


Break;


Case wm_rbuttondblclk: // right-click

{

Afxgetapp ()-> m_pmainwnd-> destroywindow (); // close the window


Break;

}


Case wm_rbuttondown: // right-click the tray and display the menu

{


Cmenu menu, * psubmenu; // The cmenu object to be used later


Cpoint point;


Menu. loadmenu (idr_popup_menu); // load the custom right-click menu


Psubmenu = menu. getsubmenu (0); // obtain the first pop-up menu

Getcursorpos (& Point); // obtain the current cursor position psubmenu-> trackpopupmenu (tpm_leftalign, point. X, point. Y, this );


Break;

}

}

}


Return 1;

}

4. Unload the tray.

When the application exits, uninstall

Notifyicondata
Notifyicondata;


Policyicondata. cbsize = sizeof (policyicondata );


Policyicondata. uflags = 0;


Policyicondata. hwnd = m_hwnd;

Notifyicondata. uid
= Idi_policyicon;

Shell_policyicon (nim_delete, & policyicondata); // unload the tray icon

 

 

The ucallbackmessage variable of the notifyicondata struct, which associates the user message response with the click and other messages of the tray. the ID of the trayIdi_policyiconIt is exactly the basis for the user message response to determine whether the message is delivered by the tray;

 

 

In multi-document or single-document-based applications, When you click the Minimize button, we can hide the interface;

Specifically, add an onsize () Message

Void cmainframe: onsize (uint ntype, int CX, int CY)

{


Cframewnd: onsize (ntype,

CX,
Cy );


If (ntype = size_minimized)

{


Showwindow (sw_hide); // hide the main window when minimized

}

// Todo: add the message processing program code here

}

The minimal tray of the dialog box is similar here, you can refer to: http://justtotry.blog.163.com/blog/static/446308122011622101427208/

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.