C++实现任务栏图标进度条显示

ITaskbarList3* m_pTaskBarlist;
VOID CALLBACK OnTimer(HWND hWnd, UINT, UINT_PTR id, DWORD) {
  if (id == 1) {
    static int progress = 0;
    if (progress > 100) {
      m_pTaskBarlist->SetProgressState(hWnd, TBPF_NOPROGRESS);
      FlashWindow(hWnd,true);
      ::KillTimer(hWnd, 1);
    }
    else {
      if (progress > 50) {
        m_pTaskBarlist->SetProgressState(hWnd, TBPF_PAUSED);
        ::KillTimer(hWnd, 1);
      }
      else {
        m_pTaskBarlist->SetProgressValue(hWnd, ++progress, 100);
      }
    }
  }
}

void CreateTaskBarList(HWND hWnd) {
  if (FAILED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL, IID_ITaskbarList3, (LPVOID*)&m_pTaskBarlist))
    || FAILED(m_pTaskBarlist->HrInit())) {
    m_pTaskBarlist->Release();
  }
  else {
    m_pTaskBarlist->SetProgressState(hWnd, TBPF_INDETERMINATE);
    m_pTaskBarlist->SetProgressValue(hWnd, 0, 100);
    ::SetTimer(hWnd, 1, 50, OnTimer);
  }
}

// 伪代码
void InitWindow(HWND hWnd) {
  CreateTaskBarList(hWnd);
  SetTimer(hWnd,1,OnTimer);
}

SetProgressValue设置进度:https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-itaskbarlist3-setprogressvalue
SetProgressState设置状态:https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-itaskbarlist3-setprogressstate

本文为@艺文笔记原创文章,转载请注明出处:https://www.xuwenyan.com/archives/138,非常感谢!
上一篇:  下一篇: