VC6.0下实现系统托盘的气泡提示窗口(Use NIIF_INFO)

作者:Wupei  |  发表时间:  |  所属分类:VC

效果演示如下:
系统托盘气球信息演示

首先需要更新一下VC6.0的SDK(否则会出现编译出错),方法如下:
1.把ShellAPI.h文件中的关于任务栏提示的函数和常量替换为下面的内容(重要):

////
//// Tray notification definitions
////

typedef struct _NOTIFYICONDATAA {
        DWORD cbSize;
        HWND hWnd;
        UINT uID;
        UINT uFlags;
        UINT uCallbackMessage;
        HICON hIcon;
#if (_WIN32_IE < 0x0500)
        CHAR   szTip[64];
#else
        CHAR   szTip[128];
#endif
#if (_WIN32_IE >= 0x0500)
        DWORD dwState;
        DWORD dwStateMask;
        CHAR   szInfo[256];
        union {
            UINT  uTimeout;
            UINT  uVersion;
        } DUMMYUNIONNAME;
        CHAR   szInfoTitle[64];
        DWORD dwInfoFlags;
#endif
#if (_WIN32_IE >= 0x600)
        GUID guidItem;
#endif
} NOTIFYICONDATAA, *PNOTIFYICONDATAA;
typedef struct _NOTIFYICONDATAW {
        DWORD cbSize;
        HWND hWnd;
        UINT uID;
        UINT uFlags;
        UINT uCallbackMessage;
        HICON hIcon;
#if (_WIN32_IE < 0x0500)
        WCHAR  szTip[64];
#else
        WCHAR  szTip[128];
#endif
#if (_WIN32_IE >= 0x0500)
        DWORD dwState;
        DWORD dwStateMask;
        WCHAR  szInfo[256];
        union {
            UINT  uTimeout;
            UINT  uVersion;
        } DUMMYUNIONNAME;
        WCHAR  szInfoTitle[64];
        DWORD dwInfoFlags;
#endif
#if (_WIN32_IE >= 0x600)
        GUID guidItem;
#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
////

2.在stdafx.h文件中添加:

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

3.添加函数(需要先添加系统托盘图标)

//添加成员变量
NOTIFYICONDATA m_nid;

/***************************************
 * pWnd - 窗体指针
 * szMsg - 显示的内部信息
 * szTitle - 显示的标题信息
 * uTimeout - 这个时间过后,气球自动消失(好像不启作用)
 * uCallbackMessage - 消息标识
 ***************************************/
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);
}

4.修改气球提示信息

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);
}

Trackback from your site.

(8)条评论

  • Wupei

    |

    使用的API是不一样的吗?….具体我也不知道

    回复

  • kan

    |

    如题

    回复

  • Wupei

    |

    因为你没有更新,造成的没有定义的标识符的错误
    首先需要更新一下VC6.0的SDK(否则会出现编译出错),方法如下:
    把ShellAPI.h文件中的关于任务栏提示的函数和常量替换为下面的内容(重要)
    看一下文章第一开始的说明

    回复

  • kiwiw3

    |

    为何我按照上述步骤后,VC6还是会报
    D:\C++_job\ScreenShot\CTrayIcon.cpp(148) : error C2065: ‘NIF_INFO’ : undeclared identifier
    D:\C++_job\ScreenShot\CTrayIcon.cpp(168) : error C2039: ‘uTimeout’ : is not a member of ‘_NOTIFYICONDATAW’
    c:\program files\microsoft visual studio\vc98\include\shellapi.h(509) : see declaration of ‘_NOTIFYICONDATAW’
    D:\C++_job\ScreenShot\CTrayIcon.cpp(169) : error C2039: ‘dwInfoFlags’ : is not a member of ‘_NOTIFYICONDATAW’
    c:\program files\microsoft visual studio\vc98\include\shellapi.h(509) : see declaration of ‘_NOTIFYICONDATAW’
    D:\C++_job\ScreenShot\CTrayIcon.cpp(171) : error C2039: ‘szInfo’ : is not a member of ‘_NOTIFYICONDATAW’
    c:\program files\microsoft visual studio\vc98\include\shellapi.h(509) : see declaration of ‘_NOTIFYICONDATAW’
    D:\C++_job\ScreenShot\CTrayIcon.cpp(172) : error C2039: ‘szInfoTitle’ : is not a member of ‘_NOTIFYICONDATAW’

    回复

  • wupei

    |

    不好意思,很抱歉程序的源代码不能给你,这个也不是”开源”的东西,今后我还会将程序改版~
    其实相关的技术都已经写在博客里了
    有什么问题一起讨论的

    回复

  • woody

    |

    谢谢,但我还是不明白
    能把你的那个工程发到我的邮箱吗?
    非常感谢

    回复

  • wupei

    |

    uCallbackMessage是回调消息的标识,用来响应事件的
    一般是用户自己定义的消息
    示例代码:
    //用户定义的消息
    #define MYWM_NOTIFYICON WM_USER+1
    //响应该消息
    LRESULT CMyDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
    // TODO: Add your specialized code here and/or call the base class
    switch(message)
    {
    case MYWM_NOTIFYICON:
    //如果是用户定义的消息
    if(lParam==WM_LBUTTONDOWN)
    {
    //鼠标左键单击图标时
    MessageBox(“鼠标左键单击”);
    }
    else if(lParam==WM_RBUTTONDOWN)
    {
    //鼠标右键单击图标时
    MessageBox(“鼠标右键单击”);
    }
    break;
    }
    return CDialog::WindowProc(message, wParam, lParam);
    }

    回复

  • woody

    |

    我现在正在找这个方面的资料
    问下BOOL CMyCatchFun::ShowBalloonTip(
    CWnd* pWnd,
    LPCTSTR szMsg,
    LPCTSTR szTitle,
    UINT uTimeout,
    UINT uCallbackMessage
    )
    这个函数最后一个参数uCallbackMessage具体是什么意思??
    我的联系方式:QQ 619587378 msn: li.xin2006@yahoo.com.cn
    邮箱同msn

    回复

请在这里留言: