Click here to Skip to main content
15,891,942 members
Articles / Desktop Programming / MFC
Article

How To Hide A Window in TaskBar

Rate me:
Please Sign up or sign in to vote.
2.80/5 (22 votes)
16 Apr 20041 min read 131.2K   35   18
This article explains how to hide a window's name in the taskbar while the window itself is still active.

Introduction

Sometimes we may want to make an app which doesn't actually need an annoying box in the taskbar. I hope this snippet will help.

Steps

  1. Global Declaration

    Here is some short explanation about the interface used:

    • DECLARE_INTERFACE(iface) is used to declare an interface that does not derive from a base interface.
    • DECLARE_INTERFACE_(iface, baseiface) is used to declare an interface that does derive from a base interface. This is the one that is used. And the interface will be derived from IUnknown interface.

    And then, let's create an alias definition for the derived interface.

    DECLARE_INTERFACE_(ITaskbarList,IUnknown)
    {
    STDMETHOD(QueryInterface)(THIS_ REFIID riid,LPVOID* ppvObj) PURE;
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
    STDMETHOD_(ULONG,Release)(THIS) PURE;
    STDMETHOD(ActiveTab)(HWND) PURE;
    STDMETHOD(AddTab)(HWND) PURE;
    STDMETHOD(DeleteTab)(HWND) PURE;
    STDMETHOD(HrInit)(HWND) PURE;
    };
    //alias
    typedef ITaskbarList *LPITaskbarList;
  2. In the Dialog Based Class declaration

    Here is up to you, whether you want to declare the pTaskbar as an attribute of a Dialog class or not. It's not a problem, actually, since the implementation (next step) will only need the window handle (HWND).

    class CMyDlg : public CDialog
    {
    .
    .
    //Init our Taskbar handler
    LPITaskbarList pTaskbar;
    .
    .
    }

    Don't forget to set the pTaskbar to NULL in the construction method for your dialog class.

  3. Initialization
    BOOL CMyDlg::OnInitDialog()
    {
    .
    .
    //initializes the Component Object Model(COM)
    CoInitialize(0);
    //We call below function since we only need to create one object
    CoCreateInstance(CLSID_TaskbarList,0, 
      CLSCTX_INPROC_SERVER,IID_ITaskbarList,(void**)&pTaskbar);
    //Below function will initialize the taskbar list object
    pTaskbar->HrInit(this);
    .
    .
    .
    }
  4. Implementation

    This is the function that you can use to hide the "box" in taskbar.

    void CMyDlg::DeleteTaskbar()
    {
    //Hide it
    pTaskbar->DeleteTab(this);
    }

Try the other methods for pTaskbar, and you will experience some stuff.

Forgive me if this article doesn't explain much. My purpose is just to provide another alternative. Since this "way" hasn't been posted yet.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Business Analyst HSBC, Citi
Indonesia Indonesia
Multi Platform System Analyst, Application Developer, Database Designer, and Project Manager in a wide variety of Business Applications and Industrial Automations.

Experienced for in-depth data analysis, data warehousing, reporting, and actively involve in supporting the business growth.

Comments and Discussions

 
QuestionWindows 10 hides such Windows from Alt+Tab menu ... Pin
schlaubstar25-Sep-18 4:46
schlaubstar25-Sep-18 4:46 
Questionerror C2065: 'LPITaskbarList' : undeclared identifier Pin
_Flaviu18-Jan-18 0:46
_Flaviu18-Jan-18 0:46 
AnswerRe: error C2065: 'LPITaskbarList' : undeclared identifier Pin
Jochen Arndt18-Jan-18 2:09
professionalJochen Arndt18-Jan-18 2:09 
QuestionHrInit() interface declaration Pin
jph.torcy21-Jun-11 3:12
jph.torcy21-Jun-11 3:12 
Hi,

Here is the good declaration of the ITaskbarList interface:

DECLARE_INTERFACE_(ITaskbarList, IUnknown)  
{  
    STDMETHOD (HrInit)       (THIS) PURE;  
    STDMETHOD (AddTab)       (THIS_ HWND hwnd) PURE;  
    STDMETHOD (DeleteTab)    (THIS_ HWND hwnd) PURE;  
    STDMETHOD (ActivateTab)  (THIS_ HWND hwnd) PURE;  
    STDMETHOD (SetActiveAlt) (THIS_ HWND hwnd) PURE;  
};  
typedef ITaskbarList *LPITaskbarList;  


