⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 systrayicon.cpp

📁 <Visual C++ 网络程序设计实例详解>配套源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 Return Value : BOOL (TRUE if the icon is hidden, else FALSE)
----------------------------------------------------------------------------*/

BOOL CSysTrayIcon::HideIcon(UINT nIconID)
{
    BOOL bIconHidden = FALSE;
    
    NOTIFYICONDATA* pnidIconInfo = GetNotifyIconDataFromID(nIconID);
    
    if (pnidIconInfo != NULL)
    {
        // The NOTIFYICONDATA.uFlags signifies that the icon in the system tray is
	    // not visible.     
	    pnidIconInfo->uFlags = 0;
	    
	    bIconHidden = ::Shell_NotifyIcon(NIM_DELETE, pnidIconInfo);
    }
    	
	return bIconHidden;
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::DeleteIcon()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Hides an icon from the system tray. Also deletes it from 
            memory

 Parameters : The icon ID
	
 Return Value : BOOL (TRUE if the icon is hidden & deleted , else FALSE)
----------------------------------------------------------------------------*/

BOOL CSysTrayIcon::DeleteIcon(UINT nIconID)
{
    BOOL bIconHidden = FALSE;
    
    NOTIFYICONDATA* pnidIconInfo = GetNotifyIconDataFromID(nIconID);
    
    if (pnidIconInfo != NULL)
    {
        // The NOTIFYICONDATA.uFlags signifies that the icon in the system tray is
	    // not visible.     
	    pnidIconInfo->uFlags = 0;
	    
	    bIconHidden = ::Shell_NotifyIcon(NIM_DELETE, pnidIconInfo);
	    
	    DeleteNotifyIconDataFromList(pnidIconInfo);
    }
    	
	return bIconHidden;
}




/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::GetNotifyIconDataFromID()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Gets the associated NOTIFYICONDATA struct from the icon ID

 Parameters : The icon ID
	
 Return Value : NOTIFYICONDATA* (NOTIFYICONDATA pointer, if available, 
                else NULL)
----------------------------------------------------------------------------*/

NOTIFYICONDATA* CSysTrayIcon::GetNotifyIconDataFromID(int nIconID)
{
    POSITION pos = m_NotifyIconDataList.GetHeadPosition();
    NOTIFYICONDATA* pnidIconInfo = NULL;                
    
    while (pos != NULL)
    {
        pnidIconInfo = m_NotifyIconDataList.GetNext(pos);
        
        if (pnidIconInfo->uID == nIconID)
        {
            return pnidIconInfo; // We found the rqeuired NOTIFYICONDATA
        }
    }
    
    return NULL;
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::DeleteNotifyIconDataFromList()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Deletes the icon and it's NOTIFYICONDATA struct from linked list
            m_NotifyIconDataList

 Parameters : The icon ID
	
 Return Value : BOOL (TRUE if possible to delete the icon with the struct
                passed as the parameter, else FALSE)
----------------------------------------------------------------------------*/

BOOL CSysTrayIcon::DeleteNotifyIconDataFromList(NOTIFYICONDATA* pnidIconInfoToBeDeleted)
{
    BOOL bIconInfoDeleted = FALSE;
    
    POSITION pos = m_NotifyIconDataList.GetHeadPosition();
    NOTIFYICONDATA* pnidIconInfo = NULL;
        
    while (pos != NULL)
    {   
        // We did not use m_NotifyIconDataList.GetNext() because, in the case
        // we enter the 'if' case below, the RemoveAt() will point to the next 
        // element rather than the element we want to delete.
        pnidIconInfo = m_NotifyIconDataList.GetAt(pos);
        
        if (pnidIconInfo == pnidIconInfoToBeDeleted)
        {
            m_NotifyIconDataList.RemoveAt(pos);
            delete pnidIconInfo;
            
            bIconInfoDeleted = TRUE;
            
            break; // We found the NOTIFYICONDATA to be deleted
        }
        
        // Move to the next element
        m_NotifyIconDataList.GetNext(pos);
    }
    
    return bIconInfoDeleted; 
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::CheckIfIconIDExists()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Checks if an icon with the requested ID exists

 Parameters : The icon ID
	
 Return Value : BOOL (TRUE if icon with this ID exists in the linked list
                m_NotifyIconDataList, else FALSE)
----------------------------------------------------------------------------*/

BOOL CSysTrayIcon::CheckIfIconIDExists(UINT nIconID)
{   
    BOOL bIconIDExists = FALSE;
    
    POSITION pos = m_NotifyIconDataList.GetHeadPosition();
    
    while (pos != NULL)
    {
        NOTIFYICONDATA* pnidIconInfo = m_NotifyIconDataList.GetNext(pos);
        
        if (pnidIconInfo->uID == nIconID)
        {
            bIconIDExists = TRUE;
            
            break;
        }
    } 
    return bIconIDExists;
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnLButtonDown()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user clicks
            and icon with the left mouse button

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnLButtonDown(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnRButtonDown()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user clicks
            and icon with the right mouse button

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnRButtonDown(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnMButtonDown()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user clicks
            and icon with the right mouse button

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnMButtonDown(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnLButtonUp()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user releases
            the left mouse button over the icon

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnLButtonUp(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnRButtonUp()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user releases
            the right mouse button over the icon

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnRButtonUp(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnMButtonUp()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user releases
            the middle mouse button over the icon

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnMButtonUp(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnLButtonDblClk()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user double
            clicks the left mouse button over the icon

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnLButtonDblClk(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnRButtonDblClk()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user double
            clicks the right mouse button over the icon

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnRButtonDblClk(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnMButtonDblClk()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user double
            clicks the middle mouse button over the icon

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnMButtonDblClk(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnLButtonDblClk()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called by the window m_wndTrayMsgReceiver when the user moves the
            mouse over the icon.

 Parameters : 1. nIconID -> The icon ID
              2. ptMouse -> screen co-ordinates of the mouse
	
 Return Value : void
----------------------------------------------------------------------------*/

void CSysTrayIcon::OnMouseMove(UINT nIconID, CPoint ptMouse)
{
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::OnContextMenu()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Called to initialize a menu over the icon, and set the foreground
            window for proper message routing.

 Parameters : 1. CMenu* -> Pointer to the menu
              2. ptMouse -> screen co-ordinates of the mouse
              3. pWndMessageHandler -> Window that will handle the message
	
 Return Value : void
----------------------------------------------------------------------------*/

BOOL CSysTrayIcon::OnContextMenu(CMenu* pContextMenu, CPoint ptMouse, CWnd* pWndMessageHandler)
{
    // The window is brought forward, so that it starts 
    // receving messages, rather than currently active window.
    // If we don't do this the messages of the context menu will
    // go to the currently active window.
    pWndMessageHandler->SetForegroundWindow();
	
    BOOL bResult = pContextMenu->TrackPopupMenu(
                       TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
                       ptMouse.x, 
                       ptMouse.y, 
                       pWndMessageHandler
                   );
    
    return bResult;
}



/*-----------------------------------------------------------------------------
 Function : CSysTrayIcon::ChangeIconTip()

 Created: Dec 5, 2002
 Author:  Prateek Kaul
 e-mail:  kaulpr@yahoo.com

 Abstract : Changes the tool tip of the icon

 Parameters : 1. nIconID -> ID of the icon whose tool tip to be changed
              2. strTip -> New tip of the icon
              	
 Return Value : void
----------------------------------------------------------------------------*/
BOOL CSysTrayIcon::ChangeIconTip(UINT nIconID, CString strTip)
{
    BOOL bIconTipChanged = FALSE;
    TCHAR* szTip = strTip.GetBuffer(0);
    
    ASSERT(::strcmp(szTip, _T("")) != 0); // Sure that icon tip == "" ??
    
    NOTIFYICONDATA* pnidIconInfo = GetNotifyIconDataFromID(nIconID);
    
    if (pnidIconInfo != NULL)
    {
        ::strcpy(pnidIconInfo->szTip, szTip); // Copy the new string containing the tip
    
        pnidIconInfo->cbSize  = sizeof(NOTIFYICONDATA);
        pnidIconInfo->uFlags |= NIF_TIP;

	    bIconTipChanged = ::Shell_NotifyIcon(NIM_MODIFY, pnidIconInfo);
    }
    	    
    return bIconTipChanged;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -