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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds