Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / MFC
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
13 Nov 2011CPOL 22.3K   2   4
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:
C++
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)
{
    ...
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Martin Richter [rMVP C++]1-Nov-22 3:58
Martin Richter [rMVP C++]1-Nov-22 3:58 
GeneralThis is just CFileDialog checking that no one has been messi... Pin
.dan.g.16-Nov-11 17:28
professional.dan.g.16-Nov-11 17:28 
Generalstrange---I tried it, and immedeately ran into the following... Pin
Holger B.-J.14-Nov-11 20:57
Holger B.-J.14-Nov-11 20:57 
QuestionRe: ASSERT(m_ofn.Flags & OFN_ENABLEHOOK); Pin
.dan.g.16-Nov-11 17:28
professional.dan.g.16-Nov-11 17:28 
This is just CFileDialog checking that no one has been messing with it, which of course we have Wink | ;) This assert can be safely ignored for now.

Alternatively, cut and paste the code from CFileDialog::DoModal() to CEnFileDialog::DoModal() and omit the call to CFileDialog::DoModal().


.dan.g.

AbstractSpoon Software
abstractspoon2_at_optusnet_dot_com_dot_au


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.