头文件:
#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/