http://www.csdn.net/expert/topic/361/361011.xml?temp=.4378931

解决方案 »

  1.   

    Shell_NotifyIcon
    Sends a message to the taskbar's status area.BOOL Shell_NotifyIcon(
        DWORD dwMessage, 
        PNOTIFYICONDATA lpdata
    );Parameters
    dwMessage 
    [in] Variable of type DWORD that specifies the action to be taken. It can have one of the following values: 
    NIM_ADD 
    Adds an icon to the status area. The hWnd and uID members of the NOTIFYICONDATA structure pointed to by pnid will be used to identify the icon in future calls to Shell_NotifyIcon. 
    NIM_DELETE 
    Deletes an icon from the status area. Use the hWnd and uID members of the NOTIFYICONDATA structure pointed to by pnid to identify the icon to be deleted. 
    NIM_MODIFY 
    Modifies an icon in the status area. Use the hWnd and uID members of the NOTIFYICONDATA structure pointed to by pnid to identify the icon to be modified. 
    NIM_SETFOCUS 
    Version 5.0. Returns focus to the taskbar notification area. Taskbar icons should use this message when they have completed their user interface operation. For example, if the taskbar icon displays a shortcut menu, but the user presses ESCAPE to cancel it, use NIM_SETFOCUS to return focus to the taskbar notification area. 
    NIM_SETVERSION 
    Version 5.0. Instructs the taskbar to behave according to the version number specified in the uVersion member of the structure pointed to by pnid. This message allows you to specify whether you want the version 5.0 behavior found on Microsoft® Windows® 2000 systems, or that found with earlier Shell versions. The default value for uVersion is zero, indicating that the original Windows 95 notify icon behavior should be used. For details, see the Res section. 
    pnid 
    [in] Address of a NOTIFYICONDATA structure. The content of the structure depends on the value of dwMessage. 
    Return Values
    Returns TRUE if successful, or FALSE otherwise. If dwMessage is set to NIM_SETVERSION, the function returns TRUE if the version was successfully changed, or FALSE if the requested version is not supported.Res
    The taskbar notification area is sometimes erroneously called the "tray."Version 5.0 of the Shell, found on Windows 2000, handles Shell_NotifyIcon mouse and keyboard events differently than earlier Shell versions, found on Windows NT® 4.0, Windows 95, and Windows 98. The differences are:If a user selects a notify icon's shortcut menu with the keyboard, the version 5.0 Shell sends the associated application a WM_CONTEXTMENU message. Earlier versions send WM_RBUTTONDOWN and WM_RBUTTONUP messages. 
    If a user selects a notify icon with the keyboard and activates it with the space bar or ENTER key, the version 5.0 Shell sends the associated application an NIN_KEYSELECT notification. Earlier versions send WM_RBUTTONDOWN and WM_RBUTTONUP messages. 
    If a user selects a notify icon with the mouse and activates it with the ENTER key, the version 5.0 Shell sends the associated application an NIN_SELECT notification. Earlier versions send WM_RBUTTONDOWN and WM_RBUTTONUP messages. 
    If a user passes the mouse pointer over an icon with which a balloon ToolTip is associated, the version 5.0 Shell sends the following messages: 
    NIN_BALLOONSHOW - Sent when the balloon is shown (balloons are queued). 
    NIN_BALLOONHIDE - Sent when the balloon disappears—when the icon is deleted, for example. This message is not sent if the balloon is dismissed because of a timeout or mouse click by the user. 
    NIN_BALLOONTIMEOUT - Sent when the balloon is dismissed because of a timeout. 
    NIN_BALLOONUSERCLICK - Sent when the balloon is dismissed because the user clicked the mouse. 
    You can select which way the Shell should behave by calling Shell_NotifyIcon with dwMessage set to NIM_SETVERSION. Set the uVersion member of the NOTIFYICONDATA structure to indicate whether you want version 5.0 or pre-version 5.0 behavior. Note The messages discussed above are not conventional Windows messages. They are sent as the lParam value of the application-defined message that is specified when the icon is added with NIM_ADD.Requirements 
      Version 4.00 and later of Shell32.dll  Windows NT/2000: Requires Windows NT 4.0 or later. 
      Windows 95/98/Me: Requires Windows 95 or later. 
      Header: Declared in Shellapi.h. 
      Import Library: Shell32.lib.
      

  2.   

    msdn里查找trayicon可以查到相关的源代码
      

  3.   

    访问系统托盘的方法是通过Shell_NotifyIcon函数和NOTIFYICONDATA结构实现的。  
    typedef struct _NOTIFYICONDATA {
       DWORD cbSize;
       HWND hWnd;
       UINT uID;
       UINT uFlags;
       UINT uCallbackMessage;
       HICON hIcon;
       TCHAR szTip[64];
       DWORD dwState;         //Version 5.0
       DWORD dwStateMask;     //Version 5.0
       TCHAR szInfo[256];     //Version 5.0
       UINT uTimeout;         //Version 5.0
       TCHAR szInfoTitle[64]; //Version 5.0
       DWORD dwInfoFlags;     //Version 5.0
    } NOTIFYICONDATA, *PNOTIFYICONDATA;     为了要在系统托盘中显示图标,用NIM_ADD标志调用Shell_NotifyIcon函数。  
    #define ID_TASKBARICON 100
    #define WM_ICONNOTIFY           (WM_USER + 101)NOTIFYICONDATA nid;// 初始化系统托盘图标
    nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = hWnd;
    nid.uID = ID_TASKBARICON;
    nid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
    nid.uCallbackMessage = WM_ICONNOTIFY;
    nid.hIcon = LoadImage(hInst, MAKEINTRESOURCE(IDI_TRAY1), IMAGE_ICON, 16, 16, 0);
    strcpy(nid.szTip, "My Tooltip Text");
    Shell_NotifyIcon(NIM_ADD, &nid);    cbSize成员是结构的大小(使用它主要是为了支持将来这个结构大小增加)。
        hWnd是窗口句柄。当图标发生某事件时(如单击、双击等),Windows将向窗口发送uCallbackMessage成员指定的消息。uID成员指定与图标关联的ID。它不是很重要,除非你需要显示并跟踪几个图标。
        uFlag成员告诉Windows应该读取哪个成员。当添加一个图标时,应该包含这个结构的大多数成员。当更新图标时,如只是需要改变图标时,你只要设置相应
    的标志就可以了。    hIcon成员是你想显示的图标。
        最后,szTip成员是提示文本。设置好这些结构成员后,调用Shell_NotifyIcon函数。    当与图标关联的事件发生时,Windows将发送uCallbackMessage成员指定的消息。IParam包含发送的消息。当获得WM_LBUTTONDBLCLK消息时显示主窗口
    或者启动主程序。当获得WM_RBUTTONUP消息时显示菜单。    注意:如果在系统托盘中单击鼠标右键,有时会有一个弹出式(上下文菜单)菜单显示/消失的怪现象,详细信息击解决办法请参阅微软知识库文章Q135788,也可以参考下列代码加以解决。  
    switch(nMsg) {
       case WM_ICONNOTIFY:
          switch(lParam) {
             case WM_LBUTTONDBLCLK:
                // Load main window here
                break;
             case WM_RBUTTONUP:
                {
                   POINT point;
                   HMENU hMenu, hSubMenu;
                   // Get mouse position
                   GetCursorPos(&point);
                   // Popup context menu
                   hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MYMENU));
                   hSubMenu = GetSubMenu(hMenu, 0);
                   SetMenuDefaultItem(hSubMenu, IDM_DEFAULTCMD, FALSE);
                   SetForegroundWindow(hMainDlg);         // Per KB Article Q135788
                   TrackPopupMenu(hSubMenu,
                      TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_LEFTALIGN,
                      point.x, point.y, 0, hWnd, NULL);
                   PostMessage(hMainDlg, WM_NULL, 0, 0);   // Per KB Article Q135788
                   DestroyMenu(hMenu);
                }
                break;
             default:
                return FALSE;
          }
    }    不论什么时候,你都可以用 NIM_MODIFY 调用 Shell_NotifyIcon。程序终止之前,用 NIM_DELETE 调用 Shell_NotifyIcon从托盘中清除图标。Shell_NotifyIcon(NIM_DELETE, &nid);  
      

  4.   

    #define WM_TRAY_NOTIFY WM_USER+0NOTIFYICONDATA nTrayData;nTrayData.cbSize=sizeof(nTrayData);
    nTrayData.hWnd=m_hWnd;
    nTrayData.hIcon=LoadIcon(AfxGetInstanceHandle(),
                             MAKEINTRESOURCE(IDR_MAINFRAME));
    nTrayData.uID=0;
    nTrayData.uCallbackMessage=WM_TRAY_NOTIFY;//传递鼠标消息,由别的函数接收
    strcpy(nTrayData.szTip,"some tips");
    nTrayData.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;Shell_NOTIfyIcon(NIM_ADD,&nTrayData);