就象mp3播放器一样,可以一次选择多个文件并放入一个List Box中。我的OPENFILENAME结构如下:
 OPENFILENAME  ofn;

ofn.lStructSize       = sizeof(OPENFILENAME);
ofn.hwndOwner         = NULL;     //m_hWnd;
ofn.hInstance         = g_hInst;
ofn.lpstrFilter       = filter;    //"*.dvr";  // filter;  //"*.dvr";
ofn.lpstrCustomFilter = NULL;     //"*.dvr";
ofn.nMaxCustFilter    = 0;
ofn.nFilterIndex      = 0;
ofn.lpstrFile         = file;     //lpstrFile; //NULL;    //szFile;
ofn.nMaxFile          = sizeof(file);
ofn.lpstrFileTitle    = NULL;
ofn.nMaxFileTitle     = 0;
ofn.lpstrInitialDir   = NULL; char title[64] = {0};
LoadString( g_hInst, IDS_OPENFILETITLE, title, sizeof(title) );
ofn.lpstrTitle        = title; ofn.nFileOffset       = 0;
ofn.nFileExtension    = 0;
ofn.lpstrDefExt       = NULL;
ofn.lCustData         = NULL;    //(LPARAM)&sMyData;
ofn.lpfnHook    = NULL;
ofn.lpTemplateName    = NULL;
ofn.Flags             = OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_FILEMUSTEXIST;
        GetOpenFileName( &ofn );
当我选一个文件时,file的内容是该文件的完全地址。但当我选多个文件时,他只会返回这些文件的路径,不包括文件名。这样是否可以?如果是这样,我该怎么把我选择的多个文件加入到List Box?

解决方案 »

  1.   

    CString strFileName;
    CFileDialog dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL );
    DWORD MAXFILE = 2412; // allocate enough memory space
    dlg.m_ofn.nMaxFile = MAXFILE; // set the buffer size
    char* buf = new char[MAXFILE];
    dlg.m_ofn.lpstrFile = buf;
    dlg.m_ofn.lpstrFile[0] = NULL; 
    int iReturn = dlg.DoModal();
    if(iReturn == IDOK)
    {
    POSITION pos = dlg.GetStartPosition();
    while (pos != NULL)
    {
    strFileName=dlg.GetNextPathName(pos); // get the individual file name
    // do some operations here
    }
    }
    else if(iReturn == IDCANCEL) AfxMessageBox("Cancel");delete [] buf;
      

  2.   

    参考:
    void CMyPlayerDlg::OnButtonOpen() 
    {
    // TODO: Add your control notification handler code here
    CString strFilter = "Mp3 File(*.mp3)|*.mp3|";
    strFilter += "MPEG File(*.mpg;*.mpeg)|*.mpg;*.mpeg|";
    strFilter += "Wave File(*.wav)|*.wav|";
    strFilter += "AVI File(*.avi)|*.avi|";
    strFilter += "All File(*.*)|*.*|";
    CFileDialog dlgOpen(TRUE,NULL,NULL,OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,strFilter,this);
    DWORD MAXFILE = 2412; // allocate enough memory space
    dlgOpen.m_ofn.nMaxFile = MAXFILE; // set the buffer size
    CString strFileName;
    char* buf = new char[MAXFILE];
    dlgOpen.m_ofn.lpstrFile = buf;
    dlgOpen.m_ofn.lpstrFile[0] = NULL; 
    if(IDOK == dlgOpen.DoModal())
    {
    POSITION pos = dlgOpen.GetStartPosition();
    while (pos != NULL)
    {
    strFileName = dlgOpen.GetNextPathName(pos); 
    // get the individual file name
    m_SourceFileList.InsertString(m_SourceFileList.GetCount(),strFileName);// add to Mp3 ListBox
    }
    }
    delete [] buf;
    }
      

  3.   

    http://www.vccode.com/file_show.php?id=2003
      

  4.   

    这是 flag设置 OFN_EXPLORER 时 的情况,如果不设OFN_EXPLORER,file里包含了完全路径和文件名。
      

  5.   

    TCHAR str[10000]= _T("");
        CString s, tmp;
        int nImageNumber = 0;
        CString *path;
        CString Filter = "DICOM Files (*.bmp)|*.BMP|All Files (*.*)|*.*|";
        CFileDialog OpenFile(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_LONGNAMES|OFN_ALLOWMULTISELECT|OFN_EXPLORER, (LPCTSTR)Filter);
        OpenFile.m_ofn.lpstrTitle = _T("Open Files to Print ");
        OpenFile.m_ofn.nMaxFile = 10000;//the max system length is 1024, this is the selfdefined length 
        OpenFile.m_ofn.lpstrFile = str;//buffer for the selfdefined length
        if(OpenFile.DoModal() == IDOK)
        {
            path = new CString [600];
            s = "You had Selected :\n";
            POSITION pos = OpenFile.GetStartPosition();
            while (pos)
            {
                path[nImageNumber] = OpenFile.GetNextPathName(pos);
                nImageNumber ++;
            }
            m_PrtOption.nImageNum = nImageNumber;
            //s.Format("%d", m_PrtOption.nImageNum);
            m_PrtOption.FilePath = new CString[nImageNumber];
            for(int ii = 0; ii< nImageNumber; ii ++)
            {
                m_PrtOption.FilePath[ii] = path[ii];
                s += m_PrtOption.FilePath[ii];
                s += '\n';
            }
            tmp.Format("%d", nImageNumber);
            s += "You had Opened " + tmp + " Files" + '\n';
            delete []path;
        }
        else
        {
            s = "Has been Canclled !";
        }
        AfxMessageBox(s);
      

  6.   

    CFileDialog dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL );
    INT_PTR iReturn = dlg.DoModal();
    CString strFileNmae;
    if(iReturn == IDOK)
    {
       POSITION pos = dlg.GetStartPosition();
       while (pos != NULL)
       {
          strFileNmae = dlg.GetNextPathName(pos);
          //这里对strFileNmae进行处理
          AfxMessageBox(strFileNmae);
       }
    }