vc6.0 tray icon

Source: Internet
Author: User

In the Windows operating system, the right side of the taskbar (tray) will be permanent several icons, such as input method toggle icon, Volume control icon, etc., in addition we often encounter with the tray icon software, such as PowerWord, real-time monitoring function of anti-virus software. These software run in the background, usually do not occupy too much screen resources, only a small logo on the notification bar, if necessary, we can use the mouse to click on the icon to menu operation or activate its main window. Sometimes we write our own program also want to have similar effect, this article will be introduced in detail with VC design tray icon Program method.

First, NOTIFYICONDATA structure
The NOTIFYICONDATA structure contains information that the system uses to process the tray icon , which includes the selected icon, callback message, prompt message, and the window corresponding to the icon. It is defined as:

typedefstruct-notifyicondata {DWORD cbsize; //The size of this structure in bytesHWND hwnd; //window handle to receive tray icon notification messageUINT UID; //The ID number of the application-defined iconUINT uflags; //set the properties of the iconUINT Ucallbackmessage; //The application-defined message ID number that this message is passed to the HWNDHicon Hicon;//Handle to Icon    Charsztip[ -];//the message that the mouse pauses on the icon to display} notifyicondata, Pnotifyicondata; 

In this structure, the member Uflags can be one of the following combinations or either:

Nif_icon: Set member Hicon valid

Nif_message: Set member Ucallbackmessage valid

Nif_tip: Set member Sztip valid

Second, Shell_NotifyIcon function
The global function Shell_NotifyIcon () is used to add, remove, or modify icons on the pallet. The prototype is:

Winshellapi BOOL WINAPI Shell_NotifyIcon (DWORD dwmessage,pnotifyicondata  

Pnid is a pointer to the NOTIFYICONDATA structure above.

Dwmessage is the message that is being passed, and can be one of the following messages:

Nim_add: Add icon

Nim_delete: Delete icon

Nim_modify: Modify icon

Three, tray icon Programming 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, so we need to delete the following two lines of the member function InitInstance () in the Application class Ctrayapp:

pframe-〉activateframe (); Pframe//

Add the protected member variable M_tnid of the NOTIFYICONDATA structure to the CMainFrame class and add the code to generate the tray icon before the return statement in its oncreate function:

m_tnid.cbsize=sizeof(Notifyicondata); M_tnid.hwnd= This-〉m_hwnd; M_tnid.uflags=nif_message| nif_icon|Nif_tip; M_tnid.ucallbackmessage=Mywm_notifyicon; //user-defined callback messagesCString Sztooltip; Sztooltip=_t ("Example Tray icon");   _tcscpy (M_tnid.sztip, Sztooltip); M_tnid.uid=Idr_mainframe;   Hicon Hicon; Hicon=afxgetapp ()-〉loadicon (IDR_MAINFRAME); M_tnid.hicon=Hicon; :: Shell_NotifyIcon (Nim_add,&&M_tnid); if(HICON)::D Estroyicon (HICON);


The ID of the return message should be defined in the header function of the main framework class:

#define MYWM_NOTIFYICON wm_user+1

In order to handle the return message of the icon, such as the left mouse button double-click, right-clicking the message, we reload the WindowProc () function. In addition, we want the icon not to appear in the blank area of the taskbar when the main frame window is minimized, and to handle this function at the same time.

LRESULT Cmainframe::windowproc (UINT message, WPARAM WPARAM, LPARAM LPARAM) {Switch(message) { CaseMywm_notifyicon://If a user-defined message    if(lparam==WM_LBUTTONDBLCLK) {//The main window appears when you double-click the mouseAfxGetApp ()-〉m_pmainwnd-〉showwindow (sw_show); }Else if(Lparam==wm_rbuttondown) {//Right-click popup menuCMenu menu; Menu. LoadMenu (Idr_right_menu); //load pre-defined menusCmenupmenu=menu. GetSubMenu (0);   CPoint POS; GetCursorPos (&&POS); Pmenu-〉trackpopupmenu (tpm_leftalign|Tpm_rightbutton,pos.x,pos.y,afxgetmainwnd ()); } Break;  CaseWm_syscommand://If this is a system message    if(wparam= =sc_minimize) {//The main window hides when a minimized message is receivedAfxGetApp ()-〉m_pmainwnd-〉showwindow (sw_hide); return 0; } Break; }returncframewnd::windowproc (Message, WParam, LParam); } 


To make the icon disappear when the application exits, map the Wm_destroy message and add it in the OnDestroy () function:

At this point, we have implemented the general functions of the tray icon program. We can also implement the change of different state markers through the invocation of the Shell_NotifyIcon () function.
The flashing of the tray icon is in a timer, with a blank icon icon, and the original tray icon to replace each other

   voidCmainframe::ontimer (UINT nidevent) {//todo:add your message handler code here and/or call default    if(Nidevent = =1)    {        //Flashing of taskbar window        if(!m_bisshow)        {FlashWindow (TRUE); }        Else{FlashWindow (FALSE); }        //flashing of the tray iconHicon Hicon; if(M_ntimes = =0) {M_ntimes=1; Hicon=afxgetapp ()LoadIcon (IDR_MAINFRAME); }        Else if(m_ntimes==1) {M_ntimes=0; Hicon=afxgetapp ()LoadIcon (Idi_icon_none); } M_trayicon.hicon=Hicon; :: Shell_NotifyIcon (Nim_modify,&M_trayicon); if(HICON) {::D Estroyicon (HICON); }} cframewnd::ontimer (Nidevent);}

This program in VC + + 6.0 debugging through.

vc6.0 tray icon

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.