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 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
} 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 a single-document or multi-document 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
// The menu resource idr_right_menu needs to be defined by yourself
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 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.
I am a newbie. If there are any mistakes, please criticize and advise me. QQ: 1078285863. Technical exchanges are welcome.