Click here to Skip to main content
11,705,123 members (52,492 online)
Click here to Skip to main content

Windows 2000 Common File Dialog

, 16 May 2000 228.2K 2.3K 45
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Keyvan Saneinejad
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionhow to add places bar & new text boxes to the open file dialog Pin
pubba22-Mar-04 19:18
memberpubba22-Mar-04 19:18 
I want to add some extra features to the open file dialog.It can be done by creating a child dialog of the CFiledialog.I have already know to add exra features to the open file dialog. But I don't know how to add places bar to the cutomizable open file dialog.So please any one know these things,send a reply.:
in the open file dialog I want to do 2 major issues.
first is adding extra features like check boxes,buttons etc.
second is adding the "places bar" to the open dialog.
I want to do both of things in the program.
pl send me reply immmediately...................
AnswerRe: how to add places bar & new text boxes to the open file dialog [it works with both OFN_EXPLORER and OFN_ENABLEHOOK] Pin
Marius Razvan padding string2-Aug-04 21:44
memberMarius Razvan padding string2-Aug-04 21:44 
GeneralCFileDialogue crash Pin
ManojP2-Apr-03 17:46
memberManojP2-Apr-03 17:46 
Generalset default directory Pin
Zoltan28-Jan-03 0:29
memberZoltan28-Jan-03 0:29 
GeneralPlaces Bar Customization Pin
rwfst135-Dec-02 4:33
memberrwfst135-Dec-02 4:33 
GeneralRe: Places Bar Customization Pin
JoeB18-Jul-03 15:48
sussJoeB18-Jul-03 15:48 
GeneralSetCurrentDirectory in Windows 2000 Pin
Joe123456789016-Jul-02 13:51
sussJoe123456789016-Jul-02 13:51 
GeneralRe: SetCurrentDirectory in Windows 2000 Pin
shdesigns15-Feb-03 23:58
sussshdesigns15-Feb-03 23:58 
GeneralGreat Job!!! Pin
Anonymous2-May-02 10:16
memberAnonymous2-May-02 10:16 
GeneralCommon Dialog View Mode Pin
Ceri11-Sep-01 4:03
memberCeri11-Sep-01 4:03 
Question2000 style dialogs in ME? Pin
stHASH8-Dec-00 3:23
memberstHASH8-Dec-00 3:23 
GeneralCentering the Dialog Pin
Brian Hart6-Sep-00 14:23
sussBrian Hart6-Sep-00 14:23 
GeneralRe: Centering the Dialog Pin
uli9-Aug-01 7:03
memberuli9-Aug-01 7:03 
GeneralAnother way to do it: I should patent this Pin
Mike20-Jun-00 8:12
sussMike20-Jun-00 8:12 
GeneralRe: Another way to do it: I should patent this Pin
Leon Matthews28-Jun-00 19:51
sussLeon Matthews28-Jun-00 19:51 
GeneralRe: Another way to do it: I should patent this Pin
Kyle Alons4-Jul-00 13:31
sussKyle Alons4-Jul-00 13:31 
GeneralRe: Another way to do it: I should patent this Pin
barto13-Apr-03 2:50
memberbarto13-Apr-03 2:50 
GeneralRe: Another way to do it: I should patent this Pin
Mark Richards23-Aug-00 9:36
sussMark Richards23-Aug-00 9:36 
GeneralAlmost, but a safer approach would be to... Pin
Kirk Stowell27-Jan-01 3:49
memberKirk Stowell27-Jan-01 3:49 
GeneralGreat Works!! Pin
jimmy carter22-Aug-02 10:22
sussjimmy carter22-Aug-02 10:22 
GeneralPaul DiLascia has done it before Pin
KarstenK8-Jul-02 23:14
memberKarstenK8-Jul-02 23:14 
GeneralRe: Paul DiLascia has done it before Pin
Hugo Galindo5-Aug-02 16:55
sussHugo Galindo5-Aug-02 16:55 
General::GetOpenFileName(&ofn); Pin
Lars Beck25-May-00 3:12
sussLars Beck25-May-00 3:12 
GeneralRe: ::GetOpenFileName(&ofn); Pin
Anonymous20-Oct-02 4:50
sussAnonymous20-Oct-02 4:50 
GeneralUpdate Will Block Parent Window Pin
Keyvan Saneinejad23-May-00 15:42
sussKeyvan Saneinejad23-May-00 15:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 17 May 2000
Article Copyright 2000 by Keyvan Saneinejad
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid