Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Windows 2000 Common File Dialog

Rate me:
Please Sign up or sign in to vote.
4.44/5 (10 votes)
16 May 2000 257.3K   2.5K   45   46
How to show the new Common File Dialog in MFC Apps in Windows 2000.

Sample Image - win2000fd.jpg

Introduction

If you've tried using the MFC common dialog class CFileDialog in Windows 2000, you'll notice that it will show the 'old' file dialog even with the OFN_EXPLORER flag set. The reason for this is that Windows 2000 uses OFN_ENABLE_HOOK flag to determine which dialog to use. If the OFN_ENABLE_HOOK flag is set, Windows 2000 uses the old dialog, if not, it uses the new dialog. CFileDialog in MFC uses the OFN_ENABLE_HOOK flag which explains why all MFC applications display the old dialog and regular applications (not compiled with MFC) show the new dialog, if run in Windows 2000. For a more detailed explanation, look in April 2000 issue of MSDN pp.138,139. I've managed to get around this by overriding some of MFC's virtual functions and not allowing MFC to set the OFN_ENABLE_HOOK flag.

Step 1:

Insert CKSDialog into your MFC project. Add the following header file to your App's implementation file.

#include "KSDialog.h"

Step 2:

Add the following function headers to you App's header file.

virtual BOOL DoPromptFileName(CString& fileName, 
  UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate);

Then enter the functions body in the your App's implementation file.

BOOL CMyAppApp::DoPromptFileName(CString& fileName, 
  UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
{
  CKSFileDialog dlgFile(bOpenFileDialog);
  dlgFile.SetAppPointer(this);
  dlgFile.SetStringFilter("My File Format (*.mff)|*.mff|My File Format File (*.mff)");
  dlgFile.SetMyParent(AfxGetMainWnd());
  return dlgFile.DoPromptFileName(fileName,nIDSTitle,
      lFlags,bOpenFileDialog, pTemplate);
}

Step 3:

You need to override your App's CMyAppApp::OnFileOpen() function, this can be done easily through the class wizard. Enter the following code for OnFileOpen():

void CMyAppApp::OnFileOpen() 
{
    CString newName;
    if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
      OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL))
        return; // open cancelled

    OpenDocumentFile(newName);
}

Step 4:

Now, you need to override a Windows function in your document class. This will give you the ability to save using the new dialog. So in CMyAppDocument header class, override the DoSave() virtual function.

virtual BOOL DoSave(LPCTSTR lpszPathName, BOOL bReplace=TRUE );

Add the implementation of the function in your Document's implementation file.

BOOL CMyAppDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
{
    CKSFileDialog dlgFile(FALSE);
    dlgFile.SetDocumentPointer(this);
    dlgFile.SetAppPointer(AfxGetApp());
    dlgFile.SetMyParent(AfxGetMainWnd());
    return dlgFile.DoSave(lpszPathName,bReplace);
}

You're done.

Now, when you try to open or save a document, the Windows 2000 file dialog should pop up. See if you can make the dialog pop up in the center of your client window instead of the top left, the first time it's called. I haven't been able to do this as of yet. If I try to hook into the messaging system of CFileDialog to override WM_INITDIALOG, Windows 2000 thinks that it has to show the old style dialog.

If you want the new file dialog to pop up generically, meaning not through the standard Windows Document/View Open, Save, Save As messaging system, just use the following code: (this is also shown in the demo project). You don't have to go through the above code, just use it like a regular CFileDialog.

For saving:

const char szFilters[]="Some Kind of Files (*.fn2)|*.fn2|All Files (*.*)|*.*||";
CKSFileDialog dlg (FALSE, "fn2", NULL, 
  OFN_OVERWRITEPROMPT| OFN_HIDEREADONLY, szFilters);
dlg.SetMyParent(AfxGetMainWnd());

if (dlg.DoModal()==IDOK)
{
    //some file writing code here
}

For reading:

