weixin_39918690
2020-11-29 07:53 阅读 0

nana::filebox that can select a folder instead of a file

Hello,

This is more of a feature request than a bug. It would be a nice addition if a nana::filebox can be configured such that it would select a directory rather than a file.

该提问来源于开源项目:cnjinhao/nana

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • weixin_39949954 weixin_39949954 2020-11-29 07:53

    Perhaps it is good to have a new class for selecting a directory.

    点赞 评论 复制链接分享
  • weixin_39851977 weixin_39851977 2020-11-29 07:53

    This implementation might be handy. https://github.com/guillaumechereau/noc/blob/master/noc_file_dialog.h

    On Windows, NOC_FILE_DIALOG_DIR isn't supported so I would use this code.

    cpp
    
    #include "BrowseForFolder.h"
    
    #include <shlobj.h>
    
    #pragma comment(lib, "Shell32.lib")
    
    static int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData)
    {
        // If the BFFM_INITIALIZED message is received
        // set the path to the start path.
        switch (uMsg)
        {
            case BFFM_INITIALIZED:
            {
                if (NULL != lpData)
                {
                    SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
                }
            }
        }
    
        return 0; // The function should always return 0.
    }
    
    // HWND is the parent window.
    // szCurrent is an optional start folder. Can be NULL.
    // szPath receives the selected path on success. Must be MAX_PATH characters in length.
    BOOL BrowseForFolder(HWND hwnd, LPCTSTR szCurrent, LPTSTR szPath)
    {
        BROWSEINFO   bi = { 0 };
        LPITEMIDLIST pidl;
        TCHAR        szDisplay[MAX_PATH];
        BOOL         retval;
    
        CoInitialize(NULL);
    
        bi.hwndOwner      = hwnd;
        bi.pszDisplayName = szDisplay;
        bi.lpszTitle      = TEXT("Please choose a folder.");
        bi.ulFlags        = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
        bi.lpfn           = BrowseCallbackProc;
        bi.lParam         = (LPARAM) szCurrent;
    
        pidl = SHBrowseForFolder(&bi);
    
        if (NULL != pidl)
        {
            retval = SHGetPathFromIDList(pidl, szPath);
            CoTaskMemFree(pidl);
        }
        else
        {
            retval = FALSE;
        }
    
        if (!retval)
        {
            szPath[0] = TEXT('\0');
        }
    
        CoUninitialize();
        return retval;
    }
    
    </shlobj.h>
    点赞 评论 复制链接分享
  • weixin_39581845 weixin_39581845 2020-11-29 07:53

    If this feature is ever implemented, on Windows I recommend using the IFileDialog interface.

    
    #include <shobjidl.h>
    
    int main()
    {
        CoInitialize(NULL);
        IFileDialog *fd(nullptr);
        HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fd));
        if(SUCCEEDED(hr))
        {
            fd->SetOptions(FOS_PICKFOLDERS);
            fd->Show(0); // the native handle of the parent nana form goes here
            IShellItem *si;
            hr = fd->GetResult(&si); // fails if user cancelled
            if(SUCCEEDED(hr))
            {
                PWSTR pwstr(nullptr);
                hr = si->GetDisplayName(SIGDN_FILESYSPATH, &pwstr);
                if(SUCCEEDED(hr))
                {
                    // use the c-string pointed to by pwstr here
                    CoTaskMemFree(pwstr);
                }
                si->Release();
            }
            fd->Release();
        }
        CoUninitialize();
    }
    </shobjidl.h>
    点赞 评论 复制链接分享
  • weixin_39949954 weixin_39949954 2020-11-29 07:53

    A new folderbox has been added, it will be shown up in next release.

    点赞 评论 复制链接分享
  • weixin_39949954 weixin_39949954 2020-11-29 07:53

    A folderbox has been shown up in release 1.6.1

    点赞 评论 复制链接分享

相关推荐