Solved

Open file dialog box

Posted on 2001-08-21
8
596 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
Comment Utility
Can you show your code?
0
 
LVL 1

Accepted Solution

by:
tvanceplus earned 50 total points
Comment Utility
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
Comment Utility
?
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
??
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now