NOTIFYICONDATA图标通知c ++

时间:2017-12-04 18:11:34

标签: c++ winapi notifications system-tray

我有一个作为服务运行的应用程序,以及一个监视服务的辅助应用程序。监视应用程序使用NOTIFYICONDATA存在于系统托盘中,工作正常。

我目前正在尝试做的是,当应用程序注意到服务已停止时,我想显示通知(类似于笔记本电脑上的电池电量不足)。我的代码基于this article。我必须这样做的功能如下:

void CALLBACK checkit( HWND hwnd, UINT umsg, UINT timerid, DWORD dwtime ) {
    if ( isServiceRunning() ) {
        if ( nidApp.dwInfoFlags != NIIF_NONE ) {
            Log( "dwInfoFlags != NIFF_NONE" );
            nidApp.dwInfoFlags = NIIF_NONE;
            strcpy_s( nidApp.szInfoTitle, sizeof( nidApp.szInfoTitle ), "" );
            strcpy_s( nidApp.szInfo, sizeof( nidApp.szInfoTitle ), "" );
            Log( "%d", Shell_NotifyIcon( NIM_MODIFY, &nidApp ) );
        }
    } else {
        if ( nidApp.dwInfoFlags != NIIF_WARNING ) {
            Log( "dwInfoFlags != NIIF_WARNING" );
            nidApp.dwInfoFlags = NIIF_WARNING;
            strcpy_s( nidApp.szInfoTitle, sizeof( nidApp.szInfoTitle ), "Service Stopped" );
            strcpy_s( nidApp.szInfo, sizeof( nidApp.szInfo ), "The " PROGRAM_NAME " service has been stopped. Any runs in progress have been terminated." );
            nidApp.uTimeout = 10000;
            Log( "%d", Shell_NotifyIcon( NIM_MODIFY, &nidApp ) );
        }
    }
}

此功能每五秒调用一次。使用日志,我可以看到dwInfoFlags设置正确,Shell_NotifyIcon返回TRUE,但是没有显示通知。我敢肯定我一定会遗漏一些东西,但我无法弄清楚它是什么。

nidApp在CPP文件的顶部定义为NOTIFYICONDATA nidApp;,设置如下:

hMainIcon = LoadIcon( hInstance, (LPCTSTR)MAKEINTRESOURCE( IDI_ICON1 ) );
nidApp.cbSize = sizeof( NOTIFYICONDATA ); // sizeof the struct in bytes
nidApp.hWnd = (HWND)hWnd;            //handle of the window which will process this app. messages
nidApp.uID = IDI_ICON1;       //ID of the icon that willl appear in the system tray
nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_GUID | NIF_SHOWTIP; //ORing of all the flags
nidApp.hIcon = hMainIcon; // handle of the Icon to be displayed, obtained from LoadIcon
nidApp.uCallbackMessage = WM_USER_SHELLICON;
nidApp.uVersion = NOTIFYICON_VERSION_4;
nidApp.guidItem = myGUID;
strcpy_s( nidApp.szTip, sizeof( nidApp.szTip ), PROGRAM_NAME " Service Controller" );
Shell_NotifyIcon( NIM_ADD, &nidApp );
Shell_NotifyIcon( NIM_SETVERSION, &nidApp );

1 个答案:

答案 0 :(得分:3)

您应该将nidApp.uFlags设置为NIF_INFO以显示通知。现在,您使用与创建通知图标相同的标记来调用Shell_NotifyIcon