©2005 黄友生。本文由原作者发布于MSN Space、www.csdn.net、www.veryinfo.com。你可以保存、在非商业软件中使用、在非盈利性文章中引用本文中的部分或全部文字,但请注明作者及原文地址。要用于其它用途,请先联系作者(eien@eyou.com)。作者不保证本文完全正确无误、不对因本文中的理论或代码缺陷造成的损失负责。
在 IE4.0 或更新版本中,windows 提供了一个 ITaskbarList 接口,通过此接口可以做到让某个窗口不在任务栏上显示。按如下步骤操作即可:
1. 在最新的 Platform SDK 中,文件 ShObjIdl.h 定义了 ITaskbarList 这个接口,因此要包含此头文件。如果你没有Platform SDK,那么把以下代码复制到自己的.h或.cpp文件中也是可以的。
#ifndef __ITaskbarList_INTERFACE_DEFINED__
#define __ITaskbarList_INTERFACE_DEFINED__
EXTERN_C const IID IID_ITaskbarList;
#if defined(__cplusplus) && !defined(CINTERFACE)
MIDL_INTERFACE("56FDF342-FD6D-11d0-958A-006097C9A090")
ITaskbarList : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE HrInit( void) = 0;
virtual HRESULT STDMETHODCALLTYPE AddTab(
/* [in] */ HWND hwnd) = 0;
virtual HRESULT STDMETHODCALLTYPE DeleteTab(
/* [in] */ HWND hwnd) = 0;
virtual HRESULT STDMETHODCALLTYPE ActivateTab(
/* [in] */ HWND hwnd) = 0;
virtual HRESULT STDMETHODCALLTYPE SetActiveAlt(
/* [in] */ HWND hwnd) = 0;
};
#else /* C style interface */
typedef struct ITaskbarListVtbl
{
BEGIN_INTERFACE
HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
ITaskbarList * This,
/* [in] */ REFIID riid,
/* [iid_is][out] */ void **ppvObject);
ULONG ( STDMETHODCALLTYPE *AddRef )(
ITaskbarList * This);
ULONG ( STDMETHODCALLTYPE *Release )(
ITaskbarList * This);
HRESULT ( STDMETHODCALLTYPE *HrInit )(
ITaskbarList * This);
HRESULT ( STDMETHODCALLTYPE *AddTab )(
ITaskbarList * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *DeleteTab )(
ITaskbarList * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *ActivateTab )(
ITaskbarList * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *SetActiveAlt )(
ITaskbarList * This,
/* [in] */ HWND hwnd);
END_INTERFACE
} ITaskbarListVtbl;
interface ITaskbarList
{
CONST_VTBL struct ITaskbarListVtbl *lpVtbl;
};
#ifdef COBJMACROS
#define ITaskbarList_QueryInterface(This,riid,ppvObject) (This)->lpVtbl -> QueryInterface(This,riid,ppvObject)
#define ITaskbarList_AddRef(This) (This)->lpVtbl -> AddRef(This)
#define ITaskbarList_Release(This) (This)->lpVtbl -> Release(This)
#define ITaskbarList_HrInit(This) (This)->lpVtbl -> HrInit(This)
#define ITaskbarList_AddTab(This,hwnd) (This)->lpVtbl -> AddTab(This,hwnd)
#define ITaskbarList_DeleteTab(This,hwnd) (This)->lpVtbl -> DeleteTab(This,hwnd)
#define ITaskbarList_ActivateTab(This,hwnd) (This)->lpVtbl -> ActivateTab(This,hwnd)
#define ITaskbarList_SetActiveAlt(This,hwnd) (This)->lpVtbl -> SetActiveAlt(This,hwnd)
#endif /* COBJMACROS */
#endif /* C style interface */
HRESULT STDMETHODCALLTYPE ITaskbarList_HrInit_Proxy(
ITaskbarList * This);
void __RPC_STUB ITaskbarList_HrInit_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
HRESULT STDMETHODCALLTYPE ITaskbarList_AddTab_Proxy(
ITaskbarList * This,
/* [in] */ HWND hwnd);
void __RPC_STUB ITaskbarList_AddTab_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
HRESULT STDMETHODCALLTYPE ITaskbarList_DeleteTab_Proxy(
ITaskbarList * This,
/* [in] */ HWND hwnd);
void __RPC_STUB ITaskbarList_DeleteTab_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
HRESULT STDMETHODCALLTYPE ITaskbarList_ActivateTab_Proxy(
ITaskbarList * This,
/* [in] */ HWND hwnd);
void __RPC_STUB ITaskbarList_ActivateTab_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
HRESULT STDMETHODCALLTYPE ITaskbarList_SetActiveAlt_Proxy(
ITaskbarList * This,
/* [in] */ HWND hwnd);
void __RPC_STUB ITaskbarList_SetActiveAlt_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
#endif /* __ITaskbarList_INTERFACE_DEFINED__ */
#ifndef __ITaskbarList2_INTERFACE_DEFINED__
#define __ITaskbarList2_INTERFACE_DEFINED__
/* interface ITaskbarList2 */
/* [object][uuid] */
EXTERN_C const IID IID_ITaskbarList2;
#if defined(__cplusplus) && !defined(CINTERFACE)
MIDL_INTERFACE("602D4995-B13A-429b-A66E-1935E44F4317")
ITaskbarList2 : public ITaskbarList
{
public:
virtual HRESULT STDMETHODCALLTYPE MarkFullscreenWindow(
/* [in] */ HWND hwnd,
/* [in] */ BOOL fFullscreen) = 0;
};
#else /* C style interface */
typedef struct ITaskbarList2Vtbl
{
BEGIN_INTERFACE
HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
ITaskbarList2 * This,
/* [in] */ REFIID riid,
/* [iid_is][out] */ void **ppvObject);
ULONG ( STDMETHODCALLTYPE *AddRef )(
ITaskbarList2 * This);
ULONG ( STDMETHODCALLTYPE *Release )(
ITaskbarList2 * This);
HRESULT ( STDMETHODCALLTYPE *HrInit )(
ITaskbarList2 * This);
HRESULT ( STDMETHODCALLTYPE *AddTab )(
ITaskbarList2 * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *DeleteTab )(
ITaskbarList2 * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *ActivateTab )(
ITaskbarList2 * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *SetActiveAlt )(
ITaskbarList2 * This,
/* [in] */ HWND hwnd);
HRESULT ( STDMETHODCALLTYPE *MarkFullscreenWindow )(
ITaskbarList2 * This,
/* [in] */ HWND hwnd,
/* [in] */ BOOL fFullscreen);
END_INTERFACE
} ITaskbarList2Vtbl;
interface ITaskbarList2
{
CONST_VTBL struct ITaskbarList2Vtbl *lpVtbl;
};
#ifdef COBJMACROS
#define ITaskbarList2_QueryInterface(This,riid,ppvObject) (This)->lpVtbl -> QueryInterface(This,riid,ppvObject)
#define ITaskbarList2_AddRef(This) (This)->lpVtbl -> AddRef(This)
#define ITaskbarList2_Release(This) (This)->lpVtbl -> Release(This)
#define ITaskbarList2_HrInit(This) (This)->lpVtbl -> HrInit(This)
#define ITaskbarList2_AddTab(This,hwnd) (This)->lpVtbl -> AddTab(This,hwnd)
#define ITaskbarList2_DeleteTab(This,hwnd) (This)->lpVtbl -> DeleteTab(This,hwnd)
#define ITaskbarList2_ActivateTab(This,hwnd) (This)->lpVtbl -> ActivateTab(This,hwnd)
#define ITaskbarList2_SetActiveAlt(This,hwnd) (This)->lpVtbl -> SetActiveAlt(This,hwnd)
#define ITaskbarList2_MarkFullscreenWindow(This,hwnd,fFullscreen) (This)->lpVtbl -> MarkFullscreenWindow(This,hwnd,fFullscreen)
#endif /* COBJMACROS */
#endif /* C style interface */
HRESULT STDMETHODCALLTYPE ITaskbarList2_MarkFullscreenWindow_Proxy(
ITaskbarList2 * This,
/* [in] */ HWND hwnd,
/* [in] */ BOOL fFullscreen);
void __RPC_STUB ITaskbarList2_MarkFullscreenWindow_Stub(
IRpcStubBuffer *This,
IRpcChannelBuffer *_pRpcChannelBuffer,
PRPC_MESSAGE _pRpcMessage,
DWORD *_pdwStubPhase);
#endif /* __ITaskbarList2_INTERFACE_DEFINED__ */
2. 调用以下函数实现在任务栏上的隐藏及显示。
void ShowInTaskbar( HWND hWnd, BOOL bShow )
{
// 在程序的起始处(例如 InitInstance 中)加入 ::CoInitialize( NULL );
HRESULT hr;
ITaskbarList* pTaskbarList;
hr = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
IID_ITaskbarList, ( void** )&pTaskbarList );
pTaskbarList->HrInit( );
if( bShow )
{
pTaskbarList->AddTab( hWnd );
}
else
{
pTaskbarList->DeleteTab( hWnd );
}
pTaskbarList->Release( );
// 在程序的末尾(例如 ExitInstance 中)加入::CoUninitialize( );
}
3. 打完收工。