从托盘中删除图标

发布于 2024-12-14 09:59:11 字数 750 浏览 7 评论 0原文

使用下面的代码会导致有时在调用 removeIconFromTray 方法后图标保留在托盘中,并且仅在用户将鼠标移到托盘中的图标上后才消失。

void CMyDlg::addIconToTray()
{
    static HICON hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hIcon = hIcon;
    data.hWnd = m_hWnd;
    strcpy (data.szTip, m_sTrayIconTip.c_str());
    data.uFlags = NIF_ICON | NIF_TIP;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_ADD, &data);
}

void CMyDlg::removeIconFromTray()
{
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hWnd = m_hWnd;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_DELETE, &data);

}

这段代码有什么问题以及如何实现在从托盘中删除图标的代码完成工作后图标立即从托盘中消失?

Using the code below results in that sometimes an icon remains in a tray right after call to removeIconFromTray method and disappears only after a user moves over an icon in tray.

void CMyDlg::addIconToTray()
{
    static HICON hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hIcon = hIcon;
    data.hWnd = m_hWnd;
    strcpy (data.szTip, m_sTrayIconTip.c_str());
    data.uFlags = NIF_ICON | NIF_TIP;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_ADD, &data);
}

void CMyDlg::removeIconFromTray()
{
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hWnd = m_hWnd;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_DELETE, &data);

}

Whats wrong in this code and how to achieve that an icon disappears from a tray as soon as a code deleting it form there finished working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十雾 2024-12-21 09:59:11

一个明显的问题是您无法初始化结构。您应该这样做:

NOTIFYICONDATA data = { 0 };

除此之外,检查错误并调用 GetLastError 找出导致错误的原因。

One obvious problem is that you are failing to initialize your struct. You should do this:

NOTIFYICONDATA data = { 0 };

Other than that check for errors and call GetLastError to find out what caused any error.

花开浅夏 2024-12-21 09:59:11

根据 MSDN:

Shell_NotifyIcon 函数

从状态区域删除图标。 lpdata指向的NOTIFYICONDATA结构使用图标添加到通知区域(NIM_ADD)时最初分配给图标的ID来标识要删除的图标。

因此,您应该将 NOTIFYICONDATA 的相同数据传递给 Shell_NotifyIcon 函数。

void CMyDlg::addIconToTray()
{
    static HICON hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hIcon = hIcon;
    data.hWnd = m_hWnd;
    strcpy (data.szTip, m_sTrayIconTip.c_str());
    data.uFlags = NIF_ICON | NIF_TIP;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_ADD, &data);
}

void CMyDlg::removeIconFromTray()
{
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hIcon = hIcon;
    data.hWnd = m_hWnd;
    strcpy (data.szTip, m_sTrayIconTip.c_str());
    data.uFlags = NIF_ICON | NIF_TIP;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_DELETE, &data);

}

这将正常工作。
或者,将数据保存到成员变量中。

According to MSDN:

Shell_NotifyIcon function

Deletes an icon from the status area. NOTIFYICONDATA structure pointed to by lpdata uses the ID originally assigned to the icon when it was added to the notification area (NIM_ADD) to identify the icon to be deleted.

So, you should pass the same data of NOTIFYICONDATA to Shell_NotifyIcon function.

void CMyDlg::addIconToTray()
{
    static HICON hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hIcon = hIcon;
    data.hWnd = m_hWnd;
    strcpy (data.szTip, m_sTrayIconTip.c_str());
    data.uFlags = NIF_ICON | NIF_TIP;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_ADD, &data);
}

void CMyDlg::removeIconFromTray()
{
    NOTIFYICONDATA data;

    data.cbSize = sizeof(data);
    data.hIcon = hIcon;
    data.hWnd = m_hWnd;
    strcpy (data.szTip, m_sTrayIconTip.c_str());
    data.uFlags = NIF_ICON | NIF_TIP;
    data.uID = (UINT)this;

    Shell_NotifyIcon (NIM_DELETE, &data);

}

This will work properly.
Or, save the data to a member variable.

倾城°AllureLove 2024-12-21 09:59:11

正如 DavidHeffernan 所说,您应该对 data 结构进行零初始化(您应该始终对传递给 Win32 API 函数的任何结构进行零初始化),例如:

NOTIFYICONDATA data = {0};

或者:

NOTIFYICONDATA data;
ZeroMemory(&data, sizeof(data));

这样,任何未使用的字段都具有一致的和可预测的值。在您的情况下,当调用NIM_DELETE时,您没有初始化data,因此它的uFlags字段将具有随机位,这可能会导致< code>Shell_NotifyIcon() 会误解 NOTIFYICONDATA 的内容并失败,因此您的图标不会被删除。

As DavidHeffernan said, you should zero-initialize your data struct (you should ALWAYS zero-init any struct that you pass to a Win32 API function), eg:

NOTIFYICONDATA data = {0};

Or:

NOTIFYICONDATA data;
ZeroMemory(&data, sizeof(data));

This way, any unused fields have consistent and predictable values. In your case, when calling NIM_DELETE, you are not initializing data, so its uFlags field will have random bits, which is likely to cause Shell_NotifyIcon() to misinterpret the content of your NOTIFYICONDATA and fail, and thus your icon is not removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文