HrInit() has no parameter (Cf. http://msdn.microsoft.com/en-us/library/bb774652%28v=vs.85%29.aspx[^])

Here you can find a small example:

- Create a new MFC project named TestITaskbarList
- select Dialog based
- Click Next, ..., Next, Finish
- add a button and double click on it
- Save the project

- update the TestITaskbarListDlg.h file:

// TestITaskbarListDlg.h : header file
//

#if !defined(AFX_TESTITASKBARLISTDLG_H__)
#define AFX_TESTITASKBARLISTDLG_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

DECLARE_INTERFACE_(ITaskbarList, IUnknown)  
{  
    STDMETHOD (HrInit)       (THIS) PURE;  
    STDMETHOD (AddTab)       (THIS_ HWND hwnd) PURE;  
    STDMETHOD (DeleteTab)    (THIS_ HWND hwnd) PURE;  
    STDMETHOD (ActivateTab)  (THIS_ HWND hwnd) PURE;  
    STDMETHOD (SetActiveAlt) (THIS_ HWND hwnd) PURE;  
};  
typedef ITaskbarList *LPITaskbarList;  

/////////////////////////////////////////////////////////////////////////////
// CTestITaskbarListDlg dialog

class CTestITaskbarListDlg : public CDialog
{
// Construction
public:
	CTestITaskbarListDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CTestITaskbarListDlg)
	enum { IDD = IDD_TESTITASKBARLIST_DIALOG };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CTestITaskbarListDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	LPITaskbarList pTaskbar;

	// Generated message map functions
	//{{AFX_MSG(CTestITaskbarListDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnButton1();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TESTITASKBARLISTDLG_H__)


- update the TestITaskbarListDlg.cpp file:

CTestITaskbarListDlg::CTestITaskbarListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestITaskbarListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestITaskbarListDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon  = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	pTaskbar = NULL;
}

...
...

BEGIN_MESSAGE_MAP(CTestITaskbarListDlg, CDialog)
	//{{AFX_MSG_MAP(CTestITaskbarListDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

...
...

BOOL CTestITaskbarListDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	HRESULT hRes = ::CoInitialize(0);
	if ( hRes != S_OK )
	{
		::AfxMessageBox("*** Error: Problem to initialize the COM library !");
		::PostQuitMessage(0);
	}

	//We call below function since we only need to create one object
	::CoCreateInstance(CLSID_TaskbarList, 0, CLSCTX_SERVER, IID_ITaskbarList, (void**)&pTaskbar);
	if ( hRes != S_OK )
	{
		::AfxMessageBox("*** Error: Problem to create a single instance of the task bar list !");
		::PostQuitMessage(0);
	}

	//Below function will initialize the taskbar list object
	hRes = pTaskbar->HrInit();
	if ( hRes != S_OK )
	{
		::AfxMessageBox("*** Error: Problem to initialize the COM instance !");
		::PostQuitMessage(0);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

...
...

void CTestITaskbarListDlg::OnButton1() 
{
	if ( NOERROR != pTaskbar->DeleteTab(m_hWnd) )
	{
		TRACE("*** Error: Problem to delete tab from the Task bar !!!");
	}
}



Click the button. The tab disappears.

Hope it can help.
GeneralMy vote of 1 Pin
Elchay2-Feb-09 11:55
Elchay2-Feb-09 11:55 
QuestionTaskbar button comes back Pin
Pietju20-Sep-08 5:33
Pietju20-Sep-08 5:33 
QuestionAny idea how to change the text of a taskbar item Pin
Pit M.23-Dec-05 1:57
Pit M.23-Dec-05 1:57 
GeneralThis method might be simpler. Pin
Anonymous15-Sep-05 18:18
Anonymous15-Sep-05 18:18 
GeneralRe: This method might be simpler. Pin
aquawicket12-Jul-07 16:54
aquawicket12-Jul-07 16:54 
Questionhow do i hide a program/window/taskbar/icon Pin
Anonymous28-Mar-05 5:57
Anonymous28-Mar-05 5:57 
AnswerRe: how do i hide a program/window/taskbar/icon Pin
Milind Shingade6-Mar-06 18:44
Milind Shingade6-Mar-06 18:44 
Questionsample code ? Pin
riki_risnandar23-Nov-04 22:07
riki_risnandar23-Nov-04 22:07 
GeneralWhere place &quot;pTaskbar-&gt;DeleteTab(...)&quot; Pin
pagaia15-Sep-04 5:01
pagaia15-Sep-04 5:01 
GeneralSuggestion. Pin
WREY17-Apr-04 9:30
WREY17-Apr-04 9:30 
GeneralRe: Suggestion. Pin
Aris Adrianto S17-Apr-04 10:38
Aris Adrianto S17-Apr-04 10:38 
General"annoying box in the taskbar" Pin
dog_spawn17-Apr-04 11:09
dog_spawn17-Apr-04 11:09 
GeneralRe: &quot;annoying box in the taskbar&quot; Pin
Aris Adrianto S17-Apr-04 11:42
Aris Adrianto S17-Apr-04 11:42 
GeneralRe: &quot;annoying box in the taskbar&quot; Pin
hcihlen18-Nov-04 6:02
hcihlen18-Nov-04 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.