weidagang2046的专栏

物格而后知致
随笔 - 8, 文章 - 409, 评论 - 101, 引用 - 0
数据加载中……

The File Dialog

Overview

Microsoft Windows provides a series of objects called Common Dialog boxes. One of them is intended for file processing. The beauty of these dialog boxes is that they are highly configured to handle their intended job but you can still customize them as you see fit.

The File Dialog box is a control used to open or save a file. To use a File Dialog, you can first declare a CFileDialog variable using its constructor whose syntax is:

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

The CFileDialog constructor requires one argument: the bOpenFileDialog.

The second argument, lpszDefExt, is the default extension that should be used or applied on the file.

The dwFlags parameter specifies the options to use on the dialog box.

The lpszFilter string specifies the types of files that will be dealt with.

The pParentWnd parameter is a pointer to the parent of the dialog box, if the dialog box has one.

File Saving

One of the operations you can allow a user to perform on a file is to save it, either after creating it or after saving. If you want to save a file, pass the first argument of the CFileDialog constructor as FALSE.

File Opening

As opposed to saing a file, you can allow the user to open one. To create a File Dialog box that the user can use to open a file, pass the first argument of the CFileDialog constructor as TRUE.

File Dialog Properties

The only member variable that the File Dialog has is called m_ofn. It is an object derived from a Win32 class (or structure) called OPENFILENAME. In fact, the MFC doesn't do miracles than the Win32 would. The MFC only provides some type of "translation" of the Win32 library on this issue.

The OPENFILENAME class is defined as follows:

typedef struct tagOFN { 
  DWORD         lStructSize; 
  HWND          hwndOwner; 
  HINSTANCE     hInstance; 
  LPCTSTR       lpstrFilter; 
  LPTSTR        lpstrCustomFilter; 
  DWORD         nMaxCustFilter; 
  DWORD         nFilterIndex; 
  LPTSTR        lpstrFile; 
  DWORD         nMaxFile; 
  LPTSTR        lpstrFileTitle; 
  DWORD         nMaxFileTitle; 
  LPCTSTR       lpstrInitialDir; 
  LPCTSTR       lpstrTitle; 
  DWORD         Flags; 
  WORD          nFileOffset; 
  WORD          nFileExtension; 
  LPCTSTR       lpstrDefExt; 
  LPARAM        lCustData; 
  LPOFNHOOKPROC lpfnHook; 
  LPCTSTR       lpTemplateName; 
#if (_WIN32_WINNT >= 0x0500)
  void *        pvReserved;
  DWORD         dwReserved;
  DWORD         FlagsEx;
#endif // (_WIN32_WINNT >= 0x0500)
} OPENFILENAME, *LPOPENFILENAME;

File Dialog Operations

In order to use a File Dialog box, the user must open it. This can be done by calling the CFileDialog::DoModal() method.

When it comes up, the File Dialog displays two buttons regularly available on a normal dialog box. If the file is being opened, the buttons are Open and Cancel. If the user is saving a file, the buttons are Save and Cancel. After using the dialog box, either to open or to save a file, the user can click Open/Save or Cancel. If the user clicks Open or Save, the dialog box is (partly) equipped to validate the operation. For example, if the user clicks Open, the dialog box is configured to make sure that the user selected a file. If the user clicks Save, he or she should have given a name to the file. To dismiss the dialog box without continuing with the intended operation, the user can click Cancel. If the user clicks Cancel, the dialog box throws a CommDlgExtendedError. This error allows you to find out what happens: maybe the user didn't click Cancel, maybe the dialog box was not even created, maybe this, maybe that. This error allows you to diagnose a potential problem and deal with it. If you don't want to go through this, you can just cancel the operation yourself.

After displaying the File Dialog box, in other to effectively perform an intended operation, the user must provide a name for the file. To find out what name the user provided, you can call the CFileDialog::GetFileName() method. Its syntax is:

CString GetFileName( ) const;

A file name is indeed made of various sections. In the strict sense, the file name is a long string that starts from its drive all the way to its "normal" name. This is also referred to as its path. To get the path of the being dealt with, you can call the CFileDialog::GetPathName() method. Its syntax is:

CString GetPathName( ) const;

In the traditional sense, the name of a file ends with 2, 3, or 4 characters known as its extension. For example, the extension of a Microsoft Word file is doc. The extension of a Perl file is pl. After the user has dealt with a file or while dealing with it, to find out the extension of that file, you can call the CFileDialog::GetFileExt() method. Its syntax is:

CString GetFileExt( ) const;

Practical Learning: Using the File Dialog Box

  1. If you want to follow with this example, create a New Dialog Based application named FileProc
  2. Design the dialog box as follows:
     
    ControlIDCaptionAdditional Properties
    Static TextStatic TextMake:

     
    Edit BoxIDC_MAKE  
    Static Text Model: 
    Edit BoxIDC_MODEL  
    Static Text Year: 
    Edit BoxIDC_YEAR Align Text: Right
    Static Text Mileage: 
    Edit BoxIDC_MILEAGE Align Text: Right
    Static Text Owner 
    Edit BoxIDC_OWNER  
    ButtonIDC_SAVE_BTN&Save 
    ButtonIDC_OPEN_BTN&Open 
    ButtonIDCANCELClose 
  3. Using either the ClassWizard or the Add Member Variable Wizard, add the following member variables to the controls:
     
    IdentifierValue Variable
    IDC_MAKECStringm_Make
    IDC_MODELCStringm_Model
    IDC_YEARCStringm_Year
    IDC_MILEAGECStringm_Doors
    IDC_OWNERCStringm_Owner
  4. Generate a BN_CLICKED message for the IDC_SAVE_BTN button and, after accepting the suggested name of the event, implement it as follows:
     
    void CFileProc1Dlg::OnSaveBtn() 
    {
    	// TODO: Add your control notification handler code here
    	this->UpdateData();
    
    	CFile f;
    
    	char strFilter[] = { "BCR Files (*.bcr)|*.bcr|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(FALSE, ".bcr", NULL, 0, strFilter);
    
    	if( FileDlg.DoModal() == IDOK )
    	{
    	f.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
    		CArchive ar(&f, CArchive::store);
    
    		ar << m_Make << m_Model << m_Year << m_Mileage << m_Owner;
    		ar.Close();
    	}
    	else
    		return;
    
    	f.Close();
    }
  5. Generate a BN_CLICKED message for the IDC_OPEN_BTN button and, after accepting the suggested name of the event, implement it as follows:
     
    void CFileProc1Dlg::OnOpenBtn() 
    {
    	// TODO: Add your control notification handler code here
    	this->UpdateData();
    
    	CFile f;
    
    	char strFilter[] = { "BCR Files (*.bcr)|*.bcr|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(TRUE, ".bcr", NULL, 0, strFilter);
    
    	if( FileDlg.DoModal() == IDOK )
    	{
    		if( f.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
    			return;
    		CArchive ar(&f, CArchive::load);
    
    		ar >> m_Make >> m_Model >> m_Year >> m_Mileage >> m_Owner;
    		ar.Close();
    	}
    	else
    		return;
    
    	f.Close();
    	this->UpdateData(FALSE);
    }
  6. Test the application

  7. Create and save a file
     

  8. Close the dialog box

  9. Test the application again

  10. Open the previously created file


from: http://www.functionx.com/visualc/controls/filedialog.htm

posted on 2006-09-28 22:03 weidagang2046 阅读(681) 评论(0)  编辑  收藏 所属分类: Windows


只有注册用户登录后才能发表评论。


网站导航: