65.9K
CodeProject is changing. Read more.
Home

Displaying the 'new' Vista/Windows 7 File Dialog in 'old' MFC Apps

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Nov 7, 2011

CPOL
viewsIcon

23079

Displaying the 'new' Vista/Windows 7 File Dialog in 'old' MFC Apps

As far as I can tell, the only reason for the MFC CFileDialog class not displaying the new Vista/W7 style dialog (with breadcrumbs, etc.) is that it uses a hook for providing the various CFileDialog callbacks (CFileDialog::OnFileNameChange(), etc.). So, if you _don't_ customize the file open dialog, all you need do to enable the new style dialogs is to derive a class from CFileDialog and disable the hook, as follows:
class CMyFileDialog : public CFileDialog
{
   // constructor/destructor
   ...
   
   int DoModal()
   {
        // disable hook funtion
	m_ofn.lpfnHook = NULL;
	m_ofn.Flags &= ~OFN_ENABLEHOOK;

	return CFileDialog::DoModal();
   }
};
And the real gift is that when the dialog returns, you can query the results through your class exactly as if you had used CFileDialog directly! Alternatively, you can simply modify the CFileDialog instance directly:
CFileDialog dialog(...);

// disable hook funtion
dialog.m_ofn.lpfnHook = NULL;
dialog.m_ofn.Flags &= ~OFN_ENABLEHOOK;

if (dialog.DoModal() == IDOK)
{
    ...
}