How to get list of selected files when using GetOpenFileName() with multiselect flag?

后端 未结 5 875
予麋鹿
予麋鹿 2021-01-15 19:20

I have tried googling, but people seem to have the same problem: we can\'t get a list of the selected files.

This is a simple piece of working code that is similar t

5条回答
  •  不思量自难忘°
    2021-01-15 19:55

    Here is a more complete version of the answers by Niall and Remy.

    vector &filePaths;
    
    if ( GetOpenFileName( &ofn ) == TRUE )
    {
        wchar_t *p = ofn.lpstrFile;
        wstring path = p;
        p += path.size() + 1;
        if ( *p == 0 )
        {
            // there is only one string, being the full path to the file
            filePaths.push_back( ConvertWideCharToUtf8( path.c_str() ) );
        }
        else
        {
            // multiple files follow the directory
            for ( ; *p != 0 ; )
            {
                wstring fileName = p;
                filePaths.push_back( ConvertWideCharToUtf8( ( path + L"\\" + fileName ).c_str() ) );
                p += fileName.size() + 1;
            }
        }
    }
    

    Where we also have the function:

    string ConvertWideCharToUtf8( const wchar_t *wideText )
    {
        int len = WideCharToMultiByte( CP_UTF8, 0, wideText, -1, NULL, 0, NULL, NULL );
        char *buffer = (char *)malloc( len );
        WideCharToMultiByte( CP_UTF8, 0, wideText, -1, buffer, len, NULL, NULL );
        string s = buffer;
        free( buffer );
    
        return s;
    }
    

提交回复
热议问题