1.添加消息处理函数

 

添加WM_SYSTEMTRAY消息处理函数
BEGIN_MESSAGE_MAP(CPcmserverDlg, CDialog)
    
//{{AFX_MSG_MAP(CPcmserverDlg)
        ...
    ON_MESSAGE(WM_SYSTEMTRAY, OnSystemTray)
    ...
        
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


LRESULT C
***Dlg::OnSystemTray(WPARAM wParam, LPARAM lParam)
{
    
switch( lParam )
    {
    
case WM_LBUTTONDOWN:
        {
            
this->ShowWindow(SW_NORMAL);
            DeleteTray();
//销毁Tray
        }
        
break;
    
case WM_RBUTTONDOWN:
                {
                        
//右键弹出托盘的菜单
                 CMenu menu;
                 menu.LoadMenu(IDR_TRAYMENU);
                 POINT pt;
                 ::GetCursorPos(
&pt);
                 menu.GetSubMenu(
0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTALIGN, pt.x, pt.y, AfxGetMainWnd());
        }
                
break;
    }

    
return 1;
}
 
void C***Dlg::DeleteTray()
{
   NOTIFYICONDATA nid;
   nid.cbSize 
= sizeof( NOTIFYICONDATA );
   nid.hWnd 
= m_hWnd;
   nid.uID 
= IDR_MAINFRAME;
   nid.uFlags 
= 0;
   ::Shell_NotifyIcon( NIM_DELETE,
&nid );
}

 

 

2.要添加Tray 可以用以下的函数

 

添加Tray
void C***Dlg::AddTray()
{
    NOTIFYICONDATA nid;
    nid.cbSize 
= sizeof( NOTIFYICONDATA );
    nid.hWnd 
= m_hWnd; // handle of window that will receive
    
// messages from icon
    nid.uID = IDR_MAINFRAME; // id for this icon
    nid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;// the next three parameters are valid
    nid.uCallbackMessage = WM_SYSTEMTRAY;// message that icon sends when clicked
    nid.hIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME );// icon

    _tcscpy(nid.szTip, _T(
"tip name"));
    
    
// bubble help message for icon
    ::Shell_NotifyIcon( NIM_ADD,&nid );
}

 

 

3.我们可以在点击系统菜单消息的时候把窗口隐藏在托盘中

 

响应最小化按钮,隐藏窗口,显示托盘图标
void C***Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    
if ((nID & 0xFFF0== IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    
else if(nID == SC_MINIMIZE)     
         {
//比如在最小化窗口的时候隐藏任务栏图标,显示托盘图标

                
this->ShowWindow(SW_HIDE);

        AddTray();
        }
    
else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

 

 

posted on 2009-12-17 10:42  米罗21  阅读(238)  评论(0编辑  收藏  举报