在系统托盘中显示气球工具提示

时间:2009-09-12 11:59:15

标签: visual-c++ winapi

这是我的代码..

设置trayicon .......

NOTIFYICONDATA data;//this is global variable.
case WM_CREATE :
data.cbSize = sizeof(NOTIFYICONDATA);
data.hWnd =hWnd;
data.uID = IDR_MAINFRAME;
data.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
data.uCallbackMessage = ID_TRAYICON_NOTIFY;
data.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_INFO));

wcscpy_s(data.szTip,128,a);

Shell_NotifyIcon( NIM_ADD, &data );

........

并设置气球 ......

case WM_RBUTTONDBLCLK:
data.hWnd = hWnd;
data.cbSize =sizeof(NOTIFYICONDATA);
data.hIcon =  LoadIcon(hInst, MAKEINTRESOURCE(IDI_INFO));
data.uTimeout = 5000;
data.uFlags = NIF_INFO;
data.dwInfoFlags = NIIF_INFO;
_tcscpy_s(data.szInfoTitle,_T("TITLE"));
_tcscpy_s(data.szInfo,_T("SOME TEXT"));
Shell_NotifyIcon(NIM_MODIFY,&data);

.......

但是,无法显示气球工具提示

plz教我。

2 个答案:

答案 0 :(得分:3)

您应该检查shell32.dll版本,并将cbSize设置为(如备注部分of the NOTIFYICONDATA docs中所述):

  • sizeof(NOTIFYICONDATA)如果版本为> = 6.0.6
  • NOTIFYICONDATA_V3_SIZE如果版本为6.0(WinXP)
  • NOTIFYICONDATA_V2_SIZE如果版本为5.0(Win2000)
  • NOTIFYICONDATA_V1_SIZE如果版本为< 5.0(NT4 / 95/98)

答案 1 :(得分:0)

我发现用于检测Windows版本的官方文档中的代码并不是很正确。 这应该更好:

尝试这个尺寸(字面意思):

BOOL CheckWindowsVersion(DWORD dwMajor, DWORD dwMinor, DWORD dwBuild)
{
    // Initialize the OSVERSIONINFOEX structure.
    OSVERSIONINFOEX osvi;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    osvi.dwMajorVersion = dwMajor;
    osvi.dwMinorVersion = dwMinor;
    osvi.dwBuildNumber = dwBuild;

    // Initialize the condition mask.
    DWORDLONG dwlConditionMask = 0;
    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
    VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
    VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);


    // Perform the test.
    return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, dwlConditionMask);
}

然后,当您想要设置图标时,您可以测试正确的Windows版本。出于某些原因,在官方文档中他们只检查6.1,当它应该是6.0.6 +所有其他Windows版本...

if( CheckWindowsVersion(6, 0, 6))
    data.cbSize = sizeof(NOTIFYICONDATA);
else if( CheckWindowsVersion(6, 0, 0))
    data.cbSize = NOTIFYICONDATA_V3_SIZE;
else if( CheckWindowsVersion(5, 0, 0))
    data.cbSize = NOTIFYICONDATA_V2_SIZE;
else
    data.cbSize = NOTIFYICONDATA_V1_SIZE;

我还没有对VER_BUILDNUMBER部分进行全面测试,但我认为这必须是关闭的。