gpt4 book ai didi

c++ - 清除通知托盘图标,windows shell

转载 作者:可可西里 更新时间:2023-11-01 10:34:08 40 4
gpt4 key购买 nike

问题:通知图标在哪里注册,如果已经从其他位置注册,如何删除?

描述:我想使用通知托盘图标,所以我启动了一个 Windows SDK 示例来检查它是如何工作的。

问题是,一旦从一个位置注册了图标,就不能在其他任何地方使用。 Shell_NotifyIcon(...) 从不同的位置失败。它甚至在示例的自述文件中进行了描述:

Please note that notification icons specified with a GUID are protected against spoofing by validating that only a single application registers them. This registration is performed the first time you call Shell_NotifyIcon(NIM_ADD, ...), and the full pathname of the calling application is stored. If you later move your binary to a different location, the system will not allow the icon to be added again.

我尝试在我的应用程序退出时使用 Shell_NotifyIcon(NIM_DELETE, ...),但它仍然不起作用,我的应用程序可以从不同的位置启动。 Ofc。我可以通过每次 Shell_NotifyIcon(...) 失败时更改 guid 来破解它,但我更愿意使用较少的蛮力方法。此外,我在系统注册表中搜索了 guid,找不到。

示例中的 guid 定义如下:

// Use a guid to uniquely identify our icon
class __declspec(uuid("9D0B8B92-4E1C-488e-A1E1-2331AFCE2CB5")) PrinterIcon;

创建通知图标的代码:

BOOL AddNotificationIcon(HWND hwnd)
{
NOTIFYICONDATA nid = {sizeof(nid)};
nid.hWnd = hwnd;
// add the icon, setting the icon, tooltip, and callback message.
// the icon will be identified with the GUID
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP | NIF_GUID;
nid.guidItem = __uuidof(PrinterIcon);
nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;
LoadIconMetric(g_hInst, MAKEINTRESOURCE(IDI_NOTIFICATIONICON), LIM_SMALL, &nid.hIcon);
LoadString(g_hInst, IDS_TOOLTIP, nid.szTip, ARRAYSIZE(nid.szTip));
Shell_NotifyIcon( NIM_ADD, &nid );

// NOTIFYICON_VERSION_4 is prefered
nid.uVersion = NOTIFYICON_VERSION_4;
return Shell_NotifyIcon(NIM_SETVERSION, &nid);
}

该代码是Windows SDK示例的一部分:SDKs/Windows/v7.1/Samples/winui/shell/appshellintegration/NotificationIcon

最佳答案

所以。最终的解决方案是我这样声明 NOTIFYICONDATA:

#define NOTIFICATIONTRAY_UID 666;
[...]

NOTIFYICONDATA nid = { 0 };

nid.cbSize = sizeof( NOTIFYICONDATA );
nid.hWnd = hWnd;
nid.uID = NOTIFICATIONTRAY_UID;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP;
nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;

LoadIconMetric( hInstance, MAKEINTRESOURCE( IDI_NOTIFICATIONICON ), LIM_SMALL, &nid.hIcon );
lstrcpy( nid.szTip, L"Tooltip text" );
Shell_NotifyIcon( NIM_ADD, &nid );

// NOTIFYICON_VERSION_4 is prefered
nid.uVersion = NOTIFYICON_VERSION_4;
return ( Shell_NotifyIcon( NIM_SETVERSION, &nid ) != 0 );

我没有设置 NIF_GUID 标志和 GUID。要稍后使用此图标(销毁、添加气球通知等),我必须提供 HWND 和 uID(感谢 Remy Lebeau 的建议)。

关于c++ - 清除通知托盘图标,windows shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789852/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com