VC6.0下实现系统托盘的气泡提示窗口
Windows中与托盘图标相关的提示有两类:一类是传统的信息提示方式,当光标移到图标上时显示;另一类是新式的信息提示即气球提示,它是由你的程序来控制显示。气球提示有点像连环漫画中的文字气球。
首先需要更新一下VC6.0的SDK(ShellAPI.h文件)
使用下面是这个结构的定义的{zx1}版本(For IE5.0+),其中已经加入了新的成员:
typedef struct _NOTIFYICONDATA {
} NOTIFYICONDATA, *PNOTIFYICONDATA;
1.把ShellAPI.h文件中的关于任务栏提示的函数和常量替换为下面的内容(重要):
屏蔽掉以前的源文件,如下
////
//// Tray notification definitions
////
////
//// End Tray Notification Icons
////
2.在次添加如下的新文件
//start
////
//// Tray notification definitions
////
typedef struct _NOTIFYICONDATAA {
#if (_WIN32_IE < 0x0500)
#else
#endif
#if (_WIN32_IE >= 0x0500)
#endif
#if (_WIN32_IE >= 0x600)
#endif
} NOTIFYICONDATAA, *PNOTIFYICONDATAA;
typedef struct _NOTIFYICONDATAW {
#if (_WIN32_IE < 0x0500)
#else
#endif
#if (_WIN32_IE >= 0x0500)
#endif
#if (_WIN32_IE >= 0x600)
#endif
} NOTIFYICONDATAW, *PNOTIFYICONDATAW;
#ifdef UNICODE
typedef NOTIFYICONDATAW NOTIFYICONDATA;
typedef PNOTIFYICONDATAW PNOTIFYICONDATA;
#else
typedef NOTIFYICONDATAA NOTIFYICONDATA;
typedef PNOTIFYICONDATAA PNOTIFYICONDATA;
#endif // UNICODE
#define NOTIFYICONDATAA_V1_SIZE FIELD_OFFSET(NOTIFYICONDATAA, szTip[64])
#define NOTIFYICONDATAW_V1_SIZE FIELD_OFFSET(NOTIFYICONDATAW, szTip[64])
#ifdef UNICODE
#define NOTIFYICONDATA_V1_SIZE NOTIFYICONDATAW_V1_SIZE
#else
#define NOTIFYICONDATA_V1_SIZE NOTIFYICONDATAA_V1_SIZE
#endif
#define NOTIFYICONDATAA_V2_SIZE FIELD_OFFSET(NOTIFYICONDATAA, guidItem)
#define NOTIFYICONDATAW_V2_SIZE FIELD_OFFSET(NOTIFYICONDATAW, guidItem)
#ifdef UNICODE
#define NOTIFYICONDATA_V2_SIZE NOTIFYICONDATAW_V2_SIZE
#else
#define NOTIFYICONDATA_V2_SIZE NOTIFYICONDATAA_V2_SIZE
#endif
#if (_WIN32_IE >= 0x0500)
#define NIN_SELECT (WM_USER + 0)
#define NINF_KEY 0x1
#define NIN_KEYSELECT (NIN_SELECT | NINF_KEY)
#endif
#if (_WIN32_IE >= 0x0501)
#define NIN_BALLOONSHOW (WM_USER + 2)
#define NIN_BALLOONHIDE (WM_USER + 3)
#define NIN_BALLOONTIMEOUT (WM_USER + 4)
#define NIN_BALLOONUSERCLICK (WM_USER + 5)
#endif
#define NIM_ADD 0x00000000
#define NIM_MODIFY 0x00000001
#define NIM_DELETE 0x00000002
#if (_WIN32_IE >= 0x0500)
#define NIM_SETFOCUS 0x00000003
#define NIM_SETVERSION 0x00000004
#define NOTIFYICON_VERSION 3
#endif
#define NIF_MESSAGE 0x00000001
#define NIF_ICON 0x00000002
#define NIF_TIP 0x00000004
#if (_WIN32_IE >= 0x0500)
#define NIF_STATE 0x00000008
#define NIF_INFO 0x00000010
#endif
#if (_WIN32_IE >= 0x600)
#define NIF_GUID 0x00000020
#endif
#if (_WIN32_IE >= 0x0500)
#define NIS_HIDDEN 0x00000001
#define NIS_SHAREDICON 0x00000002
// says this is the source of a shared icon
// Notify Icon Infotip flags
#define NIIF_NONE 0x00000000
// icon flags are mutually exclusive
// and take only the lowest 2 bits
#define NIIF_INFO 0x00000001
#define NIIF_WARNING 0x00000002
#define NIIF_ERROR 0x00000003
#define NIIF_ICON_MASK 0x0000000F
#if (_WIN32_IE >= 0x0501)
#define NIIF_NOSOUND 0x00000010
#endif
#endif
SHSTDAPI_(BOOL) Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA lpData);
SHSTDAPI_(BOOL) Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW lpData);
#ifdef UNICODE
#define Shell_NotifyIcon Shell_NotifyIconW
#else
#define Shell_NotifyIcon Shell_NotifyIconA
#endif // !UNICODE
////
//// End Tray Notification Icons
////
///end
3.在stdafx.h文件中添加:
#ifndef _WIN32_IE // 允许使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0x0500 //为 IE 5.0 及更新版本改变为适当的值。
#endif
4.添加函数(需要先添加系统托盘图标)
//添加成员变量
NOTIFYICONDATA m_nid;
BOOL CMyCatchFun::ShowBalloonTip(
CWnd* pWnd,
LPCTSTR szMsg,
LPCTSTR szTitle,
UINT uTimeout,
UINT uCallbackMessage
)
{
m_nid.cbSize = sizeof(NOTIFYICONDATA);
m_nid.uTimeout = uTimeout;
m_nid.uVersion = NOTIFYICON_VERSION;
m_nid.dwInfoFlags = NIIF_INFO; //如果前面修改错误,这里会发生"未定义标识符"错误
m_nid.uFlags = NIF_MESSAGE|NIF_INFO|NIF_ICON;
m_nid.uID = IDR_MAINFRAME;
m_nid.hWnd = pWnd->m_hWnd;
m_nid.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nid.uCallbackMessage = uCallbackMessage;
_tcscpy(m_nid.szInfoTitle,szTitle);
_tcscpy(m_nid.szInfo,szMsg);
return Shell_NotifyIcon(NIM_MODIFY,&m_nid);
}
5.修改气球提示信息
void CMyDlg::OnButton1()
{
_tcscpy(m_nid.szInfoTitle,"提醒你");
_tcscpy(m_nid.szInfo,"内容改变");
m_nid.uTimeout=1000;
m_nid.uVersion=NOTIFYICON_VERSION;
Shell_NotifyIcon(NIM_MODIFY,&m_nid);
}
在NOTIFYICONDATA.uFlags中的标志之一是NIF_TIP,用它来设置传统的信息提示,即鼠标要移动到图标上。新的标志NIF_INFO(由于_WIN32_IE >= 0x0500条件定义,因此在编译时,请注意包含{zx1}版本的头文件shellapi.h,并保证链接{zx1}版本的库文件shell32.lib,分发程序时用{zx1}版本的运行时动态链接库shell32.dll)便是为显示气球提示所用的。也就是说,要显示气球提示,那么在调用Shell_NotifyIcon函数时必须用NIF_INFO标志。提示文本填入szInfo域,标题文本填入szInfoTitle。你甚至可以在NOTIFYICONDATA.uTimeout中设置一个超时时间,当经过指定的毫秒数之后,气球提示自动隐藏。
BOOL CTrayIcon::ShowBalloonTip(LPCTSTR szMsg, LPCTSTR szTitle, UINT uTimeout, DWORD dwInfoFlags) { m_nid.cbSize=sizeof(NOTIFYICONDATA); m_nid.uFlags = NIF_INFO; m_nid.uTimeout = uTimeout; m_nid.dwInfoFlags = dwInfoFlags; strcpy(m_nid.szInfo,szMsg ? szMsg : _T("")); strcpy(m_nid.szInfoTitle,szTitle ? szTitle : _T("")); return Shell_NotifyIcon(NIM_MODIFY, &m_nid); }
通知消息 |
|
NIN_BALLOONSHOW |
显示气球提示时发送 |
NIN_BALLOONHIDE | 气球提示消失时发送;例如,当图标被删除,如果因为超时或是用户单击鼠标气球消失,此消息不会被发送 |
NIN_BALLOONTIMEOUT | 当由于超时或者用户单击气球上的关闭按钮(X),使气球消失时发送此消息 |
NIN_BALLOONUSERCLICK | 当用户在气球提示上或托盘图标上单击鼠标(此时气球处于显示状态)时发送此消息 |