Adding Toolbar button in CFileDialog

This article will show you how to add a Toolbar button in CFileDialog's Toolbar. Here is the sample code:

CONST LONG ID_NEW_BUTTON = 40790L;  // Command ID of new 
                                    // Toolbar button.

class CFileDialogEx : public CFileDialog

     CFileDialogEx(BOOL bOpenFileDialog, // TRUE for FileOpen, 
                                         // FALSE for FileSaveAs
     LPCTSTR lpszDefExt = NULL,
     LPCTSTR lpszFileName = NULL,
                     LPCTSTR lpszFilter = NULL,
                     CWnd* pParentWnd = NULL);

     virtual void OnInitDone();
     void OnNewTBClick();

     static LRESULT CALLBACK WindowProcNew(HWND hwnd, 
                                           UINT message,
                                           WPARAM wParam,
                                           LPARAM lParam);

     static WNDPROC m_wndProc;

WNDPROC CFileDialogEx::m_wndProc;


CFileDialogEx:: CFileDialogEx (BOOL bOpenFileDialog, 
                               LPCTSTR lpszDefExt, 
                               LPCTSTR lpszFileName,
                               DWORD dwFlags, 
                               LPCTSTR lpszFilter,
                               pParentWnd) :


void CFileDialogEx::OnInitDone()
   CWnd* wndParent = GetParent();
   ASSERT(wndParent != NULL);

   // Display File dialog in center

   // Implementation of Toolbar in file dialog is 
   // in class "ToolbarWindow32". We get the toolbar 
   // by looping through all CFileDialog child 
   // controls and getting their class name.

   TCHAR strClassName[255];
   CWnd* wndChild = wndParent->GetWindow(GW_CHILD);
   while( wndChild != NULL )
      // Get the class name of child control
      ::GetClassName(wndChild->GetSafeHwnd(), strClassName, 255);

      // if child control is toolbar then we add a button in it.

      if( _tcscmp(_T("ToolbarWindow32"), strClassName) == 0 )
         CToolBarCtrl* pToolbarCtrl = (CToolBarCtrl*)wndChild;
         ASSERT(pToolbarCtrl != NULL);

         // Get the number of buttons in existing 
         // toolbar control.
         int nButCount = pToolbarCtrl->GetButtonCount();

         // Add a new button in it. 
         IDB_NEW_BUTTON is a bitmap resource
         pToolbarCtrl->AddBitmap(1, IDB_NEW_BUTTON);

         // get the number of images in existing toolbar
         int nImageCount = 

         // Define a new button
         TBBUTTON tb;
         // Index of new button image.
         tb.iBitmap = nImageCount-1;

         // Command associated with toolbar button
         tb.idCommand = ID_NEW_BUTTON;

         // Setting button state
         tb.fsState = TBSTATE_ENABLED;

         // Setting button style
         tb.fsStyle = TBSTYLE_BUTTON;
         tb.dwData = 0;
         tb.iString = NULL;

         // Insert it in existing toolbar control
         pToolbarCtrl->InsertButton(nButCount, &tb);

      wndChild = wndChild->GetNextWindow();

   // setup parent window procedure to capture the 
   // mouse click event of Toolbar button//
   m_wndProc = (WNDPROC)SetWindowLong(wndParent->GetSafeHwnd(),

static CFileDialogEx* GetFileDlg (HWND hwdParent)
   CFileDialog* pDlg = 
         (CFileDialog*)CWnd::FromHandle (hwdParent);
   ASSERT (pDlg != NULL);

   CFileDialogEx* pFD = (CFileDialogEx*) pDlg->GetDlgItem(0);
   ASSERT (pFD != NULL);

   return pFD;

LRESULT CALLBACK CFileDialogEx::WindowProcNew(HWND hwnd,
                                              UINT message,
                                              WPARAM wParam,
                                              LPARAM lParam)
      case WM_COMMAND:
        if ( (LONG) LOWORD(wParam) == ID_NEW_BUTTON )
            CFileDialogEx * pFD =  GetFileDlg(hwnd);
            pFD->OnNewTBClick ();
            return 0;

     return CallWindowProc(CFileDialogEx::m_wndProc, 

void CFileDialogEx::OnNewTBClick ()
   AfxMessageBox("You clicked on new Toolbar button");


Download demo project - 20 Kb
Download source - 3 Kb


  • How can I add a button in toolbar with a dynamic method ?

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: rongrong

    How can I add a button in toolbar with a dynamic method ?
    Here I want to write a dll, after exe file call the dll , the exe's toolbar can be added a button with a dynamic methoe.

    Please help me

  • Very nice!

    Posted by Legacy on 03/21/2003 12:00am

    Originally posted by: Katerina

    Can you, please, help me about this: How to create a toolbar in Excel with C++ Builder? 
    Thank you in advance.

  • SUPERB !

    Posted by Legacy on 03/09/2003 12:00am

    Originally posted by: Yair Konfino

    great sample

  • Wonderful solution

    Posted by Legacy on 10/31/2002 12:00am

    Originally posted by: Cline Yuan

    Thank you for A-OK solution.

  • Adding Toolbar button in CFileDialog

    Posted by Legacy on 10/09/2001 12:00am

    Originally posted by: Muhammad Bilal Khurshid

    Its an excellent effort bt Mr. Mustafa. I cogratulate him on
    his efforts.

  • Classname showing AfxControlBar42 instead of ToolbarWindow32

    Posted by Legacy on 10/08/2001 12:00am

    Originally posted by: Ashley Pinto

    I need to add a toolbar (and/or) button to Ms Outlook and I was trying pieces of your code in a sample app using VC++ 6.0. The class name being returned is AfxControlBar42, with the wndChild as CDockBar. The sample app was a Wizard generated SDI application.
    I do know that MFC and Office both have different classnames for the Toolbar.
    How do I reuse your code.
    I replaced,
    if( _tcscmp(_T("ToolbarWindow32"), strClassName) == 0 )
    if( _tcscmp(_T("AfxControlBar42d"), strClassName) == 0 )

    but I'm not able to make any headway, with it.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date