【问题标题】:mfc CFileDialog using IFileOpenDialog to browse for foldermfc CFileDialog 使用 IFileOpenDialog 浏览文件夹
【发布时间】:2013-02-01 22:03:44
【问题描述】:

我发现以下代码可以让我浏览文件夹

CFileDialog od(TRUE/*bOpenFileDialog*/, NULL, NULL,     OFN_HIDEREADONLY |
                      OFN_OVERWRITEPROMPT , NULL, NULL, 0,   TRUE/*bVistaStyle*/);
    IFileOpenDialog * openDlgPtr = od.GetIFileOpenDialog();
    if ( openDlgPtr != NULL )
     {
      openDlgPtr->SetOptions(FOS_PICKFOLDERS);
      openDlgPtr->Release();
    }

    int r = od.DoModal();

它会打开一个文件对话框,我可以选择一个文件夹并启用“打开”按钮,但按下它只会打开文件夹,它不会选择它。除非我点击取消,否则 DoModal 不会返回

任何想法如何在 MFC 中选择文件夹?谢谢

顺便说一下,我知道 CFolderDialog http://www.codeproject.com/Articles/2024/CFolderDialog-Selecting-Folders?msg=4497794#xx4497794xx

不错的项目,但是当我选择我的 USB 安装的 android 文件夹时,对话框返回不正常,所以它对我没有用,除非我可以修复它

更新

我也发现了这个

BROWSEINFO bi = { 0 };
TCHAR path[MAX_PATH];
bi.lpszTitle = _T("Pick a Directory");
bi.pszDisplayName = path;
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
    // get the name of the folder
    //_tprintf ( _T("Selected Item: %s\n"), path );
    // free memory used
    IMalloc * imalloc = 0;
    if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
    {
        imalloc->Free ( pidl );
        imalloc->Release ( );
    }
    setMobilePath(path);
}

这确实允许我在我的 android 设备上选择一个文件夹,但它不会返回完整路径,只是返回的文件夹名称也没什么用

【问题讨论】:

  • 我意识到我正在尝试做的事情是根本错误的。 android 设备只显示在 windows 上,然后用户在 android 上选择 USB 调试模式,但大多数普通人不这样做,所以它不是将我的 windows 应用程序与我的 android 应用程序同步的好方法

标签: mfc cfiledialog


【解决方案1】:

将返回的pidl转换为字符串如下:

BROWSEINFO bi = { 0 };
bi.lpszTitle = _T("Pick a Directory");
LPITEMIDLIST pidl = SHBrowseForFolder (&bi);
if (pidl != 0)
{   // convert pidl to string
    TCHAR szPath[MAX_PATH];
    SHGetPathFromIDList(pidl, szPath);
    // free memory used
    IMalloc * imalloc = 0;
    if ( SUCCEEDED( SHGetMalloc(&imalloc)))
    {   imalloc->Free (pidl);
        imalloc->Release();
    }
    //_tprintf(_T("Selected Item: %s\n"), szPath);
    setMobilePath(szPath);
}

【讨论】:

  • 谢谢,我试过了,但是当我选择 andoird 设备文件夹时它崩溃了。我想这是一个特殊的挂载点,我需要使用其他东西来访问它。
【解决方案2】:

试试这个

CFolderPickerDialog dlgFolder;

if (dlgFolder.DoModal() == IDOK)
{
    CString strFolder = dlgFolder.GetPathName();
    AfxMessageBox(strFolder);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 2011-10-25
    • 2018-10-18
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2015-12-16
    相关资源
    最近更新 更多