VC中打开对话框选择文件和文件夹

时间:2021-06-26 11:07:09
1、选择文件

              CFileDialogdlg(true, NULL, NULL, NULL, "所有文件 | *.*", this);

                 if (IDOK == dlg. DoModal())

                {

                                 MessageBox(dlg .GetPathName(), _T("" ));

                }

2、选择文件夹

                 BROWSEINFO bi ;            // BROWSEINFO结构体

                 TCHAR Buffer [512] = "";

                 TCHAR FullPath [512] = "";

                 bi.hwndOwner = m_hWnd;    // m_hWnd程序主窗口

                 bi.pidlRoot = NULL;

                 bi.pszDisplayName = Buffer; // 返回选择的目录名的缓冲区

                 bi.lpszTitle = "Selection"; // 弹出的窗口的文字提示

                 bi.ulFlags = BIF_RETURNONLYFSDIRS ; // 只返回目录。其他标志看MSDN

                 bi.lpfn = NULL;                         // 回调函数,有时很有用

                 bi.lParam = 0;

                 bi.iImage = 0;

                 ITEMIDLIST* pidl = ::SHBrowseForFolder (& bi); // 显示弹出窗口,ITEMIDLIST很重要

                 if (::SHGetPathFromIDList (pidl, FullPath))     // 在ITEMIDLIST中得到目录名的整个路径

                {

                                 // 成功

                }

                 else

                {

                                 // 失败

                }
VC中打开对话框选择文件和文件夹 本文用菊子曰发布