Solved

Open file dialog box

Posted on 2001-08-21
8
632 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 139
substring method in java 1 130
Change to event 1 119
Making an alias 7 101
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.

821 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