[MFC] CFileDialog: cum fac selectie multipla?

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi

[MFC] CFileDialog: cum fac selectie multipla?

Post by Ovidiu Cucu » 17 Jun 2012, 11:31

Intrebare
Cum fac un dialog File Open care permite selectie multipla de fisiere?

Raspuns
In primul rand, se seteaza flag-ul OFN_ALLOWMULTISELECT in constructorul lui CFileDialog sau inainte de a apela CFileDialog::DoModal.
Acest lucru nu este suficient pentru ca numele fisierelor (full path and file name) vor fi intoarse toate intr-un buffer care are alocat MAX_PATH (260) caractere, in general prea putin. Deci, trebuie setat membrul lpstrFile al structurii OPENFILENAME cotinuta in CFileDialog sa pointeze la un buffer suficient de mare.
De asemenea, trebuie setat si membrul nMaxFile cu lungimea buffer-ului actual.
In fine, numele fisierelor selectate se extrag apeland CFileDialog::GetStartPosition si CFileDialog::GetNextPathName.

Exemplu

Code: Select all

   CFileDialog dlgOpenFile(TRUE);
   // add OFN_ALLOWMULTISELECT flag
   dlgOpenFile.GetOFN().Flags |= OFN_ALLOWMULTISELECT;

   try
   {
      // set a buffer to keep at least 100 full path and file names
      const int nBufferSize = 100 * (MAX_PATH + 1) + 1;
      CString strBuffer;
      LPTSTR pBuffer = strBuffer.GetBufferSetLength(nBufferSize);
      dlgOpenFile.GetOFN().lpstrFile = pBuffer;
      dlgOpenFile.GetOFN().nMaxFile = nBufferSize;

      // show modal Open File dialog
      if(IDOK == dlgOpenFile.DoModal())
      {
         // get selected files
         POSITION pos = dlgOpenFile.GetStartPosition();
         while(NULL != pos)
         {
            CString strFilePath = dlgOpenFile.GetNextPathName(pos);
            // ... do something with strFilePath.
         }
      }
      // release buffer
      strBuffer.ReleaseBuffer();
   }
   catch(CException* e)
   {
      e->ReportError();
      e->Delete();
   }
Resurse
<< Back to MFC index



Post Reply