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
{
   DECLARE_DYNAMIC(CFileDialogEx)

  public:
     CFileDialogEx(BOOL bOpenFileDialog, // TRUE for FileOpen, 
                                         // FALSE for FileSaveAs
     LPCTSTR lpszDefExt = NULL,
     LPCTSTR lpszFileName = NULL,
     DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
                     LPCTSTR lpszFilter = NULL,
                     CWnd* pParentWnd = NULL);

  protected:
     virtual void OnInitDone();
     void OnNewTBClick();

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

  public:
     static WNDPROC m_wndProc;
};

WNDPROC CFileDialogEx::m_wndProc;

IMPLEMENT_DYNAMIC(CFileDialogEx, CFileDialog)

CFileDialogEx:: CFileDialogEx (BOOL bOpenFileDialog, 
                               LPCTSTR lpszDefExt, 
                               LPCTSTR lpszFileName,
                               DWORD dwFlags, 
                               LPCTSTR lpszFilter,
                               CWnd* 
                               pParentWnd) :
                               CFileDialog(bOpenFileDialog, 
                                           lpszDefExt, 
                                           lpszFileName,
                                           dwFlags, 
                                           lpszFilter,
                                           pParentWnd)
{

}

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

   // Display File dialog in center
   wndParent->CenterWindow();

   // 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 = 
              pToolbarCtrl->GetImageList()->GetImageCount();

         // 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);
         break;
      }

      wndChild = wndChild->GetNextWindow();
   }

   // setup parent window procedure to capture the 
   // mouse click event of Toolbar button//
   m_wndProc = (WNDPROC)SetWindowLong(wndParent->GetSafeHwnd(),
                            GWL_WNDPROC,
                            (LONG)CFileDialogEx::WindowProcNew);
}

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)
{
   switch(message)
   {
      case WM_COMMAND:
        if ( (LONG) LOWORD(wParam) == ID_NEW_BUTTON )
        {
            CFileDialogEx * pFD =  GetFileDlg(hwnd);
            pFD->OnNewTBClick ();
            return 0;
        }
     }

     return CallWindowProc(CFileDialogEx::m_wndProc, 
                           hwnd,
                           message,
                           wParam,
                           lParam);
}

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

Downloads

Download demo project - 20 Kb
Download source - 3 Kb


Comments

  • 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

    Reply
  • 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.

    Reply
  • SUPERB !

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

    Originally posted by: Yair Konfino

    great sample

    Reply
  • Wonderful solution

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

    Originally posted by: Cline Yuan

    Thank you for A-OK solution.

    Reply
  • 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.

    Reply
  • Classname showing AfxControlBar42 instead of ToolbarWindow32

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

    Originally posted by: Ashley Pinto

    Hi,
    
    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 )
    with
    if( _tcscmp(_T("AfxControlBar42d"), strClassName) == 0 )

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

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

Top White Papers and Webcasts

  • As virtualization becomes the norm throughout organizations of nearly all sizes, and as more organizations look to private cloud solutions, IT decision makers are increasingly in need of ways to keep storage costs and complexity under control in the face of often-runaway virtual machine (VM) sprawl. Application-aware storage is designed to help achieve these important goals. Read this white paper to learn how application-aware storage allows you to gain VM-level visibility into application performance and …

  • The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals: Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals. IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices. Job satisfaction is the No. 1 predictor of performance against organizational …

Most Popular Programming Stories

More for Developers

RSS Feeds

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