最近做一个MFC的小界面,需要实现这样两个功能:1、在界面上加一个按钮,单击按钮弹出一个对话框选择文件,在工程中获得文件的路径;2、在界面上加一个按钮,单击按钮弹出一个对话框选择文件夹,在工程中获取文件夹的路径。
此处借鉴了http://blog.csdn.net/computerme/article/details/41307849
//选择样本图像保存路径 void CDialogSampled::OnBnClickedButtonImageSavePathSelect() { // TODO: 在此添加控件通知处理程序代码 CString m_saveFilePath; //打开图像文件,获取文件路径名 TCHAR szPath[_MAX_PATH]; BROWSEINFO bi; bi.hwndOwner = GetSafeHwnd(); bi.pidlRoot = NULL; bi.lpszTitle = "Please select the input path"; bi.pszDisplayName = szPath; bi.ulFlags = BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = NULL; LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi); if(pItemIDList) { if(SHGetPathFromIDList(pItemIDList,szPath)) { m_saveFilePath = szPath; m_saveFilePath = m_saveFilePath+"\\"; } //use IMalloc interface for avoiding memory leak IMalloc* pMalloc; if( SHGetMalloc(&pMalloc) != NOERROR ) { TRACE(_T("Can't get the IMalloc interface\n")); } pMalloc->Free(pItemIDList); if(pMalloc) pMalloc->Release(); UpdateData(FALSE); } } 123456789101112131415161718192021222324252627282930313233343536373839 <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e9f16cbbc2.css" rel="stylesheet"> </div>