在打开文件对话框中全选时iResult总是返回2,看msdn说可能是nMaxFile需要设置,不知道怎么改请高手指教。谢谢!
代码如下:
CFileDialog fDlg(TRUE);
fDlg.m_ofn.lpstrTitle = "Select image file";
fDlg.m_ofn.Flags |= OFN_ALLOWMULTISELECT;
static char BASED_CODE szFilter[] = "BMP (*.bmp)\0*.bmp\0JPEG (*.jpg;*.jpeg)\0*.jpg; *.jpeg\0ICON (*.ico)\0*.ico\0GIF (*.gif)\0*.gif\0Exif (*.exif)\0*.exif\0PNG (*.png)\0*.png\0TIFF (*.tiff)\0*.tiff\0WMF (*.wmf)\0*.wmf\0EMF (*.emf)\0*.emf\0All support format\0*.bmp; *.jpg; *.jpeg; *.ico; *.gif; *.exif; *.png, *.tiff; *.wmf; *.emf\0All Files (*.*)\0*.*\0\0";
fDlg.m_ofn.lpstrFilter =  szFilter;
fDlg.m_ofn.nFilterIndex = 10;
int iResult = fDlg.DoModal();
if (IDOK == iResult)
{
POSITION pos;
pos = fDlg.GetStartPosition();
while(pos != NULL)
{
CString strFile;
strFile = fDlg.GetNextPathName(pos);
do something ...
}
}

解决方案 »

  1.   

    使用CFileDialog类并设置OFN_ALLOWMULTISELECT标志时,OPENFILENAME结构体的lpstrFile成员是一个指向用户申请分配的缓冲区,里面接受所选的路径和文件名列表,这个列表的每一项由一个NULL隔开,最末以两个NULL结束。nMaxFile成员指明了缓冲区的大小,如果所选择的文件名的加起来的总长度超过了此值,则DoModal函数返回IDCANCEL,如果用户试图打开超过缓冲区大小的文件集的话,CommDlgExtendedError()将返回FNERR_BUFFERTOOSMALL,这时候,lpstrFile的前两个byte将会包含实际需要的缓冲区的字节数。如果是在Windows NT 4.0下,能被拷贝到lpstrFile缓冲区的字节数被限制在2562字节以内,如果超过将被截断。以下是一个使用该类进行多文件名选择的例子,希望能对您有所帮助。#include "cderr.h" //for definition of FNERR_BUFFERTOOSMALL      CFileDialog   dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL );
          DWORD MAXFILE = 4000
          dlg.m_ofn.nMaxFile = MAXFILE;
          char* pc = new char[MAXFILE];
          dlg.m_ofn.lpstrFile = pc;
          dlg.m_ofn.lpstrFile[0] = NULL;      int iReturn = dlg.DoModal();
          if(iReturn ==  IDOK)
          {
             int nCount = 0;
             POSITION pos = dlg.GetStartPosition();
             while (pos != NULL)
             {
                dlg.GetNextPathName(pos);
                nCount++;
             }
             CString str;
             str.Format("Successfully opened %d files\n", nCount);
             AfxMessageBox(str);
          }
          else if(iReturn == IDCANCEL)
             AfxMessageBox("Cancel");      if(CommDlgExtendedError() == FNERR_BUFFERTOOSMALL)
             AfxMessageBox("BUFFERTOOSMALL");
          delete []pc;
    如果需要进一步的信息,您可以参看:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cfiledialog.asp以及Q179372等有关信息。
      

  2.   

    偶Copy自己的项目代码给楼主作参考,记得偶得好噢~~~~~~~void CGeoAnView::OnLayerAddin() 
    {
    // TODO: Add your command handler code here
    char szFilter[]="Shape文件(*.shp)|*.shp|所有文件(*.*)|*.*||";
    CFileDialog dlg(TRUE,"shp", _T("*.shp"),
    OFN_HIDEREADONLY |  OFN_ALLOWMULTISELECT,
    szFilter,this);
    CString strFileNames;
    dlg.m_ofn.lpstrFile=strFileNames.GetBuffer(2048);
    dlg.m_ofn.nMaxFile=2048;
    if(dlg.DoModal()==IDOK)
    {
    POSITION pos = dlg.GetStartPosition();
    while (pos)
    {
    CString strFilePath=dlg.GetNextPathName(pos);
    // find file path name yes or no in the list
    //
    m_MoProject.AddLayer(strFilePath);
    }
    RefreshLayerDlg();
    RefreshSearchDlg();
    }
    }