Question

Such a newbie question I know but I can't seem to find any answers online. Basically I am using the CFile Dialog and not sure if I should put it in the .cpp file or the header file. Thanks in advance.

CFileDialog( BOOL bOpenFileDialog, 
             LPCTSTR lpszDefExt = NULL, 
             LPCTSTR lpszFileName = NULL, 
             DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
             LPCTSTR lpszFilter = NULL, 
             CWnd* pParentWnd = NULL ); 

edit by ChrisBD

Okay, so I have added the includes to my FileDialogDlg.cpp and added the code:

CFileDialog fileDlg( TRUE, 
                     NULL, 
                     NULL, 
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, 
                     "All Files (.)|*.*||", 
                     this); 

// Initializes m_ofn structure 
fileDlg.m_ofn.lpstrTitle = "My File Dialog"; 

// Call DoModal 
if ( fileDlg.DoModal() == IDOK) 
{ 
    CString szlstfile = fileDlg.GetPathName(); // This is your selected file 
                                               // name with path

    AfxMessageBox("Your file name is :" +szlstfile ); 
} 

My compiler is still showing a load of errors

Was it helpful?

Solution

My bet regarding the "cannot convert parameter 5 from ..." error is that you compile your app as Unicode (which is a good thing). You must then use Unicode-aware string literals in your code for string parameters:

CFileDialog fileDlg( TRUE,  
                     NULL,  
                     NULL,  
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY,  
                     L"All Files (.)|*.*||", // <-- I Added the leading L  
                     this);  

You could also decide to make it both ANSI/Unicode compatible using the TEXT() macro or its _T() shortcut.

CFileDialog fileDlg( TRUE,  
                     NULL,  
                     NULL,  
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY,  
                     _T("All Files (.)|*.*||"), // <-- _T("blah")
                     this);  

OTHER TIPS

The answer is neither - the CFileDialog class is already declared for you in afxdlgs.h (according to the CFileDialog documentation), so just:

#include <afxdlgs.h>

Then you can use CFileDialog in your code.

I would suggest that you create a new instance locally, set its properties and then open it modally. For example:

// Create an Open dialog; the default file name extension is ".txt".
   CFileDialog fileDlg (TRUE, "txt", "*.txt", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

   // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
   // returns IDOK.
   if( fileDlg.DoModal ()==IDOK )
   {
      CString pathName = fileDlg.GetPathName();

      // Implement opening and reading file in here.
      ...
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top