CFileDialog 打开目录文件 保存目录文件

来源:互联网 发布:老公家暴 知乎 编辑:程序博客网 时间:2024/04/28 20:08


格式说明:

 

 

explicit CFileDialog(
   BOOL bOpenFileDialog,                         //TRUE 为打开, FALSE 为保存        


   LPCTSTR lpszDefExt = NULL,                 // 默认文件扩展名

 


   LPCTSTR lpszFileName = NULL,            //文件对话框中 初始的文件名


   DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,         //设定对话框功能

 


   LPCTSTR lpszFilter = NULL,                   //文件过滤

 


   CWnd* pParentWnd = NULL,


   DWORD dwSize = 0,                            // The size of theOPENFILENAME structure, 默认为0 ,表示自动确定正确的大小


   BOOL bVistaStyle = TRUE
);

 

 

参数含义详细说明:

 

[in] bOpenFileDialog

The parameter that specifies what type of dialog box to create. Set it to TRUE to construct a File Open dialog box. Set it to FALSE to construct aFile Save As dialog box.

 

[in] lpszDefExt

The default file name extension. If the user does not include an extension in the Filename box, the extension specified bylpszDefExt is automatically appended to the file name. If this parameter isNULL, no extension is appended.

 

[in] lpszFileName

The initial file name that appears in the Filename box. If NULL, no initial file name appears.

 

[in] dwFlags

A combination of one or more flags that you can use to customize the dialog box. For a description of these flags, see theOPENFILENAME structure in the Windows SDK. If you modify them_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.

 

[in] lpszFilter

A series of string pairs that specify filters you can apply to the file. If you specify file filters, only files that match filter criteria will appear in the Files list. See the Remarks section for more information about how to work with file filters.

 

[in] pParentWnd

A pointer to the parent or owner window of the file dialog box.

 

[in] dwSize

The size of the OPENFILENAME structure. This value depends on the operating system version. MFC used this parameter to determine the appropriate kind of dialog box to create (for example, new Windows 2000 dialog boxes instead of NT4 dialog boxes). The default size of 0 means that the MFC code will determine the correct dialog box size to use based on the operating system version on which the program is run.

 

[in] bVistaStyle

Note   This parameter is applicable only if you are compiling in Windows Vista.

The parameter that specifies the style of the file dialog. Set it to TRUE to use the new Vista style file dialogs. Otherwise, the old style of dialog boxes will be used. See the Remarks section for more information about compiling under Vista.

 

       Either a File Open or File Save As dialog box is constructed, depending on the value ofbOpenFileDialog.

 

To enable the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before you callDoModal.

 

You must supply your own file name buffer to store the returned list of multiple file names. Do this by replacing

 

m_ofn.lpstrFile with a pointer to a buffer you have allocated, after you construct theCFileDialog, but before you call

 

DoModal. Additionally, you must set m_ofn.nMaxFile with the number of characters in the buffer pointed to by

 

m_ofn.lpstrFile. If you set the maximum number of files to be selected ton, the necessary buffer size isn*

 

(_MAX_PATH + 1) + 1.

 

 

实例1 打开文件:

 

 

[c-sharp] view plaincopyprint?
  1. // Create dialog to open multiple files.  
  2. CFileDialog dlg(TRUE, _T("txt"), _T("*.txt"), OFN_ALLOWMULTISELECT);  
  3.   
  4. // Create buffer for file names.  
  5. const DWORD numberOfFileNames = 100;  
  6. const DWORD fileNameMaxLength = MAX_PATH + 1;  
  7. const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;  
  8. TCHAR* filenamesBuffer = new TCHAR[bufferSize];  
  9.   
  10. // Initialize beginning and end of buffer.  
  11. filenamesBuffer[0] = NULL;  
  12. filenamesBuffer[bufferSize-1] = NULL;  
  13.   
  14. // Attach buffer to OPENFILENAME member.  
  15. dlg.m_ofn.lpstrFile = filenamesBuffer;  
  16. dlg.m_ofn.nMaxFile = bufferSize;  
  17.   
  18. // Create array for file names.  
  19. CString fileNameArray[numberOfFileNames];  
  20. if(dlg.DoModal() == IDOK)  
  21. {  
  22.     // Retrieve file name(s).  
  23.     POSITION fileNamesPosition = dlg.GetStartPosition();  
  24.     int iCtr = 0;  
  25.     while(fileNamesPosition != NULL)  
  26.     {  
  27.         fileNameArray[iCtr] = dlg.GetNextPathName(fileNamesPosition);  
  28.         iCtr++;  
  29.     }    
  30. }  
  31. // Release file names buffer.  
  32. delete[] filenamesBuffer;  

 

 

实例2 打开文件:

[c-sharp] view plaincopyprint?
  1.    CFileDialog  dlg(  
  2.                      TRUE,  
  3.                      "*",   
  4.                      "*.xyz",  
  5.                      OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,   
  6.                     "All Files(*.xyz|*.xyz||"  
  7.                   );  
  8.    char   szbuffer[1024];  
  9.  szbuffer[0]=0;  
  10.  dlg.m_ofn.lpstrFile = szbuffer;  
  11.  dlg.m_ofn.nMaxFile = 1024;  
  12.   
  13.  if(IDOK==dlg.DoModal())  
  14. {  
  15.  POSITION   pos = dlg.GetStartPosition();  
  16.  CString    filepath;  
  17.   
  18.  while(pos!=NULL)  
  19.  {  
  20.      filepath = dlg.GetNextPathName(pos);  
  21.  }  
  22. }     

 

 

实例3 打开文件:

 

[c-sharp] view plaincopyprint?
  1.           
  2.         CString filePath;  
  3. char    fileName[256];  
  4. char    filter[] = "GEO Files(*.GEO)|*.GEO|All Files(*.*)|*.*||";  
  5.   
  6. UpdateData(TRUE);  
  7.   
  8. CFileDialog  fdlg(TRUE, "bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter );  
  9.   
  10. strcpy(FileName, _T("文件"));   
  11.   
  12. if ( IDOK != cf.DoModal()) return;  
  13. filePath = fdlg.GetPathName();   // filePath即为所打开的文件的路径  
  14.   
  15. UpdateData(FALSE);  

 

 实例4 打开文件,并设置对话框标题

[cpp] view plaincopyprint?
  1. CFileDialog nFileDlg(TRUE,L"xml",L"",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"XML文件(*.xml)|*.xml||");  
  2. nFileDlg.m_pOFN->lpstrTitle=L"打开空白任务"//文件对话框标题  
  3. if(nFileDlg.DoModal()==IDOK)  
  4. {  
  5.     m_PicFolder=L"Blank";  
  6.     m_XMLFolder=L"Blank";  
  7.   
  8.     CString szXmlFilePath;  
  9.     CString szXmlParentPath;  
  10.     CString nXMLFileName;  
  11.   
  12.     szXmlFilePath=nFileDlg.GetPathName();  //  绝对路径文件名  
  13.     nXMLFileName=nFileDlg.GetFileName();   //  不带路径的文件名  
  14.     szXmlParentPath=szXmlFilePath.Left(szXmlFilePath.GetLength()-nXMLFileName.GetLength()-1);  //文件所在的父目录  
  15.   
  16.   
  17. }  




 

 

实例5  保存文件:

[c-sharp] view plaincopyprint?
  1. char        FileName[256];  
  2. CString     Title, FmtString;  
  3. CString     PathName;  
  4.        CString           path_and_fileName;  
  5.   
  6. UpdateData(TRUE);  
  7.           
  8.        PathName=_T("path.xml");  
  9.   
  10. char    BASED_CODE szFilter[] = "XML Files(*.xml)|*.XML|All Files(*.*)|*.*||";  
  11.   
  12. CFileDialog fdlg(FALSE, "XML", PathName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter );  
  13.   
  14. strcpy(FileName, _T("文件名"));   
  15.   
  16. if ( IDOK != fdlg.DoModal() ) return;  
  17. path_and_fileName= fdlg.GetPathName();   //path_and_fileName即为文件保存路径  
  18.   
  19. UpdateData(FALSE);  

 

 

 

 

 以上是使用 CFileDialog打开文件,下面是使用SHBrowseForFolder打开路径:


[cpp] view plaincopyprint?
  1. OnBnClickedButton1()  
  2. {  
  3.     BROWSEINFO bi;  
  4.     ZeroMemory(&bi,sizeof(BROWSEINFO));  
  5.     LPMALLOC pMalloc;  
  6.     LPITEMIDLIST pidl = SHBrowseForFolder(&bi);  
  7.   
  8.     if (pidl==NULL)  
  9.         return;  
  10.   
  11.     if(pidl != NULL)  
  12.     {  
  13.         TCHAR * path = new TCHAR[MAX_PATH];   
  14.   
  15.         SHGetPathFromIDList(pidl,path);  
  16.         //      MessageBox(NULL,path,TEXT("Choose"),MB_OK);  
  17.         if(SUCCEEDED(SHGetMalloc(&pMalloc)))//pidl指向的对象用完应该释放,之前忽略了  
  18.         {  
  19.             pMalloc->Free(pidl);  
  20.             pMalloc->Release();  
  21.         }  
  22.         m_Path=path;  
  23.         UpdateData(FALSE);    
  24.   
  25.         delete [] path;  
  26.     }  
  27.   
  28.   
  29.   
  30.   
  31.   
  32. }  
0 0
原创粉丝点击