MFC选择文件(夹)按钮实现
选择文件(夹)
void CFileSelectDlg::OnBnClickedButtonSelect()
{
if(((CButton*)(GetDlgItem(IDC_RADIO_FILE)))->GetCheck())
{
char strExt[] = "dat";//设置文件的读取类型
CString szFilter = "GeoTiff Files (*.dat)|*.dat|All Files (*.*)|*.*||";
SelectFile(strExt,szFilter);
}
else
{
CString floder = SelectFolder();
SetDlgItemText(IDC_EDIT_PATH,floder);
}
}
选择文件
bool CFileSelectDlg::SelectFile(char* strExt,CString const& szFilter)
{
CFileDialog dlg(TRUE,strExt,NULL,OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,szFilter);
dlg.m_ofn.lpstrTitle = "请选择缓存文件";
CString inPath;
CArray<CString,CString> aryFilename;
if(dlg.DoModal() == IDOK)
{
CString filenames;
POSITION posFile = dlg.GetStartPosition();
while(posFile != NULL)
{
aryFilename.Add(dlg.GetNextPathName(posFile));
}
} int SelFileNum = aryFilename.GetSize();
if(SelFileNum==)
{
AfxMessageBox("没有选择缓存文件");
return false;
}
return true;
}
选择文件夹
CString CFileSelectDlg::SelectFolder()
{
TCHAR szFolderPath[] = {};
CString strFolderPath;
BROWSEINFO sInfo;
::ZeroMemory(&sInfo,sizeof(BROWSEINFO));
sInfo.pidlRoot = ;
sInfo.lpszTitle = "请选择缓存文件所在文件夹";
sInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_EDITBOX |BIF_DONTGOBELOWDOMAIN;
sInfo.lpfn = NULL; LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
if(lpidlBrowse != NULL)
{
if(::SHGetPathFromIDListA(lpidlBrowse,szFolderPath))
{
strFolderPath = szFolderPath;
}
}
if(lpidlBrowse != NULL)
{
::CoTaskMemFree(lpidlBrowse);
}
return strFolderPath;
}