我看到通知气球,但没有显示图标。并且在成功(返回TRUE)之后调用Shell_NotifyIcon会出现Windows错误:尝试引用不存在的令牌。主要问题是为什么我没有看到图标? Windows 7与最新更新一起使用。
以下是代码段:
namespace tray_tip
{
#define FINAL_INIT( tip, title, ic, icon_id, inst )\
if ( icon )\
{\
static const GUID myGUID = \
{0x23977b55, 0x10e0, 0x4041, {0xb8, 0x62, 0xb1, 0x95, 0x41, 0x96, 0x36, 0x69}};\
data.guidItem = myGUID;\
data.dwInfoFlags |= NIF_ICON;\
ExtractIconEx( _T("task_dialog_test.ico"), 0, NULL, &(data.hIcon), 1 );\
}\
data.dwInfoFlags |= ic;\
_tcscpy_s( data.szInfo, tip );\
_tcscpy_s( data.szInfoTitle, title );
static NOTIFYICONDATA data = {0};
inline void init( HWND const & parent, UINT const & msg_id, UINT const & time_out = 30 )
{
ULONGLONG ullVersion = GetDllVersion( _T( "Shell32.dll" ) );
if ( ullVersion > MAKEDLLVERULL ( 6,0,0,0 ) )
data.cbSize = sizeof ( NOTIFYICONDATA );
else if ( ullVersion == MAKEDLLVERULL( 6,0,0,0 ) )
data.cbSize = sizeof( NOTIFYICONDATA_V3_SIZE );
else if ( ullVersion >= MAKEDLLVERULL( 5,0,0,0 ) )
data.cbSize = NOTIFYICONDATA_V2_SIZE;
else
data.cbSize = NOTIFYICONDATA_V1_SIZE;
data.hWnd = parent;
data.uFlags = NIF_INFO | NIF_MESSAGE;// | NIF_SHOWTIP;
data.uCallbackMessage = msg_id;
data.uTimeout = time_out;
}
inline BOOL show( BOOL show = FALSE, TCHAR tip[256] = _T(""), TCHAR title[64] = _T(""), DWORD const & ic = NIIF_INFO, UINT const & icon = 0, HINSTANCE const & inst_h = NULL )
{
FINAL_INIT( tip, title, ic, icon, inst_h );
return Shell_NotifyIcon( show == TRUE ? NIM_ADD : NIM_DELETE, &data ); // function checks if version can be changed if it was preset.
}
}
答案 0 :(得分:0)
O.K。,这是我对解决方案的建议。对于Windows 7,有一种识别图标的特定方法。 可以通过两种方式识别通知区域中的每个图标。 用于在注册表中声明图标的GUID。这是Windows 7及更高版本的首选方法。 与通知区域图标相关联的窗口的句柄,以及应用程序定义的图标标识符。此方法用于Windows Vista及更早版本。 因此,在指定data.uID时,Windows 7不应从父句柄和uID字段值生成唯一ID,而应从指定的GUID(全局唯一标识符)生成。因此,当使用uID向通知区域图标发送任何消息时,ShellNotify_Icon将返回零(错误ID)。但是,在创建GUID时,例如使用随VS2010工具提供的工具GuidGen,它需要机器网卡地址和ID,机器配置,当前日期和时间,随机组件,Windows 7将有一个唯一的ID来处理。 有关MSDN的更多信息,请参阅我对Hans Passant消息的评论中提到的链接。
以下是一段代码片段:
namespace tray_tip
{
#define FINAL_INIT( tip, title, ic, icon_id, inst_h )\
data.uFlags |= NIF_ICON;\
LoadIconMetric(inst_h, MAKEINTRESOURCE(icon_id), LIM_SMALL, &(data.hIcon));\
static const GUID ic_guid = { 0xa351efe, 0x9642, 0x4acc, { 0xb3, 0x25, 0xec, 0x1a, 0x49, 0x91, 0x75, 0xaf } };\
data.uFlags |= NIF_GUID;\
data.guidItem = ic_guid;\
data.dwStateMask = NIS_HIDDEN;\
data.dwInfoFlags |= ic;\
_tcscpy_s( data.szInfo, tip );\
_tcscpy_s( data.szInfoTitle, title );
//#define FINAL_INIT( tip, title, ic, icon_id, inst_h )\
// data.uFlags |= NIF_ICON;\
// LoadIconMetric(inst_h, MAKEINTRESOURCE(icon_id), LIM_SMALL, &(data.hIcon));\
// data.uID = icon_id,\
// data.dwStateMask = NIS_HIDDEN;\
// data.dwInfoFlags |= ic;\
// _tcscpy_s( data.szInfo, tip );\
// _tcscpy_s( data.szInfoTitle, title );
static NOTIFYICONDATA data = {0};
inline void init( HWND const & parent, UINT const & msg_id, UINT const & time_out = 30 )
{
ULONGLONG ullVersion = GetDllVersion( _T( "Shell32.dll" ) );
if ( ullVersion > MAKEDLLVERULL ( 6,0,0,0 ) )
data.cbSize = sizeof ( NOTIFYICONDATA );
else if ( ullVersion == MAKEDLLVERULL( 6,0,0,0 ) )
data.cbSize = sizeof( NOTIFYICONDATA_V3_SIZE );
else if ( ullVersion >= MAKEDLLVERULL( 5,0,0,0 ) )
data.cbSize = NOTIFYICONDATA_V2_SIZE;
else
data.cbSize = NOTIFYICONDATA_V1_SIZE;
data.hWnd = parent;
data.uFlags = NIF_INFO | NIF_MESSAGE;
data.uCallbackMessage = msg_id;
data.uTimeout = time_out;
}
inline BOOL show( BOOL show = FALSE, TCHAR tip[256] = _T(""), TCHAR title[64] = _T(""), DWORD const & ic = NIIF_INFO, UINT const & icon_id = 0, HINSTANCE const & inst_h = NULL )
{
FINAL_INIT( tip, title, ic, icon_id, inst_h );
data.dwState = show ? 0 : 1;
static bool added = false;
if ( added || ( !added && show ) )
{
added = true;
return Shell_NotifyIcon( show == TRUE ? NIM_ADD : NIM_DELETE, &data ); // function checks if version can be changed if it was preset.
}
else
return 0;
}
} // namespace tray_tip