MFC下选择 目录对话框 和 选择文件对话框

1、打开目录

CString m_strPath;
	UpdateData(TRUE);

	TCHAR szDir[MAX_PATH];
	BROWSEINFO bi;
	ITEMIDLIST *pidl;

	bi.hwndOwner = this->m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szDir;
	bi.lpszTitle =_T( "请选择目录");
	bi.ulFlags = BIF_STATUSTEXT | BIF_USENEWUI | BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
	bi.lParam = 0;
	bi.iImage = 0;
	pidl = SHBrowseForFolder(&bi);

	if( pidl == NULL )  
		return;
	if(!SHGetPathFromIDList(pidl, szDir))  
		return;
	else  
		m_strPath = szDir;
	UpdateData(FALSE);   

2、打开文件

	CString m_ReleaseVerFilePath;
        //打开文件的类型
	TCHAR szFilter[]=_T("files(*.bat)|*.bat|files(*.py)|*.py|所有文件(*.*)|*.*||");
	CFileDialog dlg(1,NULL,NULL,NULL,szFilter);

	//设置默认路径
	dlg.m_ofn.lpstrInitialDir  = _T("C://build_nk//") ;
		
	if(dlg.DoModal()==IDOK) 
	{
		m_ReleaseVerFilePath = dlg.GetPathName();
	}

3、打开一个路径下的文件夹

ShellExecute(this->GetSafeHwnd(),"open",strPath,NULL,NULL,SW_SHOW);
//其中strPath就是你的参数路径了

4、获取文件的路径

TCHAR szBuffer[MAX_PATH] = {0}; 
OPENFILENAME ofn= {0}; 
ofn.lStructSize = sizeof(ofn); 
ofn.hwndOwner = m_hWnd; 
ofn.lpstrFilter = _T("Exe文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0");//要选择的文件后缀 
ofn.lpstrInitialDir = _T("D:\\Program Files");//默认的文件路径 
ofn.lpstrFile = szBuffer;//存放文件的缓冲区 
ofn.nMaxFile = sizeof(szBuffer)/sizeof(*szBuffer); 
ofn.nFilterIndex = 0; 
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER ;//标志如果是多选要加上OFN_ALLOWMULTISELECT
BOOL bSel = GetOpenFileName(&ofn); 





©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页