VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1727|回复: 3

[交流] VC实现Win7任务栏编程

[复制链接]
51_avatar_middle
online_admins 发表于 2016-8-23 08:56:27 | 显示全部楼层 |阅读模式
今天安装一个游戏,看到任务栏有进度条显示,就到网上查了资料,但发现大多数都是C#.Net的,关于MFC的只有下面一篇文章http://www.shuax.com/?p=942,介绍的挺简单的,看其给的例子的代码也不难,涉及到COM编程。

文章中提到的头文件-----win7shellapi.h

  1. /* Win7ShellApi.h -- Window 7 Shell API */

  2. #ifndef __Win7ShellApi_h__
  3. #define __Win7ShellApi_h__

  4. #include <ole2.h>

  5. #ifndef MSGFLT_ADD
  6. #define MSGFLT_ADD 1
  7. #endif


  8. #ifndef __ITaskbarList3_INTERFACE_DEFINED__
  9. #define __ITaskbarList3_INTERFACE_DEFINED__

  10. EXTERN_C const CLSID CLSID_TaskbarList;

  11. EXTERN_C const IID IID_ITaskbarList3Vtbl;

  12. typedef enum _THUMBBUTTONFLAGS {
  13.     THBF_ENABLED    = 0,
  14.     THBF_DISABLED    = 0x1,
  15.     THBF_DISMISSONCLICK    = 0x2,
  16.     THBF_NOBACKGROUND    = 0x4,
  17.     THBF_HIDDEN    = 0x8,
  18.     THBF_NONINTERACTIVE    = 0x10
  19. } THUMBBUTTONFLAGS;

  20. typedef enum _THUMBBUTTONMASK {
  21.     THB_BITMAP    = 0x1,
  22.     THB_ICON    = 0x2,
  23.     THB_TOOLTIP    = 0x4,
  24.     THB_FLAGS    = 0x8
  25. } THUMBBUTTONMASK;

  26. #include <pshpack8.h>
  27. typedef struct _THUMBBUTTON
  28. {
  29.     THUMBBUTTONMASK dwMask;
  30.     UINT iId;
  31.     UINT iBitmap;
  32.     HICON hIcon;
  33.     WCHAR szTip[ 260 ];
  34.     THUMBBUTTONFLAGS dwFlags;
  35. } THUMBBUTTON, *LPTHUMBBUTTON;

  36. #include <poppack.h>
  37. #define THBN_CLICKED        0x1800

  38. typedef enum _TBPFLAG {
  39.     TBPF_NOPROGRESS    = 0,
  40.     TBPF_INDETERMINATE    = 0x1,
  41.     TBPF_NORMAL    = 0x2,
  42.     TBPF_ERROR    = 0x4,
  43.     TBPF_PAUSED    = 0x8
  44. } TBPFLAG;

  45. //typedef _COM_interface ITaskbarList3Vtbl ITaskbarList3Vtbl;

  46. #define INTERFACE ITaskbarList3Vtbl
  47. DECLARE_INTERFACE_(ITaskbarList3Vtbl,IUnknown)
  48. {
  49.     STDMETHOD(QueryInterface)(THIS_ REFIID,LPVOID*) PURE;
  50.     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
  51.     STDMETHOD_(ULONG,Release)(THIS) PURE;
  52.     STDMETHOD(HrInit)(THIS) PURE;
  53.     STDMETHOD(AddTab)(THIS_ HWND) PURE;
  54.     STDMETHOD(DeleteTab)(THIS_ HWND) PURE;
  55.     STDMETHOD(ActivateTab)(THIS_ HWND) PURE;
  56.     STDMETHOD(SetActiveAlt)(THIS_ HWND) PURE;
  57.     STDMETHOD(MarkFullscreenWindow)(THIS_ HWND,BOOL) PURE;
  58.     STDMETHOD(SetProgressValue)(THIS_ HWND,ULONGLONG,ULONGLONG) PURE;
  59.     STDMETHOD(SetProgressState)(THIS_ HWND,DWORD) PURE;
  60.     STDMETHOD(RegisterTab)(THIS_ HWND,HWND) PURE;
  61.     STDMETHOD(UnregisterTab)(THIS_ HWND) PURE;
  62.     STDMETHOD(SetTabOrder)(THIS_ HWND,HWND) PURE;
  63.     STDMETHOD(SetTabActive)(THIS_ HWND,HWND,DWORD) PURE;
  64.     STDMETHOD(ThumbBarAddButtons)(THIS_ HWND,UINT,LPTHUMBBUTTON) PURE;
  65.     STDMETHOD(ThumbBarUpdateButtons)(THIS_ HWND,UINT,LPTHUMBBUTTON) PURE;
  66.     STDMETHOD(ThumbBarSetImageList)(THIS_ HWND,HIMAGELIST) PURE;
  67.     STDMETHOD(SetOverlayIcon)(THIS_ HWND,HICON,LPCWSTR) PURE;
  68.     STDMETHOD(SetThumbnailTooltip)(THIS_ HWND,LPCWSTR) PURE;
  69.     STDMETHOD(SetThumbnailClip)(THIS_ HWND,HICON,RECT) PURE;
  70. };
  71. #undef INTERFACE
  72. typedef struct ITaskbarList3Vtbl ITaskbarList3;

  73. #if !defined(__cplusplus) || defined(CINTERFACE)
  74. #define ITaskbarList3_QueryInterface(p,riid,ppvObject) (p)->lpVtbl->QueryInterface(p,riid,ppvObject)
  75. #define ITaskbarList3_AddRef(p) (p)->lpVtbl->AddRef(p)
  76. #define ITaskbarList3_Release(p) (p)->lpVtbl->Release(p)
  77. #define ITaskbarList3_HrInit(p) (p)->lpVtbl->HrInit(p)
  78. #define ITaskbarList3_AddTab(p,hwnd) (p)->lpVtbl->AddTab(p,hwnd)
  79. #define ITaskbarList3_DeleteTab(p,hwnd) (p)->lpVtbl->DeleteTab(p,hwnd)
  80. #define ITaskbarList3_ActivateTab(p,hwnd) (p)->lpVtbl->ActivateTab(p,hwnd)
  81. #define ITaskbarList3_SetActiveAlt(p,hwnd) (p)->lpVtbl->SetActiveAlt(p,hwnd)
  82. #define ITaskbarList3_MarkFullscreenWindow(p,hwnd,fFullscreen) (p)->lpVtbl->MarkFullscreenWindow(p,hwnd,fFullscreen)
  83. #define ITaskbarList3_SetProgressValue(p,hwnd,ullCompleted,ullTotal) (p)->lpVtbl->SetProgressValue(p,hwnd,ullCompleted,ullTotal)
  84. #define ITaskbarList3_SetProgressState(p,hwnd,tbpFlags) (p)->lpVtbl->SetProgressState(p,hwnd,tbpFlags)
  85. #define ITaskbarList3_RegisterTab(p,hwndTab,hwndMDI) (p)->lpVtbl->RegisterTab(p,hwndTab,hwndMDI)
  86. #define ITaskbarList3_UnregisterTab(p,hwndTab) (p)->lpVtbl->UnregisterTab(p,hwndTab)
  87. #define ITaskbarList3_SetTabOrder(p,hwndTab,hwndInsertBefore) (p)->lpVtbl->SetTabOrder(p,hwndTab,hwndInsertBefore)
  88. #define ITaskbarList3_SetTabActive(p,hwndTab,hwndMDI,dwReserved) (p)->lpVtbl->SetTabActive(p,hwndTab,hwndMDI,dwReserved)
  89. #define ITaskbarList3_ThumbBarAddButtons(p,hwnd,cButtons,pButton) (p)->lpVtbl->ThumbBarAddButtons(p,hwnd,cButtons,pButton)
  90. #define ITaskbarList3_ThumbBarUpdateButtons(p,hwnd,cButtons,pButton) (p)->lpVtbl->ThumbBarUpdateButtons(p,hwnd,cButtons,pButton)
  91. #define ITaskbarList3_ThumbBarSetImageList(p,hwnd,himl) (p)->lpVtbl->ThumbBarSetImageList(p,hwnd,himl)
  92. #define ITaskbarList3_SetOverlayIcon(p,hwnd,hIcon,pszDescription) (p)->lpVtbl->SetOverlayIcon(p,hwnd,hIcon,pszDescription)
  93. #define ITaskbarList3_SetThumbnailTooltip(p,hwnd,pszTip) (p)->lpVtbl->SetThumbnailTooltip(p,hwnd,pszTip)
  94. #define ITaskbarList3_SetThumbnailClip(p,hwnd,prcClip) (p)->lpVtbl->SetThumbnailClip(p,hwnd,prcClip)
  95. #else  /* !defined(__cplusplus) || defined(CINTERFACE) */
  96. #define ITaskbarList3_QueryInterface(p,riid,ppvObject) (p)->QueryInterface(p,riid,ppvObject)
  97. #define ITaskbarList3_AddRef(p) (p)->AddRef(p)
  98. #define ITaskbarList3_Release(p) (p)->Release(p)
  99. #define ITaskbarList3_HrInit(p) (p)->HrInit(p)
  100. #define ITaskbarList3_AddTab(p,hwnd) (p)->AddTab(p,hwnd)
  101. #define ITaskbarList3_DeleteTab(p,hwnd) (p)->DeleteTab(p,hwnd)
  102. #define ITaskbarList3_ActivateTab(p,hwnd) (p)->ActivateTab(p,hwnd)
  103. #define ITaskbarList3_SetActiveAlt(p,hwnd) (p)->SetActiveAlt(p,hwnd)
  104. #define ITaskbarList3_MarkFullscreenWindow(p,hwnd,fFullscreen) (p)->MarkFullscreenWindow(p,hwnd,fFullscreen)
  105. #define ITaskbarList3_SetProgressValue(p,hwnd,ullCompleted,ullTotal) (p)->SetProgressValue(p,hwnd,ullCompleted,ullTotal)
  106. #define ITaskbarList3_SetProgressState(p,hwnd,tbpFlags) (p)->SetProgressState(p,hwnd,tbpFlags)
  107. #define ITaskbarList3_RegisterTab(p,hwndTab,hwndMDI) (p)->RegisterTab(p,hwndTab,hwndMDI)
  108. #define ITaskbarList3_UnregisterTab(p,hwndTab) (p)->UnregisterTab(p,hwndTab)
  109. #define ITaskbarList3_SetTabOrder(p,hwndTab,hwndInsertBefore) (p)->SetTabOrder(p,hwndTab,hwndInsertBefore)
  110. #define ITaskbarList3_SetTabActive(p,hwndTab,hwndMDI,dwReserved) (p)->SetTabActive(p,hwndTab,hwndMDI,dwReserved)
  111. #define ITaskbarList3_ThumbBarAddButtons(p,hwnd,cButtons,pButton) (p)->ThumbBarAddButtons(p,hwnd,cButtons,pButton)
  112. #define ITaskbarList3_ThumbBarUpdateButtons(p,hwnd,cButtons,pButton) (p)->ThumbBarUpdateButtons(p,hwnd,cButtons,pButton)
  113. #define ITaskbarList3_ThumbBarSetImageList(p,hwnd,himl) (p)->ThumbBarSetImageList(p,hwnd,himl)
  114. #define ITaskbarList3_SetOverlayIcon(p,hwnd,hIcon,pszDescription) (p)->SetOverlayIcon(p,hwnd,hIcon,pszDescription)
  115. #define ITaskbarList3_SetThumbnailTooltip(p,hwnd,pszTip) (p)->SetThumbnailTooltip(p,hwnd,pszTip)
  116. #define ITaskbarList3_SetThumbnailClip(p,hwnd,prcClip) (p)->SetThumbnailClip(p,hwnd,prcClip)
  117. #endif  /* !defined(__cplusplus) || defined(CINTERFACE) */

  118. #endif     /* __ITaskbarList3_INTERFACE_DEFINED__ */

  119. #endif /* __Win7ShellApi_h__ */
