How to show/hide a button on the taskbar?

I have a Dialog based application.  I made a System Tray Icon and I am now trying to remove its icon from the taskbar.

I can do this for a SDI Application, but it dosn't work for a Dialog based application.

Please help!!
stormpAsked:
Who is Participating?
 
sirrewobConnect With a Mentor Commented:
See above
0
 
WhaleCommented:
override the DestroyWindow
BOOL CDlgYourApp::DestroyWindow()
{
              NOTIFYICONDATA tnid;
      tnid.cbSize = sizeof(NOTIFYICONDATA);
      tnid.hWnd = AfxGetApp()->m_pMainWnd->m_hWnd;
      tnid.uID = ID_ICON_SYSTRAY;
      Shell_NotifyIcon(NIM_DELETE, &tnid);

      return CDialog::DestroyWindow();
}

0
 
sirrewobCommented:
Not sure want to you want to do?

If its removing the icon from the system tray then the example provided by Whale is correct, but if its to remove the applications button from the taskbar then thats a different matter.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
stormpAuthor Commented:
> Not sure want to you want to do?
> If its removing the icon from the system tray then the example provided by Whale
> is correct, but if its to remove the applications button from the taskbar then thats
> a different matter.

I need to remove the applications button from the taskbar and still have the window open on the desktop.

0
 
sirrewobCommented:
I have the answer for this, but it's a bit difficult and long winded. Not sure if this is the only answer, but I use it and I haven't seen any other solutions.

The method is based on using the ITaskBarList interface, and its corresponding methods, DeleteTab and AddTab. The only problem is that there is no documentation or header files, so you have to create your own.

Please read carfully and i'll try and explain.

1) Add the following to the head of the cpp file that will control the taskbar.
#include "taskbarlist.h"

2) Create taskbarlist.h and add the following.

/* this ALWAYS GENERATED file contains the definitions for the interfaces */


/* File created by MIDL compiler version 5.01.0164 */
/* at Wed Nov 11 14:48:42 1998
 */
/* Compiler settings
    Os (OptLev=s), W1, Zp8, env=Win32, ms_ext, c_ext
    error checks: allocation ref bounds_check enum stub_data
*/
//@@MIDL_FILE_HEADING(  )


/* verify that the <rpcndr.h> version is high enough to compile this file*/
#ifndef __REQUIRED_RPCNDR_H_VERSION__
#define __REQUIRED_RPCNDR_H_VERSION__ 440
#endif

#include "rpc.h"
#include "rpcndr.h"

#ifndef __RPCNDR_H_VERSION__
#error this stub requires an updated version of <rpcndr.h>
#endif // __RPCNDR_H_VERSION__

#ifndef COM_NO_WINDOWS_H
#include "windows.h"
#include "ole2.h"
#endif /*COM_NO_WINDOWS_H*/

#ifndef __ITaskbarList_h__
#define __ITaskbarList_h__

#ifdef __cplusplus
extern "C"{
#endif

/* Forward Declarations */

#ifndef __ITaskbarList_FWD_DEFINED__
#define __ITaskbarList_FWD_DEFINED__
typedef interface ITaskbarList ITaskbarList;
#endif       /* __ITaskbarList_FWD_DEFINED__ */


/* header files for imported files */
#include "oaidl.h"
#include "oleidl.h"

void __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t);
void __RPC_USER MIDL_user_free( void __RPC_FAR * );

#ifndef __ITaskbarList_INTERFACE_DEFINED__
#define __ITaskbarList_INTERFACE_DEFINED__

/* interface ITaskbarList */
/* [unique][uuid][object][local] */


EXTERN_C const IID IID_ITaskbarList;

#if defined(__cplusplus) && !defined(CINTERFACE)
   
    MIDL_INTERFACE("BD4FAF8A-E047-11D2-9BC0-0004ACB296D8")
    ITaskbarList : public IUnknown
    {
    public:
        virtual HRESULT STDMETHODCALLTYPE ActivateTab(
            /* [in] */ HWND hWnd) = 0;
       
        virtual HRESULT STDMETHODCALLTYPE AddTab(
            /* [in] */ HWND hWnd) = 0;
       
        virtual HRESULT STDMETHODCALLTYPE DeleteTab(
            /* [in] */ HWND hWnd) = 0;
       
        virtual HRESULT STDMETHODCALLTYPE HrInit( void) = 0;
       
        virtual HRESULT STDMETHODCALLTYPE SetActiveAlt(
            /* [in] */ HWND hWnd) = 0;
       
    };
   
