简体   繁体   English

Win32 API,系统托盘图标消失

[英]Win32 API, System Tray Icon disapearing

I'm currently working on a Win32 application and I want to have an icon in the system task bar. 我当前正在使用Win32应用程序,并且我希望系统任务栏中有一个图标。 I'm using Shell_NotifyIcon to display the icon, and it works well but often the icon disapears without explanation... 我正在使用Shell_NotifyIcon来显示图标,它工作正常,但经常消失而无须说明...

In fact, the bug appears the first time I run the code in Visual Studio 2013 after having built the code OR when I run the application from the .exe in another directory (I don't have any error like missing dll and the application is running, but the icon is not there anymore... ). 实际上,该错误是我在构建代码后第一次在Visual Studio 2013中运行代码时出现的,或者当我从另一个目录中的.exe运行应用程序时出现的错误(我没有任何错误,例如missing dll并且该应用程序是正在运行,但图标不再存在...)。

The code I use to create the icon: 我用来创建图标的代码:

// At the beginning of the file
static NOTIFYICONDATA m_nid ;

// After
m_nid.cbSize = sizeof (m_nid);
m_nid.hWnd = hwnd;
m_nid.uVersion = NOTIFYICON_VERSION_4 ;
static const GUID myGUID =
{0x5CA81ADA, 0xA481, 0x4BA8, {0x8B, 0x70, 0x80, 0x3f, 0x48, 0x67, 0xA1, 0x68}};
m_nid.guidItem = myGUID;

m_nid.uFlags = NIF_ICON | NIF_TIP | NIF_GUID | NIF_MESSAGE ;
m_nid.uCallbackMessage = WM_SYSTEM_TRAY;
StringCchCopy (m_nid.szTip, ARRAYSIZE (m_nid.szTip), NotificationManager::APPLICATION_NAME.c_str ());
m_nid.hIcon = (HICON) LoadImage (NULL, L"icon_v1.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);

ShowWindow (m_nid.hWnd, SW_HIDE);
Shell_NotifyIcon (NIM_ADD, &m_nid);
Shell_NotifyIcon (NIM_SETVERSION, &m_nid);

Edit: I think it's a problem with the GUID because in Release mode the icon was not showing, but since I changed the GUID it's working... But it still not works when I run the program outside Visual Studio... 编辑:我认为这是GUID的问题,因为在“ Release模式下未显示该图标,但是由于我更改了GUID ,它仍在工作...但是当我在Visual Studio外部运行程序时,它仍然不起作用...

There are several possibilities for why a system tray icon disappears. 系统任务栏图标消失的原因有多种。

  1. The HWND associated with the icon was destroyed. 与该图标关联的HWND被销毁。

  2. In modern Windows versions, the icon has been hidden from view by user settings outside of your app's control. 在现代Windows版本中,该图标已被应用程序控制范围之外的用户设置隐藏起来。

  3. Windows Explorer crashed and restarted, but you did not handle the TaskbarCreated message to re-add your icon. Windows资源管理器崩溃并重新启动,但是您没有处理TaskbarCreated消息来重新添加图标。

Edit : Another possibility is that you are simply passing bad input to Shell_NotifyIcon() and it is failing with an error that you are ignoring. 编辑 :另一种可能性是您只是将错误的输入传递给Shell_NotifyIcon()并且由于失败而失败,而您忽略了一个错误。

It is most likely intended behavior. 这很可能是预期的行为。 Windows 7 (and later) is very aggressive about not always displaying notification icons, because they are, and were, heavily abused. Windows 7(及更高版本)非常激进,因为并不总是显示通知图标,因为它们曾经而且曾经被严重滥用。 Their intend were, as the name implies, to provide notifications, and nothing else. 顾名思义,它们的意图是提供通知,仅此而已。

The API doesn't guarantee that your icon will be visible. API不保证您的图标将可见。 It only allows you to register that you would like to make a notification. 它仅允许您注册您要发出通知的内容。 It is up to the OS to decide whether that notification should be displayed, and for how long. 由操作系统决定是否应显示该通知以及显示多长时间。

If you go into Notification Area Icons in the Control Panel, you can see the settings that are applied to each icon. 如果进入“控制面板”中的“通知区域图标”,则可以看到应用于每个图标的设置。 Most likely, yours is set to either "Hide icon and notifications", or "only show notifications". 最有可能将您设置为“隐藏图标和通知”或“仅显示通知”。

And no, there is nothing you can do about that, other than asking the user to allow your icon. 不,除了要求用户允许您的图标外,您无能为力。 Once again, the API is for delivering notifications, and if the user doesn't want to see your icon or your notification, the OS respects that. 同样,该API用于传递通知,并且如果用户不想看到您的图标或通知,则操作系统会尊重这一点。

from the notification documentation on msdn I see this: 从msdn的通知文档中,我看到以下内容:

> Each icon in the notification area can be identified in two ways:
> 
>     The GUID with which the icon is declared in the registry. This is the preferred method on Windows 7 and later.
>     The handle of a window associated with the notification area icon, plus an application-defined icon identifier. This method is used on
> Windows Vista and earlier.

here is the link also: http://msdn.microsoft.com/en-us/library/windows/desktop/ee330740%28v=vs.85%29.aspx 这也是链接: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ee330740%28v=vs.85%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM