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”);
}