Question

Dans MFC, est-il un dialogue Ouvrir le dossier? Ainsi, plutôt que de choisir un nom de fichier, il choisit un nom de dossier? Idéalement, je voudrais que ce soit la façon dont Visual Studio, il fait lors de la navigation pour un « Emplacement du projet » (lors de la création d'un nouveau projet), qui ressemble beaucoup à une boîte de dialogue de fichier normal. Mais je pouvais faire avec l'un des arbre vertical sorte d'interfaces si l'ancien n'existe pas.

Était-ce utile?

La solution

Ce code vous obtiendrez une boîte de dialogue de dossier ouvert (ce qui a été quelque part sur le web, mais je ne sais pas vraiment où).

CString szSelectedFolder = _T("");

// This is the recommended way to select a directory
// in Win95 and NT4.
BROWSEINFO bi;
memset((LPVOID)&bi, 0, sizeof(bi));
TCHAR szDisplayName[_MAX_PATH];
szDisplayName[0] = '\0';
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = _T("Select a folder");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
// Set the callback function
bi.lpfn = BrowseCallbackProc;

LPITEMIDLIST pIIL = ::SHBrowseForFolder(&bi);
TCHAR szReturnedDir[_MAX_PATH];

BOOL bRet = ::SHGetPathFromIDList(pIIL, (TCHAR*)&szReturnedDir);
if (bRet)
{
    if (szReturnedDir != _T(""))
    {
        szSelectedFolder = szReturnedDir;
    }

    LPMALLOC pMalloc;
    HRESULT HR = SHGetMalloc(&pMalloc);
    pMalloc->Free(pIIL);
    pMalloc->Release();
}

vous aurez également à mettre en œuvre cette fonction de rappel:

TCHAR szInitialDir[_MAX_PATH];

// Set the initial path of the folder browser
int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
    // Look for BFFM_INITIALIZED
    if (uMsg == BFFM_INITIALIZED)
    {
        SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)szInitialDir);
    }
    return 0;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top