New style MFC folder (directory) selection dialog CFolderPickerDialog

tags: VC++ source code example  New style select directory or folder

New style select directory or folder style, as shown below:

prompt: This category is only applicable to vista and above windows operating systems. Selecting the directory on the xp system will become the selected file.
 
Method one [recommended]: use MFC CFolderPickerDialog

void CMCFolderPickerDialogDemoDlg:: OnBnClickedButton1()
{
        CFolderPickerDialog c fd( NULL, 0, this, 0);
       
        if (c fd. DoModal() == IDOK)
       {
               CString strFolder;
               strFolder = c fd. GetPathName();           
               MessageBox( strFolder);
       }
}
 
 
 
Method 2: Use com interface: IFileOpenDialog、IFileSaveDialog
Note: The interface method is invalid on the xp system.

static wstring  MySelectF olderDialog() {
        wstring  strFolder;
        CComPtr< IFileOpenDialog> spFileOpenDialog;
        if ( SUCCEEDED( spFileOpenDialog. CoCreateInstance( __uuidof( FileOpenDialog)))) {
               FILEOPENDIALOGOPTIONS options;
               if ( SUCCEEDED( spFileOpenDialog -> GetOptions(& options))) {
                      spFileOpenDialog -> SetOptions( options | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
                      if ( SUCCEEDED( spFileOpenDialog -> Show( nullptr))) {
                            CComPtr< IShellItem> spResult;
                            if ( SUCCEEDED( spFileOpenDialog -> GetResult( & spResult))) {
                                   wchar_t* pFolderN ame;
                                   if ( SUCCEEDED( spResult -> GetDisplayName( SIGDN_FILESYSPATH, &pFolderN ame))) {
                                          strFolder  = pFolderN ame;
                                          CoTaskMemFree(pFolderN ame);
                                  }
                           }
                     }
              }
       }
        return std:: move( strFolder);
}

 

Intelligent Recommendation

QT opens a new file selection dialog to get the selected file directory

A total of three files, main.cpp, widget.cpp, widget.h, pro-tested. QT5 can be manually dragged and dropped some buttons, and then linked to the slot function; the ui->*** involved in the interface...

Implementation of MFC-File Selection Dialog

Click on a button to pop up the code instance of the dialog box for the selection file. As shown in the figure below, click the Browse button, pop up the file selection dialog. Implement the code as f...

[MFC] Select Directory Dialog and Select File Dialog

Original link:[MFC] Select Directory Dialog and Select File Dialog Select Directory Dialog Select File Dialog Choose to replace "\" in the path to "\\"  ...

Use the folder selection dialog in WPF

Sometimes I want to achieve the effect of "select a folder" in development: In WPF, you can only select files using Microsoft.Win32.OpenFileDialog. FolderBrowserDialog can only select folder...

More Recommendation

Custom file (folder) selection dialog

In QT programming, customized interfaces are a very common requirement. After all, QT is indeed very powerful in this respect. As a commonly used file (folder) selection dialog, customization is inevi...

VC / MFC and folder selection file selection, snippet

1. Select the batch file 2, select the folder  ...

MFC Opens Folder dialog Initialization Open Path

When using the MFC developer, you sometimes need to select a folder, you can use the ShbrowseForFolder to resolve, but this method does not support the initialization selection path. Blog homepage: Ar...

A encapsulated ios style selection dialog

Speaking of graduating for a year and a half, I have always wanted to move some of the accumulated things to the blog, but I can’t find time (actually because of laziness, >_<). Now I have...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top