VC实现托盘气泡提示

如果在VC6.0环境下,要将ShellAPI.h文件中的关于任务栏提示的函数和常量替换为下面的内容:

/// /
/// /Traynotificationdefinitions
/// /

typedef
struct _NOTIFYICONDATAA{
DWORDcbSize;
HWNDhWnd;
UINTuID;
UINTuFlags;
UINTuCallbackMessage;
HICONhIcon;
#if (_WIN32_IE<0x0500)
CHARszTip[
64 ];
#else
CHARszTip[
128 ];
#endif
#if (_WIN32_IE>=0x0500)
DWORDdwState;
DWORDdwStateMask;
CHARszInfo[
256 ];
union{
UINTuTimeout;
UINTuVersion;
}DUMMYUNIONNAME;
CHARszInfoTitle[
64 ];
DWORDdwInfoFlags;
#endif
#if (_WIN32_IE>=0x600)
GUIDguidItem;
#endif
}NOTIFYICONDATAA,
* PNOTIFYICONDATAA;
typedef
struct _NOTIFYICONDATAW{
DWORDcbSize;
HWNDhWnd;
UINTuID;
UINTuFlags;
UINTuCallbackMessage;
HICONhIcon;
#if (_WIN32_IE<0x0500)
WCHARszTip[
64 ];
#else
WCHARszTip[
128 ];
#endif
#if (_WIN32_IE>=0x0500)
DWORDdwState;
DWORDdwStateMask;
WCHARszInfo[
256 ];
union{
UINTuTimeout;
UINTuVersion;
}DUMMYUNIONNAME;
WCHARszInfoTitle[
64 ];
DWORDdwInfoFlags;
#endif
#if (_WIN32_IE>=0x600)
GUIDguidItem;
#endif
}NOTIFYICONDATAW,
* PNOTIFYICONDATAW;
#ifdefUNICODE
typedefNOTIFYICONDATAWNOTIFYICONDATA;
typedefPNOTIFYICONDATAWPNOTIFYICONDATA;
#else
typedefNOTIFYICONDATAANOTIFYICONDATA;
typedefPNOTIFYICONDATAAPNOTIFYICONDATA;
#endif // UNICODE


#define NOTIFYICONDATAA_V1_SIZEFIELD_OFFSET(NOTIFYICONDATAA,szTip[64])
#define NOTIFYICONDATAW_V1_SIZEFIELD_OFFSET(NOTIFYICONDATAW,szTip[64])
#ifdefUNICODE
#define NOTIFYICONDATA_V1_SIZENOTIFYICONDATAW_V1_SIZE
#else
#define NOTIFYICONDATA_V1_SIZENOTIFYICONDATAA_V1_SIZE
#endif

#define NOTIFYICONDATAA_V2_SIZEFIELD_OFFSET(NOTIFYICONDATAA,guidItem)
#define NOTIFYICONDATAW_V2_SIZEFIELD_OFFSET(NOTIFYICONDATAW,guidItem)
#ifdefUNICODE
#define NOTIFYICONDATA_V2_SIZENOTIFYICONDATAW_V2_SIZE
#else
#define NOTIFYICONDATA_V2_SIZENOTIFYICONDATAA_V2_SIZE
#endif


#if (_WIN32_IE>=0x0500)
#define NIN_SELECT(WM_USER+0)
#define NINF_KEY0x1
#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_ADD0x00000000
#define NIM_MODIFY0x00000001
#define NIM_DELETE0x00000002
#if (_WIN32_IE>=0x0500)
#define NIM_SETFOCUS0x00000003
#define NIM_SETVERSION0x00000004
#define NOTIFYICON_VERSION3
#endif

#define NIF_MESSAGE0x00000001
#define NIF_ICON0x00000002
#define NIF_TIP0x00000004
#if (_WIN32_IE>=0x0500)
#define NIF_STATE0x00000008
#define NIF_INFO0x00000010
#endif
#if (_WIN32_IE>=0x600)
#define NIF_GUID0x00000020
#endif

#if (_WIN32_IE>=0x0500)
#define NIS_HIDDEN0x00000001
#define NIS_SHAREDICON0x00000002

// saysthisisthesourceofasharedicon

// NotifyIconInfotipflags
#define NIIF_NONE0x00000000
// iconflagsaremutuallyexclusive
// andtakeonlythelowest2bits
#define NIIF_INFO0x00000001
#define NIIF_WARNING0x00000002
#define NIIF_ERROR0x00000003
#define NIIF_ICON_MASK0x0000000F
#if (_WIN32_IE>=0x0501)
#define NIIF_NOSOUND0x00000010
#endif
#endif

SHSTDAPI_(BOOL)Shell_NotifyIconA(DWORDdwMessage,PNOTIFYICONDATAAlpData);
SHSTDAPI_(BOOL)Shell_NotifyIconW(DWORDdwMessage,PNOTIFYICONDATAWlpData);
#ifdefUNICODE
#define Shell_NotifyIconShell_NotifyIconW
#else
#define Shell_NotifyIconShell_NotifyIconA
#endif // !UNICODE

/// /
/// /EndTrayNotificationIcons
/// /

然后在stdafx.h文件中添加以下内容:

#ifndef _WIN32_IE // 允许使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0x0500 // 为 IE 5.0 及更新版本改变为适当的值。
#endif

自定义消息及处理函数:

#define WM_ICONNOTIFY WM_USER + 909 //图标响应消息(lParam)
#define ID_TASKBARICON 100 //参数(wParam)
ON_MESSAGE(WM_ICONNOTIFY, OnNotifyIcon)

添加变量: NOTIFYICONDATA m_nid;

在触发事件函数中添加代码:

SetIcon();
ShowBalloonTip("正在监控中...", "进程监控程序", 3000, 1);

初始化系统托盘图标:

void CDlg::SetIcon()
{
m_nid.cbSize
= sizeof (NOTIFYICONDATA);
m_nid.hWnd
= m_hWnd;
m_nid.uID
= ID_TASKBARICON; // 发出的消息中的wParam参数
m_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
m_nid.uCallbackMessage
= WM_ICONNOTIFY; // 点击托盘图标系统发出的消息(即发出的消息中的lParam参数)
m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)); // 图标文件
strcpy(m_nid.szTip, " 进程监控程序 " );
Shell_NotifyIcon(NIM_ADD,
& m_nid); // 加入图标
ShowWindow(SW_HIDE); // 隐藏窗体
}

托盘气泡提示函数:

BOOLCDlg::ShowBalloonTip(LPCTSTRszMsg,LPCTSTRszTitle,UINTuTimeout,DWORDdwInfoFlags)
{
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);
}

双击系统托盘,使程序正常化:

LRESULTCDlg::OnNotifyIcon(WPARAMwParam,LPARAMlParam)
{
POINTrpoint;
CMenutmenu;
GetCursorPos(
& rpoint);
// 如果在托盘图标上双击左键
if ((wParam == ID_TASKBARICON) && (lParam == WM_LBUTTONDBLCLK))
{
ShowWindow(SW_SHOW);
// 显示窗体
Shell_NotifyIcon(NIM_DELETE, & m_nid); // 删除系统托盘图标
}
// 如果在托盘图标上单击右键
if ((wParam == ID_TASKBARICON) && (lParam == WM_RBUTTONDOWN))
{
tmenu.LoadMenu(IDR_MAINMENU);
// 加载系统主菜单
SetForegroundWindow(); // 将程序置于桌面顶部,否则菜单将不会消失
// 在点击图标处弹出系统菜单的第一项
tmenu.GetSubMenu( 0 ) -> TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,rpoint.x,rpoint.y, this );
}
return 0 ;
}

你可能感兴趣的:(IE,vc++)