简体   繁体   中英

Showing a Windows Explorer Dialog from a C++ application

I have a windows application written in C++. The application generates certain configuration files in a hidden directory. I want to give user an option to open that directory from my application. Clicking that option should open a windows explorer like dialog with an input directory location. I spend time searching for a similar api, but end up with certain dialogs like "DlgDirListComboBoxW" or "GetOpenFileName" or "GetSaveFileName". I am looking for an api to open normal Windows explorer like Dialog with an input directory location. It would be really helpful if the api belongs to CommonDialogs section.

How about:

HWND hWndOwner = NULL;

ShellExecute(
    hWndOwner,
    _T("explore"),
    _T("c:\\some\\path"),
    NULL,
    NULL,
    SW_SHOWNORMAL);

You can set hWndOwner to your main window handle if you're so inclined and can choose from a variety of other options.

For more information and usage details, check out the MSDN page on ShellExecute .

You can use the SHBrowseForFolder

It shows a dialog similar to this:

在此处输入图片说明

This is a example for how to use it:

BOOL GetFolder(LPCSTR folderpath, 
               LPCSTR szCaption, 
               HWND hOwner /*= NULL*/)
{
    BOOL retVal = FALSE;

    // The BROWSEINFO struct tells the shell 
    // how it should display the dialog.
    BROWSEINFO bi;
    memset(&bi, 0, sizeof(bi));

    bi.ulFlags   = BIF_USENEWUI;
    bi.hwndOwner = hOwner;
    bi.lpszTitle = szCaption;   

    // must call this if using BIF_USENEWUI
    ::OleInitialize(NULL);

    // Show the dialog and get the itemIDList for the 
    // selected folder.
    LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);

    if(pIDL != NULL)
    {
        // Create a buffer to store the path, then 
        // get the path.
        char buffer[_MAX_PATH] = {'\0'};
        if(::SHGetPathFromIDList(pIDL, buffer) != 0)
        {
            // Set the string value.
            folderpath = buffer;
            retVal = TRUE;
        }

        // free the item id list
        CoTaskMemFree(pIDL);
    }

    ::OleUninitialize();

    return retVal;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM