Solved

Open file dialog box

Posted on 2001-08-21
8
642 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 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
Industry Leaders: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 171
maven archtype selection in eclipse 1 59
wait notify demo infinite loop 3 148
How to convert MFC::CString to UTF8 wchar_t* 10 448
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 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