复制代码


但是如果只包含这个头文件会报错,还要引入
  1. #include <commctrl.h>
  2. #include <Shobjidl.h>
复制代码


两个头文件

加入以下代码就有进度条效果了

  1. ITaskbarList3 * pTaskbarList = NULL;
  2. CoInitialize(0);
  3. CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL, IID_ITaskbarList, (void **)&pTaskbarList);
  4. /*
  5. 设置进度条的类型
  6. TBPF_NORMAL、TBPF_INDETERMINATE、TBPF_NOPROGRESS------蓝色
  7. TBPF_ERROR-------红色
  8. TBPF_PAUSED------黄色
  9. */
  10. pTaskbarList->SetProgressState(this->GetSafeHwnd(),TBPF_PAUSED);
  11. /*
  12. 设置进度条的位置,现在设为7/10
  13. */
  14. pTaskbarList->SetProgressValue(this->GetSafeHwnd(),7,10);
复制代码


还有一点,不能再界面还未显示出来的时候调用以上代码。

评分

参与人数 2驿站币 +2 热心值 +2 收起 理由
22_avatar_small sunflover + 1 + 1 赞一个!刚好最近也在考虑加上这个。
25_avatar_small berry + 1 + 1 感谢分享!

查看全部评分





上一篇:boost 之 function and bind
下一篇:2016年国外一款密码解霸 类似国内的密码结巴,但比密码结巴强大。网页接收密码。
24_avatar_middle
在线会员 发表于 2016-8-23 12:03:48 | 显示全部楼层
我很好奇是什么游戏啊VC实现Win7任务栏编程

还有,最后syc忘了释放呢。。。。
  1. pointer->Release();
  2. pointer = NULL;
复制代码
85_avatar_middle
online_vip 发表于 2016-8-23 13:14:12 | 显示全部楼层
VC实现Win7任务栏编程
20_avatar_middle
在线会员 发表于 2016-8-24 14:19:51 | 显示全部楼层
支持VC实现Win7任务栏编程
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号tongdun|网站地图wx_jqr

GMT+8, 2019-3-21 02:49

Powered by Discuz! X3.4

© 2009-2019 cctry.com

快速回复 返回顶部 返回列表