1

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

StevieG
  • 8,639
  • 23
  • 31
bigbaz34
  • 385
  • 2
  • 8
  • 27
  • Can you post up the errors that you're getting and check that I've added your code correctly into your question please. – ChrisBD Oct 25 '11 at 08:54
  • Ah I posted the errors once but they seemed to dissapear. error C2664: 'CFileDialog::CFileDialgo(BOOL,LPCTSTR,LPCTSTR,DWORD,LPCTSTR,CWnd*,DWORD)': cannot convert parameter 5 from 'const char [20]' to 'LPCTSTR' the other is error C2440: '=': cannot convert from 'const char '[15]' to 'LPCWSTR' – bigbaz34 Oct 25 '11 at 08:59
  • 1
    You can't add a CString to a char array like that. You need to build your string for the AfxMessageBox. I'd suggest using ostringstream.. – StevieG Oct 25 '11 at 09:01

3 Answers3

2

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);  
Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
1

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 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) { My compiler is still showing a load of errors. 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. – bigbaz34 Oct 25 '11 at 08:41
  • We can't properly help you unless you show (a) your code, and (b) the errors from your compiler. – Greg Hewgill Oct 25 '11 at 08:43
  • Okay I am getting three errors. error C2678: binary '+': no operator found which takes a left-hand operand of type 'con char [20]' (or there is no acceptable conversion) error C2664: 'CFileDialog(Bool,LPCTSTR,LPCSTR,DWORD,LPCSTR,CWnd*,DWORD)':cannot convert parameter 5 from 'const char [22]' and error C2240 '=': cannot convert from 'const char [15] to 'LPCWSTR'. The only code I have enetered is the code in the above post. – bigbaz34 Oct 25 '11 at 08:48
  • It looks like you are compiling in Unicode mode. Use `_T("All Files...")` and use `TCHAR` wherever you have used `char`. – Greg Hewgill Oct 25 '11 at 08:55
  • I have done as said and it is coming up with error C2440 still as well as C2143, C2065 and C2059. – bigbaz34 Oct 25 '11 at 09:03
  • Sorry, you're not being forthcoming with enough actual information. I'm afraid I can't help you without a lot more detail. – Greg Hewgill Oct 25 '11 at 09:06
  • Basically all I want to do is just create a very basic open file dialog window. All I have done so far is openened Visual Studio 2005 and created an MFC dialog application. I have added a button to the dialog and created an event handler. Underneath the event handler I added the code above. I'm sure I am over complicating things, sorry about that. If there is any other information you need please let me know. – bigbaz34 Oct 25 '11 at 09:12
1

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.
      ...
   }
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • Sorry to be a pain in the backside but what do you mean locally? Is this my normal .cpp or header file or my dialog .cpp or .h file. Thanks. – bigbaz34 Oct 25 '11 at 08:51
  • 1
    Variables and instances declared and created within a function or procedure, prior to use are said to be declared/created locally. They are only in scope whilst that function is being processed. – ChrisBD Oct 25 '11 at 09:23