VC++打开文件 CFileDialog::DoModal
CFileDialog::DoModal
调用此功能公开Windows常用文件对话框并允许用户浏览文件和目录并输入文件名。
virtual INT_PTR DoModal( );
IDOK 或 IDCANCEL。 如果 IDCANCEL 返回,则调用Windows CommDlgExtendedError 函数确定是否发生了错误。
IDOK 和 IDCANCEL 是指示的常数用户是否选择了"或"取消"按钮。
如果要通过设置 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); } }