const char szFilters[]="Some Kind of Files (*.fn2)|*.fn2|All Files (*.*)|*.*||";
CKSFileDialog dlg (FALSE, "fn2", NULL, 
  OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters);
dlg.SetMyParent(AfxGetMainWnd());

if (dlg.DoModal()==IDOK)
{
    //some file reading code here
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat Works!! Pin
jimmy carter22-Aug-02 10:22
jimmy carter22-Aug-02 10:22 
GeneralPaul DiLascia has done it before Pin
KarstenK8-Jul-02 23:14
mveKarstenK8-Jul-02 23:14 
GeneralRe: Paul DiLascia has done it before Pin
HugoGalindo5-Aug-02 16:55
HugoGalindo5-Aug-02 16:55 
General::GetOpenFileName(&ofn); Pin
Lars Beck25-May-00 3:12
professionalLars Beck25-May-00 3:12 
GeneralRe: ::GetOpenFileName(&ofn); Pin
Anonymous20-Oct-02 4:50
Anonymous20-Oct-02 4:50 
GeneralUpdate Will Block Parent Window Pin
Keyvan Saneinejad23-May-00 15:42
Keyvan Saneinejad23-May-00 15:42 
GeneralDosent Work On Win95 Pin
+Ari+17-May-00 20:12
+Ari+17-May-00 20:12 
GeneralRe: Dosent Work On Win95 Pin
Keyvan Saneinejad18-May-00 8:43
Keyvan Saneinejad18-May-00 8:43 
Unfortunately, This is by design the dialog will only work on Win2000 and will show the regular file dialog in any other windows operating system.

Keyva
GeneralRead before asking people. Pin
17-Jan-01 2:54
suss17-Jan-01 2:54 
GeneralWindow98 version Pin
James Khan17-May-00 8:06
James Khan17-May-00 8:06 
GeneralRe: Window98 version Pin
Member 7509-Jun-00 11:30
Member 7509-Jun-00 11:30 
GeneralRe: Window98 version Pin
Paolo Messina9-Jun-00 13:55
professionalPaolo Messina9-Jun-00 13:55 
GeneralRe: Window98 version Pin
Jeff25-Jun-00 10:34
Jeff25-Jun-00 10:34 
GeneralThere's one already (95/98/Me/NT 4) Pin
Andy Metcalfe28-Sep-00 0:46
sussAndy Metcalfe28-Sep-00 0:46 
GeneralRe: Window98 version Pin
Reece H. Dunn7-Jul-00 2:49
sussReece H. Dunn7-Jul-00 2:49 
GeneralDoesn't block parent window Pin
street27-Apr-00 10:05
street27-Apr-00 10:05 
GeneralRe: Doesn't block parent window Pin
Keyvan Saneinejad28-Apr-00 7:46
Keyvan Saneinejad28-Apr-00 7:46 
GeneralRe: Doesn't block parent window Pin
Kyle Alons29-Apr-00 11:32
sussKyle Alons29-Apr-00 11:32 
GeneralRe: Doesn't block parent window Pin
Keyvan Saneinejad1-May-00 12:20
Keyvan Saneinejad1-May-00 12:20 
GeneralRe: Doesn't block parent window Pin
Martin Bohring9-May-00 7:15
Martin Bohring9-May-00 7:15 
GeneralRe: Doesn't block parent window Pin
Keyvan Saneinejad9-May-00 7:49
Keyvan Saneinejad9-May-00 7:49 
GeneralFirst time called, the dialog is not in the middle Pin
Andre Muscat23-Mar-00 8:24
sussAndre Muscat23-Mar-00 8:24 
GeneralRe: First time called, the dialog is not in the middle Pin
Keyvan Saneinejad23-Mar-00 11:35
Keyvan Saneinejad23-Mar-00 11:35 
GeneralThanks! Pin
andygood22-Mar-00 17:51
andygood22-Mar-00 17:51 

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.