Funnotes Logo
Home Sai Satcharitra Talapatram

Creating a File Open Dialog Box using MFCs

[BACK]

ENVIRONMENT VS .NET 2003

A file open dialog box in an application is a basic need of all programmers. This tutorial gives you a quick glance at how to create a File Open Dialog Box using MFC and how to get selected file name.

MFC's CFileDialog encapsulates the Windows common file dialog box which can be used to open a file or save a file.

Steps in using CFileDialog

  • Create an instance of CFileDialog by its constructor
  • Set or modify m_pOFN structure values
  • Call DoModal to display the dialog

Sample Code:

The code below is a snippet of my code where I load a text file containing some data and use it for my purposes in my program. OnBnClickedLoadFile() is an afx_msg function declared in MyDialog class

Which is inherited from CDialog. OnBnClickedLoadFile() is called when user clicks a button in the interface that a programmer provides. For example a button in a dialog box.

void MyDialog::OnBnClickedLoadFile()
{
	CFileDialog fOpenDlg(TRUE, "txt", "vicon_cams_data", OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, 
	"Camera Data Files (*.txt)|*.txt|*.dat||", this);
  
	fOpenDlg.m_pOFN->lpstrTitle="Camera Data File";
  
	fOpenDlg.m_pOFN->lpstrInitialDir="c:";
  
	if(fOpenDlg.DoModal()==IDOK)
	{
		if(!ViconCamera::Instance()->LoadCamerasData((LPCTSTR)fOpenDlg.GetPathName()))
    		{
			::MessageBox(NULL, "File Not Found or Invalid File Or No Of Cameras Zero", 
			"File Not Found", MB_OK);
       			return;
		}
		// Do something useful here
	}
}
[BACK][TOP]

I give you a generic example.

Write the below code on any GUI button's event handler or menu's event handler where you want to call file open dialog box.

// Create an instance First
CFileDialog fOpenDlg(TRUE, "txt", "vicon_cams_data", OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "Camera Data Files (*.txt)|*.txt|*.dat||", this);

// Initializes m_pOFN structure
fOpenDlg.m_pOFN->lpstrTitle="Camera Data File";

// Call DoModal

if(fOpenDlg.DoModal()==IDOK)
{
	if(!ViconCamera::Instance()->LoadCamerasData((LPCTSTR)fOpenDlg.GetPathName()))
	// LoadCamerasData(char *) returns true if file is valid and is opened and false otherwise
	{
		::MessageBox(NULL, "File Not Found or Invalid File Or No Of Cameras Zero", "File Not
	       	Found", MB_OK);
		return;
	}
	// Do something useful here
}

Here is the explanation for the constructor options of CFileDialog class

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

bOpenFileDialog : Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box.

lpszDefExt : The default filename extension. If the user does not include an extension in the Filename edit box, the extension specified by lpszDefExt is automatically appended to the filename. If this parameter is NULL, no file extension is appended.

lpszFileName : The initial filename that appears in the filename edit box. If NULL, no filename initially appears.

dwFlags : A combination of one or more flags that allow you to customize the dialog box. For a description of these flags, see the OPENFILENAME structure in the Win32 SDK documentation. If you modify the m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.

lpszFilter :A series of string pairs that specify filters you can apply to the file. If you specify file filters, only selected files will appear in the Files list box. See the Remarks section for more information on how to work with file filters.

pParentWnd :A pointer to the file dialog-box object's parent window.

Refer to MSDN for further help.

Getting a File Extension

What if you want to browse only certain types of files. Say files only with extension .bmp.

CFileDialog bitmapDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "Bitmap Files(*.bmp)|*.bmp||",this);

CFileDialog In Detail

Let's see what else CFileDialog can do for us. CFileDialog's constructor creates an instance of CFileDialog. After creating an instance, you can set or modify values in the m_pOFN structure. After initialization, you can DoModal to display the dialog.

You use CFileDialog constructor to create an instance of the CFileDialog.

[BACK][TOP]

Here are some more useful functions:

DoModal Displays the dialog box and allows the user to make a selection.

GetPathName Returns the full path of the selected file. The path of the filename includes the file's title plus the entire directory path. For example, GetPathName will return "C:\FILES\TEXT.DAT" for the file C:\FILES\TEXT.DAT.

GetFileName Returns the filename of the selected file. The name of the file includes both the prefix and the extension. For example, GetFileName will return "TEXT.DAT" for the file C:\FILES\TEXT.DAT.

GetFileExt Returns the file extension of the selected file. If the name of the file entered is DATA.TXT, GetFileExt returns "TXT".

GetFileTitle Returns the title of the selected file. The title of the file includes only its prefix, without the path or the extension. For example, GetFileTitle will return "TEXT" for the file C:\FILES\TEXT.DAT.

GetNextPathName Returns the full path of the next selected file. The path of the filename includes the file's title plus the entire directory path. For example, GetNextPathName will return "C:\FILES\TEXT.DAT" for the file C:\FILES\TEXT.DAT. You can use GetNextPathName in a forward iteration loop if you establish the initial position with a call to GetStartPosition.

GetReadOnlyPref Returns the read-only status of the selected file.

GetStartPosition Returns the position of the first element of the filename list.

m_pOFN (in VS .NET 2003) is a structure of type LPOPENFILENAME. Use this structure to initialize the appearance of a File Open or File Save As dialog box after it is constructed but before it is displayed with the DoModal member function.

NOTE: See OPENFILENAME structure in MSDN for more details about settings.

[BACK][TOP]

Include File

The CFileDialog class is defined in <afxdlgs.h> header file and implementation is in COMMDLG.DLL. Include the line #include <afxdlgs.h> in the stdafx.h file

V Rama Aravind,
11-07-04.

Reference: MindCracker.com


© 2003 - 2023, Rama Aravind Vorray, Inc. Site Last Updated: 2023-04-08. Contact Me