Microsoft KB Archive/102332

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 10:17, 20 July 2020 by X010 (talk | contribs) (Text replacement - ">" to ">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base

How to Show a Custom Common Dialog using CFileDialog

Article ID: 102332

Article Last Modified on 11/21/2006


  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft C/C++ Professional Development System 7.0
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.51
    • Microsoft Visual C++ 1.52 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 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)

Additional query words: kbinf 7.00 1.00 1.50 2.00 2.50

Keywords: KB102332