2009-07-10 18 views
3

で複数のファイルを選択し、MFCは、私は複数のファイルにVC++ 6.0でのCFileDialog

CFileDialog opendialog(true); // opens the dialog for open; 
opendialog.m_ofn.lpstrTitle="SELECT FILE"; //selects the file title; 
opendialog.m_ofn.lpstrFilter="text files (*.txt)\0*.txt\0"; //selects the filter; 

if(opendialog.DoModal()==IDOK) //checks wether ok or cancel button is pressed; 
{ 
    srcfilename=opendialog.GetPathName(); //gets the path name; 
    ... 
} 

を選択するコードサンプルは、上記の時点で選択されているだけで1つのファイルを許可しますが、私は複数のテキストファイルを選択したいですたとえば、コントロールキー(ctrl +複数のファイルを選択)を押し続けます。どうすればこれを達成できますか?

答えて

1

マルチ選択を許可するには、OFN_ALLOWMULTISELECTフラグをOpenFileName構造体に渡す必要があります。

+0

私はより明確になりますように、あなたは私の例をすることができます –

8

したがってCFileDialogのコンストラクタでは、 'OFN_ALLOWMULTISELECT'を持つようにdwFlagsパラメータを設定できます。実際に複数のファイル名を取得するには、CFileDialogのm_ofn.lpstrFileメンバを、割り当てたバッファを指すように変更する必要があります。ここを見て:ここで

http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.80).aspx

はそれの使用例は、コメントで十分願って、次のとおりです。

void CMainFrame::OnFileOpen() 
{ 
    char strFilter[] = { "Rule Profile (*.txt)|*.txt*||" }; 

    CFileDialog FileDlg(TRUE, "txt", NULL, OFN_ALLOWMULTISELECT, strFilter); 
    CString str; 
    int nMaxFiles = 256; 
    int nBufferSz = nMaxFiles*256 + 1; 
    FileDlg.GetOFN().lpstrFile = str.GetBuffer(nBufferSz); 
    if(FileDlg.DoModal() == IDOK) 
    { 
     // The resulting string should contain first the file path: 
     int pos = str.Find(' ', 0); 
     if (pos == -1); 
      //error here 
     CString FilePath = str.Left(pos); 
     // Each file name is seperated by a space (old style dialog), by a NULL character (explorer dialog) 
     while ((pos = str.Find(' ', pos)) != -1) 
     { // Do stuff with strings 
     } 
    } 
    else 
     return; 
} 
+0

まだ私は混乱しています、私は明確になっていない午前ありがとう、uは私のコードのサンプルを提供しますしてくださいしてくださいまたは任意の例、または上記の私のコードを変更してくださいありがとう –

+1

DoModal()の後にstr.ReleaseBuffer()を呼び出す必要はありませんか? – Magnus

+1

@Magnus:私は同意します!また 'DoModal'の前に' FileDlg.GetOFN()。nMaxFiles = nMaxFiles'を実行する必要があります。 –

0

は、この行を挿入します。

opendialog.m_ofn.Flags |= OFN_ALLOWMULTISELECT; 

かにフラグを設定しますDeusAduroのようにCFileDialogコンストラクタが実行しました。

3

例:

CString sFilter = _T("XXX Files (*.xxx)|*.xxx|All Files (*.*)|*.*||"); 


CFileDialog my_file_dialog(TRUE, _T("xxx"),NULL, 
          OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT, 
          sFilter, this); 

if (my_file_dialog.DoModal()!=IDOK) 
    return; 

POSITION pos (my_file_dialog.GetStartPosition()); 
while(pos) 
{ 
    CString filename= my_file_dialog.GetNextPathName(pos); 

    //do something with the filename variable 
} 
関連する問題