Adding Toolbar button in CFileDialog | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 8, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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 classToolbarWindow32”. 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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.