//1、首先给出的文件夹路径选择的简单例子;(SHBrowseForFolder)
BROWSEINFO bi;
WCHAR szPath[MAX_PATH];
ZeroMemory((void*)&bi, sizeof(bi));
ZeroMemory(szPath, sizeof(szPath));
bi.hwndOwner = this->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = L"搜索目录";
bi.ulFlags = BIF_RETURNONLYFSDIRS; ///< 任选目录
bi.lpfn = NULL;
bi.lParam = NULL;
bi.iImage = NULL;
LPITEMIDLIST lpdList = SHBrowseForFolder(&bi);
if (NULL == lpdList)
{
return ; ///< 选择cancel
}
if (SHGetPathFromIDList(lpdList, szPath))
{
// szPath is valid;
CString path = szPath;
}
//2、这里给出的是(CFileDialog )的例子;
//更多的参数设置参见CFileDialog ::m_ofn的设置,详见msdn
OPENFILENAME ofn;
//ZeroMemory((void*)&ofn, sizeof(ofn));
//ofn.lStructSize = sizeof (OPENFILENAME);
//ofn.hwndOwner = this->m_hWnd;
//ofn.hInstance = NULL;
//ofn.lpstrFilter = L"Text (*.txt)|(*.txt)||";
//ofn.lpstrCustomFilter = ;
//ofn.nMaxCustFilter = ;
//ofn.nFilterIndex = ;
//ofn.lpstrFile = ;
//ofn.nMaxFile = MAX_PATH;
CFileDialog dlgFile(TRUE); ///< true表示文件选择,false 文件保存,具体参见msdn
CString fileName;
WCHAR path[MAX_PATH+1];
ZeroMemory(path, sizeof(path));
dlgFile.GetOFN().lpstrFile = path;
dlgFile.GetOFN().nMaxFile = MAX_PATH;
if (IDCANCEL == dlgFile.DoModal())
{
return ;
}
CString pathName = dlgFile.GetPathName();
CString selectFile = dlgFile.GetFileName();
///< 多选路径读取;
POSITION pos = NULL;
CString posPaht;
do {
pos = dlgFile.GetStartPosition();
if (pos != NULL)
{
posPaht = dlgFile.GetNextPathName(pos);
MessageBox(posPaht, L"提示");
}
} while(pos != NULL);
vc 文件和文件路径选择
最新推荐文章于 2024-05-15 15:57:00 发布