[MFC]系统托盘操作类,图标,气泡等

先封装一个类,目前还没有图标动画。

头文件:

#ifndef __K_BUBBLE_TIP_H__

#define __K_BUBBLE_TIP_H__

 

class KBubbleTip

{

 

public:

     KBubbleTip();

 

     void init(HWND hWnd, UINT nIDResource, UINT nIDMessage);

     void init(HWND hWnd, UINT nIDResource, UINT nIDMessage, LPCSTR strTip);

     void init(HWND hWnd, UINT nIDResource, UINT nIDMessage, LPCSTR strTip, LPCSTR strBubbleTitle, LPCSTR strBubbleContent);

 

     ~KBubbleTip();

    

     void SetTip(LPCSTR strTip);      // 更改TIP提示

 

     void SetBubble(LPCSTR strBubbleTitle, LPCSTR strBubbleContent);  

                                               // 显示一个Bubble提示,如果没有设置时间,按照前一个时间或默认时间

 

     void SetBubbleTimeout(UINT uTimeout);

     //void SetBubble();    // 显示一个气泡

     //void ModifyIcon();   // 更换成一个新的ICON

    

 

private:

     NOTIFYICONDATA         m_NotifyIconData;

 

     BOOL               m_bIsInited;

 

     BOOL               m_bIsWinXP;                

     BOOL               m_bIsWin2K;

     BOOL               m_bIsVista;

     BOOL               m_bIsWin7;

     BOOL               m_bIsWin9X;

 

     UINT               m_MainVersionNumber;

     UINT               m_MiniVersionNumber;

 

     UINT               m_uDefaultTimeOut;

 

     void               GetSysInfo(); // 取得系统信息并部分初始化m_NotifyIconData,方便多个构造函数调用

};

 

#endif

 

实现文件:

#include "stdafx.h"

#include "KBubbleTip.h"

// Constructor

KBubbleTip::KBubbleTip()

{

     // Default value

     m_uDefaultTimeOut  = 2000;

     m_bIsInited            = FALSE;

}

// Initialize 

void KBubbleTip::init(

                            HWND hWnd,

                            UINT nIDResource,

                            UINT nIDMessage

                            )

{   

     this->GetSysInfo();

 

     m_NotifyIconData.hWnd       = hWnd;

     m_NotifyIconData.hIcon      = AfxGetApp()->LoadIcon(nIDResource);

 

     m_NotifyIconData.uFlags     = NIF_MESSAGE | NIF_ICON | NIF_TIP |NIF_INFO;

     m_NotifyIconData.uID        = nIDResource;

     m_NotifyIconData.dwInfoFlags= NIIF_INFO;

     m_NotifyIconData.uTimeout   = m_uDefaultTimeOut;

     m_NotifyIconData.uCallbackMessage    = nIDMessage;

     Shell_NotifyIcon(NIM_ADD,&m_NotifyIconData);

     m_bIsInited            = TRUE;

}

 

void KBubbleTip::init(

                            HWND hWnd,

                            UINT nIDResource,

                            UINT nIDMessage,

                            LPCSTR strTip

                            )

{   

     this->GetSysInfo();

 

     m_NotifyIconData.hWnd       = hWnd;

     m_NotifyIconData.hIcon      = AfxGetApp()->LoadIcon(nIDResource);

 

     m_NotifyIconData.uFlags     = NIF_MESSAGE | NIF_ICON | NIF_TIP |NIF_INFO;

     m_NotifyIconData.uID        = nIDResource;

     m_NotifyIconData.dwInfoFlags= NIIF_INFO;

     m_NotifyIconData.uTimeout   = m_uDefaultTimeOut;

 

     m_NotifyIconData.uCallbackMessage    = nIDMessage;

 

     strcpy_s(m_NotifyIconData.szTip, strTip);

 

     Shell_NotifyIcon(NIM_ADD,&m_NotifyIconData);

 

     m_bIsInited            = TRUE;

 

}

void KBubbleTip::init(

                            HWND hWnd,

                            UINT nIDResource,

                            UINT nIDMessage,

                            LPCSTR   strTip,

                            LPCSTR   strBubbleTitle,

                            LPCSTR   strBubbleContent

                            )

