MFC SDI文件打开对话框 [英] MFC SDI File Open Dialog
问题描述
我创建了一个基于资源管理器样式的MFC SDI项目(它有一个包含树视图的左侧窗格,右边我希望打开文件的手窗格。
已经有一个打开文件对话框的文件打开菜单项。当用户从文件打开对话框中选择文件时,如何知道文件已被选中以及其名称和目录位置是什么?如何在我的代码中捕获它?
非常感谢。
我编写任何MFC已经四年多了东西,但它是这样的:
CString路径;
if (dlg.DoModal()== IDOK)
{
path = dlg.GetPathName();
}
您可以使用CFileDialog :: DoModa()函数的返回值。如果是IDOK,则用户选择文件并通过单击打开按钮关闭对话框,如果是IDCANCEL,则使用取消按钮或X按钮关闭对话框。
示例:
CFileDialog FileDilog;
if(FileDialog.DoModal()== IDOK)
{
//所选文件的完整路径。
CString strFilePath = FileDialog.GetPathName();
}
如果您想知道允许多个文件选择使用代码,
if(FileDialog.DoModal()== IDOK)
{
POSITION pos = FileDialog.GetStartPosition();
while(pos!= NULL)
{
CString strFilePath = FileDialog.GetNextPathName(pos);
//处理文件
}
}
查看MSDN获取更多信息。 />
http://msdn.microsoft.com/ en-us / library / 8dwz4azc%28v = vs.80%29.aspx [ ^ ]
只需覆盖应用程序基于CDocument的类中的虚拟OnFileOpen函数。
虚拟 BOOL OnOpenDocument(LPCTSTR lpszPathName);
Hi,
I have created an MFC SDI project based on the explorer style (it has a left-hand pane containing a tree-view, and a right-hand pane into which I wish to open files).
There is already a file-open menu item which opens a file dialog. When the user selects a file from within the file-open dialog, how do I know that a file has been selected and what its name and directory location is? How do I capture this in my code?
Many thanks.
It''s been more than four years since I wrote any MFC stuff, but it goes something like this:
CString path; if (dlg.DoModal() == IDOK) { path = dlg.GetPathName(); }
Hi You can use the Return value of CFileDialog::DoModa(), Function. If it is IDOK the user selected a file and closed the dialog by clicking on open button and if it is IDCANCEL the dialog was closed using cancel button or X button.
Example:
CFileDialog FileDilog; if(FileDialog.DoModal() == IDOK) { //Full path of selected file. CString strFilePath = FileDialog.GetPathName(); }
In case if you want to know allow multiple file selection Use The code,
if (FileDialog.DoModal() == IDOK) { POSITION pos = FileDialog.GetStartPosition(); while (pos != NULL) { CString strFilePath = FileDialog.GetNextPathName(pos); // Process the file } }
Check MSDN for More info.
http://msdn.microsoft.com/en-us/library/8dwz4azc%28v=vs.80%29.aspx[^]
Just override the virtual OnFileOpen function in your application''s CDocument based class.
virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
这篇关于MFC SDI文件打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!