打开文件对话框,如何实现可以选取多个文件,然后将这些多个文件的名字和路径添加到一个
列表框中

解决方案 »

  1.   

    To allow the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before calling DoModal. You need to supply your own filename buffer to accommodate the returned list of multiple filenames. Do this by replacing m_ofn.lpstrFile with a pointer to a buffer you have allocated, after constructing the CFileDialog, but before calling DoModal. Additionally, you must set m_ofn.nMaxFile with the number of characters in the buffer pointed to by m_ofn.lpstrFile.Specifies that the File Name list box allows multiple selections. If you also set the OFN_EXPLORER flag, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface. 
    If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the filenames of the selected files. The nFileOffset member is the offset, in bytes or characters, to the first filename, and the nFileExtension member is not used. For Explorer-style dialog boxes, the directory and filename strings are NULL separated, with an extra NULL character after the last filename. This format enables the Explorer-style dialogs to return long filenames that include spaces. For old-style dialog boxes, the directory and filename strings are separated by spaces and the function uses short filenames for filenames with spaces. You can use theFindFirstFile function to convert between long and short filenames.
      

  2.   

    // 设定dwFlags
    DWORD dwFlags = OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT;
    CFileDialog dlg(TRUE, NULL, NULL, dwFlags, FileFilter);
    if (dlg.DoModal()==IDOK)
    {
    POSITION pos=dlg.GetStartPosition();
    while(pos!=NULL)
    {
    CString FileName="";
    FileName = dlg.GetNextPathName(pos);
    // 添加到列表框;
    }
    }