#else       /* C style interface */

    typedef struct ITaskbarListVtbl
    {
        BEGIN_INTERFACE
       
        HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )(
            ITaskbarList __RPC_FAR * This,
            /* [in] */ REFIID riid,
            /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
       
        ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )(
            ITaskbarList __RPC_FAR * This);
       
        ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )(
            ITaskbarList __RPC_FAR * This);
       
        HRESULT ( STDMETHODCALLTYPE __RPC_FAR *ActivateTab )(
            ITaskbarList __RPC_FAR * This,
            /* [in] */ HWND hWnd);
       
        HRESULT ( STDMETHODCALLTYPE __RPC_FAR *AddTab )(
            ITaskbarList __RPC_FAR * This,
            /* [in] */ HWND hWnd);
       
        HRESULT ( STDMETHODCALLTYPE __RPC_FAR *DeleteTab )(
            ITaskbarList __RPC_FAR * This,
            /* [in] */ HWND hWnd);
       
        HRESULT ( STDMETHODCALLTYPE __RPC_FAR *HrInit )(
            ITaskbarList __RPC_FAR * This);
       
        HRESULT ( STDMETHODCALLTYPE __RPC_FAR *SetActiveAlt )(
            ITaskbarList __RPC_FAR * This,
            /* [in] */ HWND hWnd);
       
        END_INTERFACE
    } ITaskbarListVtbl;

    interface ITaskbarList
    {
        CONST_VTBL struct ITaskbarListVtbl __RPC_FAR *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_ActivateTab(This,hWnd)      \
    (This)->lpVtbl -> ActivateTab(This,hWnd)

#define ITaskbarList_AddTab(This,hWnd)      \
    (This)->lpVtbl -> AddTab(This,hWnd)

#define ITaskbarList_DeleteTab(This,hWnd)      \
    (This)->lpVtbl -> DeleteTab(This,hWnd)

#define ITaskbarList_HrInit(This)      \
    (This)->lpVtbl -> HrInit(This)

#define ITaskbarList_SetActiveAlt(This,hWnd)      \
    (This)->lpVtbl -> SetActiveAlt(This,hWnd)

#endif /* COBJMACROS */


#endif       /* C style interface */



HRESULT STDMETHODCALLTYPE ITaskbarList_ActivateTab_Proxy(
    ITaskbarList __RPC_FAR * This,
    /* [in] */ HWND hWnd);


void __RPC_STUB ITaskbarList_ActivateTab_Stub(
    IRpcStubBuffer *This,
    IRpcChannelBuffer *_pRpcChannelBuffer,
    PRPC_MESSAGE _pRpcMessage,
    DWORD *_pdwStubPhase);


HRESULT STDMETHODCALLTYPE ITaskbarList_AddTab_Proxy(
    ITaskbarList __RPC_FAR * 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 __RPC_FAR * This,
    /* [in] */ HWND hWnd);


void __RPC_STUB ITaskbarList_DeleteTab_Stub(
    IRpcStubBuffer *This,
    IRpcChannelBuffer *_pRpcChannelBuffer,
    PRPC_MESSAGE _pRpcMessage,
    DWORD *_pdwStubPhase);


HRESULT STDMETHODCALLTYPE ITaskbarList_HrInit_Proxy(
    ITaskbarList __RPC_FAR * This);


void __RPC_STUB ITaskbarList_HrInit_Stub(
    IRpcStubBuffer *This,
    IRpcChannelBuffer *_pRpcChannelBuffer,
    PRPC_MESSAGE _pRpcMessage,
    DWORD *_pdwStubPhase);


HRESULT STDMETHODCALLTYPE ITaskbarList_SetActiveAlt_Proxy(
    ITaskbarList __RPC_FAR * This,
    /* [in] */ HWND hWnd);


void __RPC_STUB ITaskbarList_SetActiveAlt_Stub(
    IRpcStubBuffer *This,
    IRpcChannelBuffer *_pRpcChannelBuffer,
    PRPC_MESSAGE _pRpcMessage,
    DWORD *_pdwStubPhase);



#endif       /* __ITaskbarList_INTERFACE_DEFINED__ */


_COM_SMARTPTR_TYPEDEF(ITaskbarList, __uuidof(ITaskbarList));

/* Additional Prototypes for ALL interfaces */

/* end of Additional Prototypes */

#ifdef __cplusplus
}
#endif

