Shell BrowseForFolder 预选路径

发布于 2024-12-02 14:48:40 字数 367 浏览 2 评论 0原文

我有这样的调用:

oShell.BrowseForFolder(Me.hwnd, "Select path:", 0, "C:\dir\")

这将打开一个标准文件浏览器对话框,其中“C:\dir\”为 root。 我的问题是你无法浏览根文件夹之上。 (如文档 http://msdn.microsoft.com/en 中指定-us/library/bb774065(v=vs.85).aspx)

对于使用选定路径和完全浏览功能打开此对话框有什么建议吗?

谢谢

I have this call:

oShell.BrowseForFolder(Me.hwnd, "Select path:", 0, "C:\dir\")

This opens a standard file browser dialog with "C:\dir\" as root.
My problem is that you can not browse above the root folder. (as specified in doc http://msdn.microsoft.com/en-us/library/bb774065(v=vs.85).aspx)

Any suggestions on oppening this dialog with a selected path and full browsing posibility?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯离梦残月幽梦 2024-12-09 14:48:40

执行此操作的方法涉及调用底层 API,SHBrowseForFolder()< /代码>

由于您希望整个 shell 命名空间可用,因此需要将 NULL 作为 pidlRoot。为了选择所需的文件夹,您需要在 < 中提供回调代码>lpfn。使此回调响应 BFFM_INITIALIZED 通过设置选定的文件夹。此选择是通过将 BFFM_SETSELECTION 消息发送到对话框的窗口句柄(传递给回调函数)来执行的。

没有代码,因为我没有 VB6,但希望该方法的概述足以让您上路。

The way to do this involves calling the underlying API, SHBrowseForFolder().

Since you want the entire shell namespace to be available you need to pass NULL as pidlRoot. In order to select your desired folder you will need to provide a callback in lpfn. Make this callback respond to BFFM_INITIALIZED by setting the selected folder. This selection is performed by sending the BFFM_SETSELECTION message to the dialog's window handle (passed to the callback function).

No code because I don't have VB6, but hopefully this outline of the method is enough to get you on your way.

z祗昰~ 2024-12-09 14:48:40

Karl E Peterson 的优秀网站包含一个示例,演示了带有回调的 API 调用 SHBrowseForFolder,如 David Heffernan 的回答所示。

KeyStuff 项目

查看 MFolderBrowse.bas,例程 BrowseForFolderByPIDL,它传递回调函数BrowseCallbackProc

Karl E Peterson's excellent website contains a sample which demonstrates the API call SHBrowseForFolder with a callback, as in David Heffernan's answer.

The KeyStuff project

Look at MFolderBrowse.bas, routine BrowseForFolderByPIDL which passes a callback function BrowseCallbackProc.

以歌曲疗慰 2024-12-09 14:48:40

尝试旧的 CCRP 项目。它有一个很好的浏览对话框实现。我在我的几个项目中使用了它,它具有解决您遇到的问题的属性。

Try the old CCRP project. It has a nicely done implementation of the Browse dialog. I used it in several of my projects and it has properties to address the issue you are having.

乱世争霸 2024-12-09 14:48:40

下面是准备复制并粘贴到 C++ 类中的代码:

// static
int CALLBACK Func::FolderBrowserCallback(HWND h_Dlg, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
    if (uMsg == BFFM_INITIALIZED)
    {
        // Requires Windows XP or higher
        SendMessageW(h_Dlg, BFFM_SETEXPANDED, TRUE, lpData);
    }
    return 0;
}

// returns an empty string u16_PathOut if an error occurrs or if the user cancels the dialog
void Func::GetOpenFolder(HWND           h_Owner, 
                         const WCHAR* u16_Title,     // IN:  Title at the top of dialog
                         int          s32_CsidlRoot, // IN:  Root folder for treeview (CSIDL_DRIVES -> My Computer)
                         const WCHAR* u16_Preselect, // IN:  NULL or the folder to be preselected and expanded
                         WCHAR*       u16_PathOut)   // OUT: selected path
{
    u16_PathOut[0] = 0;

    // CoInitialize(NULL);
    // InitCommonControls();

    ITEMIDLIST* pk_RootPIDL = NULL; // NULL -> Root = Desktop
    SHGetSpecialFolderLocation(h_Owner, s32_CsidlRoot, &pk_RootPIDL);

    BROWSEINFOW k_Info = {0};
    k_Info.hwndOwner = h_Owner;
    k_Info.pidlRoot  = pk_RootPIDL;
    k_Info.lpszTitle = u16_Title;
    k_Info.ulFlags   = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;

    if (u16_Preselect)
    {
        k_Info.lpfn   = FolderBrowserCallback;
        k_Info.lParam = (LPARAM)u16_Preselect;
    }

    // DO NOT DISABLE Wow64FsRedirection HERE !!
    LPITEMIDLIST pk_IDlist = SHBrowseForFolderW(&k_Info);
    if (pk_IDlist)
    {
        SHGetPathFromIDListW(pk_IDlist, u16_PathOut);
        CoTaskMemFree(pk_IDlist);
    }

    CoTaskMemFree(pk_RootPIDL);
}

Here a code ready for copy and paste in a C++ class:

// static
int CALLBACK Func::FolderBrowserCallback(HWND h_Dlg, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
    if (uMsg == BFFM_INITIALIZED)
    {
        // Requires Windows XP or higher
        SendMessageW(h_Dlg, BFFM_SETEXPANDED, TRUE, lpData);
    }
    return 0;
}

// returns an empty string u16_PathOut if an error occurrs or if the user cancels the dialog
void Func::GetOpenFolder(HWND           h_Owner, 
                         const WCHAR* u16_Title,     // IN:  Title at the top of dialog
                         int          s32_CsidlRoot, // IN:  Root folder for treeview (CSIDL_DRIVES -> My Computer)
                         const WCHAR* u16_Preselect, // IN:  NULL or the folder to be preselected and expanded
                         WCHAR*       u16_PathOut)   // OUT: selected path
{
    u16_PathOut[0] = 0;

    // CoInitialize(NULL);
    // InitCommonControls();

    ITEMIDLIST* pk_RootPIDL = NULL; // NULL -> Root = Desktop
    SHGetSpecialFolderLocation(h_Owner, s32_CsidlRoot, &pk_RootPIDL);

    BROWSEINFOW k_Info = {0};
    k_Info.hwndOwner = h_Owner;
    k_Info.pidlRoot  = pk_RootPIDL;
    k_Info.lpszTitle = u16_Title;
    k_Info.ulFlags   = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;

    if (u16_Preselect)
    {
        k_Info.lpfn   = FolderBrowserCallback;
        k_Info.lParam = (LPARAM)u16_Preselect;
    }

    // DO NOT DISABLE Wow64FsRedirection HERE !!
    LPITEMIDLIST pk_IDlist = SHBrowseForFolderW(&k_Info);
    if (pk_IDlist)
    {
        SHGetPathFromIDListW(pk_IDlist, u16_PathOut);
        CoTaskMemFree(pk_IDlist);
    }

    CoTaskMemFree(pk_RootPIDL);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文