Solved

Open file dialog box

Posted on 2001-08-21
8
619 Views
Last Modified: 2013-11-20
Hello,
I'm just a beginner in MFC and have this problem:
I'm trying to open an open file dialog box with multiple file selection, but for some reason the File Title, File Name fields of the m_ofn structure of the CFileDialog class contains the path and not the file names.
Why?

(I'm working on NT4 using microsoft visual c++ 6)

thanx
qar
0
Comment
Question by:qar
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6410260
Can you show your code?
0
 
LVL 1

Accepted Solution

by:
tvanceplus earned 50 total points
ID: 6410278
Here is code that will take a CFileDialog object and read in multiple file selections into a CArray<CString,CString&>.

The array fileNameList will contain the filenames and extensions and the array pathList contains all of the full paths.

In the OPENFILENAME structure the lpstrFileName and lpstrFileTitle fields are only for single select Dialogs.

CArray<CString,CString&> fileNameList;
CArray<CString,CString&> pathList;

CArray<CString,CString&> fileList;
CFileDialog fDlg(TRUE); //open dialog

CString ext;

int lcv;

if(fileDlg.DoModal()==IDOK){

  int place=0;
  int size=0;
  int len;

  LPTSTR string=fDlg.m_ofn.lpstrFile;

  while(string[place]!='\0'){
               
    fileList.SetSize(size+1);

    while(string[place]!='\0'){

      fileList[size]+=string[place];
      ++place;

    }
    ++place;
    ++size;
  }

  len=fileList.GetSize();

  if(len>1){
    for(lcv=1;lcv<len;++lcv){

      ext=fileList[lcv].Right(3);
      ext.MakeUpper();

      if(ext!="EST" && ext!="MAS"){
        fileList.RemoveAt(lcv);
     --lcv;
     --len;
      }
    }

    len=fileList.GetSize();

    if(len<=1) return;

    if(fileList[0][fileList[0].GetLength()-1]!='\\'){
      fileList[0]+='\\';
    }

    for(lcv=1;lcv<len;++lcv){
      fileNameList.Add(fileList[lcv]);
      pathList.Add(fileList[0]+fileList[lcv]);
    }
  }

  else{
    ext=fileDlg.GetFileExt();

    ext.MakeUpper();

    if(ext=="MAS" || ext=="EST"){
      fileNameList.Add(fileDlg.GetFileName());
      pathList.Add(fileDlg.GetPathName());
    }
  }
}
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6410282
?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 32

Expert Comment

by:jhance
ID: 6410293
??
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6410300
qar: here is a class wrapper for multi-selection file dialog.


//header file
#if !defined(AFX_MFILEDLG_H__2268B980_C8BF_4084_98B1_3D162B8FA54B__INCLUDED_)
#define AFX_MFILEDLG_H__2268B980_C8BF_4084_98B1_3D162B8FA54B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MFileDlg.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMFileDlg dialog

class CMFileDlg : public CFileDialog
{
     DECLARE_DYNAMIC(CMFileDlg)

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

     int DoModal();
     virtual ~CMFileDlg();

protected:
     //{{AFX_MSG(CMFileDlg)
     // NOTE - the ClassWizard will add and remove member functions here.
     //}}AFX_MSG

     TCHAR* m_pszFile;
     DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MFILEDLG_H__2268B980_C8BF_4084_98B1_3D162B8FA54B__INCLUDED_)



//cpp file
// MFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "spectraanalisysgui.h"
#include "MFileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMFileDlg
IMPLEMENT_DYNAMIC(CMFileDlg, CFileDialog)

CMFileDlg::CMFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
     DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
     CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
     m_pszFile = new TCHAR[2048]; //set a 2K buffer to hold selected files
     m_pszFile[0] = '\0'; //initialize pointer;
}

BEGIN_MESSAGE_MAP(CMFileDlg, CFileDialog)
//{{AFX_MSG_MAP(CMFileDlg)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMFileDlg::~CMFileDlg()
{
     if (m_pszFile != NULL)
     delete [] m_pszFile; //cleanup
}

int CMFileDlg::DoModal()
{
     ASSERT_VALID(this);
     ASSERT(m_ofn.Flags & OFN_ALLOWMULTISELECT); //make sure multiple file selection is on

     m_ofn.lpstrFile = m_pszFile; //initialize the OPENFILENAME structure
     m_ofn.nMaxFile = 2048;

     return CFileDialog::DoModal();
}




an example usage of this class:    

CMFileDlg dlgFile(TRUE);
     CString strPath = GetDocument()->GetPathName();
     int start = strPath.ReverseFind('\\') + 1;
     CString strDir( strPath.Left(start) );
     dlgFile.m_ofn.lpstrTitle = strTitle;
     dlgFile.m_ofn.lpstrInitialDir = strDir;
     dlgFile.m_ofn.lpstrDefExt = "bmp";
     dlgFile.m_ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0Gif Files (*.gif)\0*.gif\0Jpeg Files (*.jpeg)\0*.jpeg\0All Files (*.*)\0*.*\0\0";
     dlgFile.m_ofn.hwndOwner = AfxGetMainWnd()->GetSafeHwnd(); //in case of MDI/SDI application only

     if ( dlgFile.DoModal() != IDOK )
          return;


cheers

0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 6410472
Sorry, some of that stuff

if(ext!="MAS" && ext!="EST")

didn't belong. I used that for my code, but I didn't mean to copy it in. The general idea is still right though. Simply change the "MAS" or "EST" to be whatever you filtered the dialog for.
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6412359
gar,

you may reject tvanceplus's answer and post your code so we can tell you what's wrong with your code.

Pacman
0
 

Author Comment

by:qar
ID: 6415402
Thanx, tvanceplus
Your code works perfectly.
After looking in your code I realized how the file names are stored in the buffer (that's the mistake in my code).

qar
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 136
sumDigits challenge 9 147
lucky13 challenge 11 142
canBalance challenge 34 81
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question