#endif

3) Create a RGS file called TaskBarList.rgs and add the following.

HKCR
{
      FTPSubUI.TaskBarList.1 = s 'TaskBarList Class'
      {
            CLSID = s '{15454908-E047-11D2-9BC0-0004ACB296D8}'
      }
      FTPSubUI.TaskBarList = s 'TaskBarList Class'
      {
            CLSID = s '{15454908-E047-11D2-9BC0-0004ACB296D8}'
            CurVer = s 'FTPSubUI.TaskBarList.1'
      }
      NoRemove CLSID
      {
            ForceRemove {15454908-E047-11D2-9BC0-0004ACB296D8} = s 'TaskBarList Class'
            {
                  ProgID = s 'FTPSubUI.TaskBarList.1'
                  VersionIndependentProgID = s 'FTPSubUI.TaskBarList'
                  ForceRemove 'Programmable'
                  LocalServer32 = s '%MODULE%'
                  val AppID = s '{AF2606D7-A096-11D2-9B6F-0004ACB296D8}'
                  'TypeLib' = s '{AF2606D6-A096-11D2-9B6F-0004ACB296D8}'
            }
      }
}
HKCR
{
      FTPSubUI.TaskBarList.1 = s 'TaskBarList Class'
      {
            CLSID = s '{BD4FAF8B-E047-11D2-9BC0-0004ACB296D8}'
      }
      FTPSubUI.TaskBarList = s 'TaskBarList Class'
      {
            CLSID = s '{BD4FAF8B-E047-11D2-9BC0-0004ACB296D8}'
            CurVer = s 'FTPSubUI.TaskBarList.1'
      }
      NoRemove CLSID
      {
            ForceRemove {BD4FAF8B-E047-11D2-9BC0-0004ACB296D8} = s 'TaskBarList Class'
            {
                  ProgID = s 'FTPSubUI.TaskBarList.1'
                  VersionIndependentProgID = s 'FTPSubUI.TaskBarList'
                  ForceRemove 'Programmable'
                  LocalServer32 = s '%MODULE%'
                  val AppID = s '{AF2606D7-A096-11D2-9B6F-0004ACB296D8}'
                  'TypeLib' = s '{AF2606D6-A096-11D2-9B6F-0004ACB296D8}'
            }
      }
}

4) The above .h and .rgs file provide all the information needed for the ITaskBarList Com object. So all you need to do now is create the object and call a method.

4a) Calling the DeleteTab method.
      ITaskbarList*       pTaskBarList       = 0 ;
      
      // Create TaskBarList object
      CoCreateInstance(      CLSID_TaskbarList,
                  NULL,
                  CLSCTX_SERVER,
                           IID_ITaskbarList,
                  reinterpret_cast<void**>(&pTaskBarList)),
                  ITaskBarList );      
            
// Delete the applications button the taskbar.
pTaskBarList->DeleteTab( m_hWnd ) ;
      
      // Release the taskbarlist interface.
      if( pTaskBarList != 0 )
      {
            pTaskBarList->Release() ;
      }

4b) Adding a button to the taskbar. Just replace the deleteTab with AddTab.
pTaskBarList->AddTab( m_hwnd ) ;


Thats all.
If you have any problems then leave a message, also if it makes you life easier I could send you a full working example.

0
 
stormpAuthor Commented:
Hi, I think you're getting out of my leage.  I thought there was a simple MFC call or two.   I would really appreciate if you can make a simple DIALOGE Based Application that uses that code and send me the project file!

Thanks a lot!
0
 
sirrewobCommented:
Where do you want me to send the project?


0
 
stormpAuthor Commented:
storm@sfu.ca

0
 
stormpAuthor Commented:
Hi sirrewob,

Any idea when you can send the demo project to me?

I appreciate it.

Storm
0
 
sirrewobCommented:
Sorry about not sending you the project, but I've been out of the office this week.

I'll try and get it to you this weekend.

Sirrewob.

0
All Courses

From novice to tech pro — start learning today.