调用此功能公开Windows常用文件对话框并允许用户浏览文件和目录并输入文件名。

virtual INT_PTR DoModal( );

CommDlgExtendedError 函数确定是否发生了错误。

如果要通过设置 m_ofn 结构的成员初始化各种文件对话框选项,则应在调用 DoModal之前执行此操作,但,对话框构造对象之后。

CFileDialog选件类,的代码示例所示在调用 DoModal之前设置 OFN_ALLOWMULTISELECT 标志。

然后可以调用其他成员函数检索安装信息或用户输入到对话框。

DoModal 是从选件类重写虚函数 CDialog

 
 
 
void CMyClass::OnFileOpen()
{
   // szFilters is a text string that includes two file name filters:
   // "*.my" for "MyType Files" and "*.*' for "All Files."
   TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

   // Create an Open dialog; the default file name extension is ".my".
   CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
      OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

   // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
   // returns IDOK.
   if(fileDlg.DoModal() == IDOK)
   {
      CString pathName = fileDlg.GetPathName();

      // Implement opening and reading file in here.

      //Change the window's title to the opened file's title.
      CString fileName = fileDlg.GetFileTitle();

      SetWindowText(fileName);
   }
}

 

相关文章: