How to Show a Custom Common Dialog using CFileDialog (102332)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft C/C++ for MS-DOS 7.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1

This article was previously published under Q102332
If an application specifies a custom dialog box template for a CFileDialog object and calls the DoModal() member function, DoModal() may return immediately without displaying the dialog box.

Verify that you assign the correct dialog box template and application instance handle to the CFileDialog data members m_ofn.lpTemplateName and m_ofn.hInstance, respectively. Verify that each control in your customized dialog box template is valid and that the dialog box resource is compiled into the .RES file. If either data member is incorrect or if it is unable to create a control in the dialog box, the DoModal() member function returns immediately without displaying the dialog box.

The Microsoft Foundation Classes library defines a CFileDialog class that encapsulates the OPENFILENAME structure and the GetOpenFileName() and GetSaveFileName() functions of the Microsoft Windows application programming interface (API). For more information about this class, please refer to Technical Note 13 (TN013.TXT) which the C/C++ version 7.0 Setup program installs, by default, into the C:\C700\MFC\DOC directory. For more information about the Common Dialog Boxes library COMMDLG.DLL, please refer to Chapter 4 of the Microsoft Windows Software Development Kit (SDK) "Programmer's Reference, Volume 4: Resources" manual for version 3.1.

To modify the dialog box that CFileDialog displays, perform the following five steps:

  1. Modify the dialog box template for the CFileDialog dialog box by changing the FILEOPEN.DLG resource. By default, this file is located in the C:\MSVC\SAMPLES\COMMDLG or C:\WINDEV\SAMPLES\COMMDLG directory. Be sure to include the <DLGS.H> file in your RESOURCE.H file and use the predefined controls in the common dialog box template.
  2. Initialize the CFileDialog with the OFN_ENABLETEMPLATE flag, as follows:
          CFileDialog fileDialog(TRUE, szFileDialogExt, NULL,
             OFN_HIDEREADONLY | OFN_ENABLETEMPLATE, szFileDialogFilter);
    				
  3. Assign the m_ofn.lpTemplateName member of the CFileDialog object to the dialog box template name in the FILEOPEN.DLG file modified in Step 1 above. For example:
          fileDialog.m_ofn.lpTemplateName = "MYFILEOPEN";
    				
  4. Assign the m_ofn.hInstance member of the CFileDialog object to the value of the current instance of your application, as follows:
          fileDialog.m_ofn.hInstance = AfxGetInstanceHandle();
    				
  5. Call the DoModal() member function of the CFileDialog class, as follows:
          if (fileDialog.DoModal() == IDOK)
             ReadFile(fileDialog.GetPathName());

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:KB102332 kbAudDeveloper