【问题标题】:How do I use the taskbar button to display a progress bar?如何使用任务栏按钮显示进度条?
【发布时间】:2017-08-08 12:50:40
【问题描述】:

我正在使用 Visual Studio 2015 更新应用程序。该应用程序是 15 年前编写的,我想在任务栏按钮上添加一个进度条覆盖,这是 Windows 7 现在提供的。我已经按照我能找到的所有教程进行操作,例如

http://www.codeproject.com/KB/vista/SevenGoodiesTaskbarStatus.aspx

https://www.codeproject.com/Articles/80082/Windows-How-to-display-progress-bar-on-taskbar-i

但是,它们似乎都使用过时的命名空间,例如 MESSAGE_HANDLER_EX 这给了我一大堆错误。有谁知道怎么做?

【问题讨论】:

标签: c++ visual-studio-2015 mfc taskbar


【解决方案1】:

正如这里已经提到的,您指出的示例项目不使用 MFC,而是使用 WTL,它是 ATL 的扩展,当前未随 Visual Studio 提供。 因此,要使它们编译,您必须 download WTL,安装并删除一些已弃用的东西。

当然,ITaskbarList3 接口也可以在 MFC 应用程序中使用。 首先,这是一个简短的示例:

class CMainDialog : public CDialog
{
    // ...
    CComPtr<ITaskbarList3> m_spTaskbarList;
};

BOOL CMainDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    // ...

    HRESULT hr = ::CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, 
        __uuidof(ITaskbarList3), reinterpret_cast<void**>(&m_spTaskbarList));

    if(SUCCEDDED(hr))
    {
       hr = m_spTaskbarList->HrInit();
    }

    // ...
    return TRUE;
}

....当然,不要忘记在应用程序的类 InitInstance 方法中调用 AfxOleInit

[稍后编辑]

对不起,我之前的例子是错误的!根据文档,在调用任何 ITaskbarList3 方法之前,必须处理“TaskbarButtonCreated”注册消息,以确保任务栏按钮到位。

UINT WM_TASKBAR_BUTTON_CREATED = ::RegisterWindowMessage(_T("TaskbarButtonCreated"));

BEGIN_MESSAGE_MAP(CMainDialog, CDialogEx)
    // ...
    ON_REGISTERED_MESSAGE(WM_TASKBAR_BUTTON_CREATED, OnTaskbarButtonCreated)
END_MESSAGE_MAP()

LRESULT CMainDialog::OnTaskbarButtonCreated(WPARAM wParam, LPARAM lParam)
{
    HRESULT hr = ::CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
        IID_ITaskbarList3, reinterpret_cast<void**>(&m_spTaskbarList));

    if (FAILED(hr))
    {
        // handle error
        return 0;
    }


    hr = m_spTaskbarList->HrInit();
    // ....
    // ... other taskbar list stuff.

    return  0;
}

另请参阅这篇文章:Windows 7: Adding toolbar buttons to taskbar button flyout

【讨论】:

  • 谢谢,我会在哪里实现 SetProgressState?它会在 OnInitDialog 内还是其他地方?
  • 可能想在其他地方称呼它,以表明发生了什么事。任你选择。
  • SUCCEDDED?真的吗?
【解决方案2】:

MESSAGE_HANDLER_EX 宏是 WTL 的一部分。它在atlcrack.h 中定义。 您很可能需要获取最新的 WTL 才能在 Visual Studio 2015 中编译项目。

正如 ISun 已经提到的,任务栏进度可以基于这篇 MSDN 文章中描述的 API 实现:https://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#progress

ITaskbarList3 接口有一个很好的包装器:https://www.codeproject.com/Articles/42345/Windows-Goodies-in-C-Taskbar-Progress-and-Status

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2011-10-15
    • 2011-05-19
    • 2012-12-27
    相关资源
    最近更新 更多