{   

     this->GetSysInfo();

 

     m_NotifyIconData.hWnd       = hWnd;

     m_NotifyIconData.hIcon      = AfxGetApp()->LoadIcon(nIDResource);

 

     m_NotifyIconData.uFlags     = NIF_MESSAGE | NIF_ICON | NIF_TIP |NIF_INFO;

     m_NotifyIconData.uID        = nIDResource;

     m_NotifyIconData.dwInfoFlags= NIIF_INFO;

     m_NotifyIconData.uTimeout   = m_uDefaultTimeOut;

 

     strcpy_s(m_NotifyIconData.szTip, strTip);

     strcpy_s(m_NotifyIconData.szInfoTitle, strBubbleTitle);

     strcpy_s(m_NotifyIconData.szInfo, strBubbleContent);

 

     m_NotifyIconData.uCallbackMessage    = nIDMessage;

 

     Shell_NotifyIcon(NIM_ADD,&m_NotifyIconData);

 

     m_bIsInited            = TRUE;

}

// Initialize 

void KBubbleTip::GetSysInfo()

{

    

     // Get operating system version

     OSVERSIONINFO osver;

 

    m_MainVersionNumber = 0;

     m_MiniVersionNumber = 0;

 

     m_bIsWinXP    = FALSE;              

     m_bIsWin2K    = FALSE;

     m_bIsVista    = FALSE;

     m_bIsWin7 = FALSE;

     m_bIsWin9X    = FALSE;

 

     osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO);

    

     if (::GetVersionEx( &osver ) )

     {

         m_MainVersionNumber = osver.dwMajorVersion;

         m_MiniVersionNumber = osver.dwMinorVersion;

 

         if(osver.dwPlatformId == VER_PLATFORM_WIN32_NT)

         {

              if(m_MainVersionNumber == 5)

              {

                   if(m_MiniVersionNumber == 1)

                       m_bIsWinXP = TRUE;

                   else

                       m_bIsWin2K = TRUE;

              }

              else if(m_MainVersionNumber == 6)

              {

                   if(m_MiniVersionNumber == 1)

                       m_bIsWin7 = TRUE;

                   else

                       m_bIsVista = TRUE;

              }

              else if(m_MainVersionNumber > 6)

              {

                   m_bIsWin7 = TRUE;

              }

         }

         else

              m_bIsWin9X = TRUE;

     }

     else

     {

         m_MainVersionNumber = 0;

         m_MiniVersionNumber = 0;

     }   

 

     ZeroMemory( &m_NotifyIconData, sizeof(m_NotifyIconData));

 

     if(m_bIsVista || m_bIsWin7)

     { 

         m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);

     }

     else if(m_bIsWinXP)

     { 

         m_NotifyIconData.cbSize = NOTIFYICONDATA_V3_SIZE;

     }

     else if(m_bIsWin2K)

     { 

         m_NotifyIconData.cbSize = NOTIFYICONDATA_V2_SIZE;

     }

     else

     { 

         m_NotifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE;

     }

}// GetSysInfo()

void KBubbleTip::SetTip(LPCSTR strTip)

{

     if (!m_bIsInited)

     {

         return;

     }

 

     strcpy_s(m_NotifyIconData.szTip, strTip);

 

     Shell_NotifyIcon(NIM_MODIFY,&m_NotifyIconData);

}// SetTip()

void KBubbleTip::SetBubble(LPCSTR strBubbleTitle, LPCSTR strBubbleContent)

{

     if (!m_bIsInited)

     {

         return;

     }

 

     strcpy_s(m_NotifyIconData.szInfoTitle, strBubbleTitle);

     strcpy_s(m_NotifyIconData.szInfo, strBubbleContent);

 

     Shell_NotifyIcon(NIM_MODIFY,&m_NotifyIconData);

}// SetBubble()

 

void KBubbleTip::SetBubbleTimeout(UINT uTimeout)

{

     this->m_uDefaultTimeOut = uTimeout;

}// SetBubbleTimeout()

// Destructor

KBubbleTip::~KBubbleTip()

{

     Shell_NotifyIcon(NIM_DELETE,&m_NotifyIconData);

}


转自:http://knix.blog.163.com/blog/static/2957503520101251584777/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值