在打开文件(GetOpenFileName( &opf ))时如果要打开多个文件该并把这些文件放入 List Box 中该怎么处理?

解决方案 »

  1.   

    char * sBuffer = new char [6000];CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, szFilter, NULL );
    dlg.m_ofn.lpstrFile = sBuffer;
    dlg.m_ofn.nMaxFile = 6000;
    dlg.m_ofn.lpstrFile[0] = NULL;if (dlg.DoModal()==IDOK)
    {
       POSITION pos = dlg.GetStartPosition();
       while (pos != NULL)
            {
              CString strFileName = dlg.GetNextPathName(pos)  ;
              ...   
            }
    }
    delete [] sBuffer;
      

  2.   

    楼上正解啊,楼主why不用mfc通用对话框呢 OPENFILENAME of;
    char szFileName[MAX_PATH];
    ZeroMemory(szFileName, MAX_PATH);
    ZeroMemory(&of, sizeof(of));
    of.hInstance = AfxGetInstanceHandle();
    of.hwndOwner = this->GetSafeHwnd();
    of.lpstrFile = szFileName;
    of.nMaxFile = MAX_PATH;
    of.lStructSize = sizeof(of);
    of.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER; if (GetOpenFileName(&of))
    {
    AfxMessageBox(of.lpstrFile);
    char *pFileName = &szFileName[of.nFileOffset];
    AfxMessageBox(pFileName);
    }lpstrFile中以NULL为分割符, 包含了所选文件目录, 以及所有的文件名成
      

  3.   

    有没有谁直接调用api做过啊,怎么都是用MFC???
      

  4.   

    借问一下
    dlg.m_ofn.nMaxFile = 6000
    指的是可以打开文件的最大数目吧?
    6000是指的个数吗?
      

  5.   

    captainwh(wh) 谢谢, 我是要显示在在一个Dialog上的  一个 List Box里。做一个列表。
      

  6.   

    dlg.m_ofn.nMaxFile  应该是m_ofn.lpstrFile 的最大长度
      

  7.   

    “lpstrFile中以NULL为分割符, 包含了所选文件目录, 以及所有的文件名“
    虽然MSDN上也是这样说,可是我得到的lpstrfile里只有文件目录,没有文件名?
      

  8.   

    参考:
    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;
    }