Design the tray icon program with VC

Source: Internet
Author: User
In Windows, there are several icons on the right side 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 for real-time monitoring, etc. 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 choose a menu or activate its main window. Sometimes our own program also hopes to have a similar effect. This article will introduce in detail how to design the tray icon program with VC.

   1. notifyicondata Structure

The notifyicondata structure contains the information used by the system to process the tray icon, including the selected icon, callback message, prompt message, and window corresponding to the icon. 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; // the handle of the icon

Char sztip [64]; // The prompt message displayed when you move your cursor over the icon

} Policyicondata ,? Pnotifyicondata;

In this structure, the member uflags can be a combination of the following or one of them:

Nif_icon: Set the member hicon to be valid.

Nif_message: Set the ucallbackmessage to be valid.

Nif_tip: Set the member sztip to be 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 to the policyicondata structure above.

Dwmessage is a transmitted message and can be one of the following messages:

Nim_add: add an icon

Nim_delete: delete icon

Nim_modify: Modify the icon

   Iii. Tray Icon Program Design Example

First, we use Appwizard to create an application tray that is not based on the document and view structure. We do not want to display the main window when the application starts. Therefore, we need to delete the following two codes of the initinstance () member function in the application class ctrayapp:

Pframe-> activateframe ();

Pframe-> showwindow (sw_show );

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 returned message should be defined in the header function of the main framework class:

# Define mywm_policyicon wm_user + 1

To process the returned message of the icon, for example, double-click the left mouse button or right-click 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 menu

Cmenu menu;

Menu. loadmenu (idr_right_menu); // load the previously defined menu

Cmenu? Pmenu = menu. getsubmenu (0 );

Cpoint Pos;

Getcursorpos (& amp; 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 remove the icon when the application exits, map the wm_destroy message and add the following to the ondestroy () function:

: Shell_policyicon (nim_delete, & m_tnid );

Now, we have implemented the common functions of the tray icon program. We can also call the shell_policyicon () function to change different status indicators.

This program is debugged in VC ++ 6.0 and Windows 98/2000 professional

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.