?
Solved

Open file dialog box

Posted on 2001-08-21
8
Medium Priority
?
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

770 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