MFC design Tray Icon Program

Source: Internet
Author: User

Design the tray icon program in VisualC 6.0

Source:Internet Author:Western Digital Time:2008-04-09

Abstract: This article describes in detail how to design the tray icon program with VC 6.0.

  Keywords: VC 6.0, Tray Icon Program

In Windows 95/98/NT/2000/XP, there are several icons on the right of the taskbar, such as the input method switch icon and volume control icon, in addition, we often encounter software with tray icons, such as Kingsoft, anti-virus software with real-time monitoring functions. These software runs in the background, usually does not occupy too much screen resources, just put a small sign on the notification bar. If necessary, you can click the icon to perform menu operations or activate its main window. Sometimes our own program also hopes to have a similar effect. This article describes in detail how to design this type of Tray Icon program with VC 6.0.

  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 for receiving the notification message from the tray icon
Uint uid; // ID of the icon defined by the application
Uint uflags; // set the attributes of the icon
Uint ucallbackmessage; // the ID of the message defined by the application. The message is sent to hwnd.
Hicon; // the handle of the icon
Char sztip [64]; // The prompt message displayed when you move your 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.

  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 a pointer to the policyicondata structure; dwmessage is a transmitted message, which can be one of the following messages:

Nim_add add icon
Nim_delete Delete icon
Nim_modify

  Tray Icon Program Design Example

---- First, we use Appwizard to create a single-document application my based on the document/view structure. Add the protection member variable m_tnid of the policyicondata structure to the cmainframe class, and add the code to generate the tray icon before the return statement in the oncreate function:

M_tnid.cbsize = sizeof (policyicondata );
M_tnid.hwnd = This-> m_hwnd;
M_tnid.uflags = nif_message | nif_icon | nif_tip;
M_tnid.ucallbackmessage = mywm_policyicon;
// User-defined callback message
Cstring sztooltip;
Sztooltip = _ T ("tray icon instance ");
_ Tcscpy (m_tnid.sztip, sztooltip );
M_tnid.uid = idr_mainframe;
Hicon;
Hicon = afxgetapp ()-> loadicon (idr_mainframe );
M_tnid.hicon = hicon;
: Shell_policyicon (nim_add, & m_tnid );
If (hicon): destroyicon (hicon );
---- The ID of the callback message should be defined in the header function of the main framework class:

# Define mywm_policyicon wm_user 1
---- To process the icon callback message, such as double-clicking with the left mouse button or right-clicking the message, we reload the windowproc () function. In addition, we also hope that the icon will not appear in the blank area of the taskbar when the main frame window is minimized, and the corresponding processing will be done in this function at the same time.

Lresult cmainframe: windowproc (uint message, wparam, lparam)
{
Switch (Message ){
Case mywm_policyicon:
// For user-defined messages
If (lparam = wm_lbuttondblclk)
{
// The main window appears when you double-click the mouse
Afxgetapp ()-> m_pmainwnd-> showwindow (sw_show );
}
Else if (lparam = wm_rbuttondown ){
// Right-click the pop-up menu
Cmenu menu;
Menu. loadmenu (idr_right_menu );
// Load the predefined menu
Cmenu * pmenu = menu. getsubmenu (0 );
Cpoint Pos;
Getcursorpos (& Pos );
Pmenu-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, POS. X, POS. Y, afxgetmainwnd ());
}
Break;
Case wm_syscommand:
// If it is a system message
If (wparam = SC _minimize ){
// The main window is hidden when the minimum message is received
Afxgetapp ()-> m_pmainwnd-> showwindow (sw_hide );
Return 0;
}
Break;
}
Return cframewnd: windowproc (message, wparam, lparam );
}
---- To cause the icon to disappear when the application exits, map the wm_destroy message and add it to the ondestroy () function:

: Shell_policyicon (nim_delete, & m_tnid );
---- Now, we have implemented 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.

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.