Knowledge Base Nr: 00019 filedialog.cpp - http://www.swe-kaiser.de

MFC: Beispiel für CFileDialog (single- und multiselection)

  
//singleselection
void CDalliklickDlg::OnBilderlisteStarten()
{
const char* lpszFilter = "ClickCon Bilderlisten (*.ccl)|*.ccl|"
"Alle Files|*.*|"
"|";

CFileDialog dlg(TRUE, "ccl", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, lpszFilter);

if (dlg.DoModal() == IDOK)
{
CString strFilename = dlg.GetPathName();

FILE* fp = fopen(strFilename, "rt");
...
}
}


//multiselection
void CDalliklickDlg::OnDateiImportieren()
{
const char* lpszFilter = "Alle Files|*.jpg;*.jpe;*.jpeg;*.tif;*.tiff;*.png;*.tga;*.pcx|"
"JPEG Files (*.jpg;*.jpe;*.jpeg)|*.jpg;*.jpe;*.jpeg|"
"TIFF Files (*.tif;*.tiff)|*.tif;*.tiff|"
"PNG Files (*.png)|*.png|"
"TGA Files (*.tga)|*.tga|"
"PCX Files (*.pcx)|*.pcx|"
"|";

CFileDialog dlg(TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, lpszFilter, NULL);
/*
To allow the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before calling DoModal.
You need to supply your own filename buffer to accommodate the returned list of multiple filenames.
Do this by replacing m_ofn.lpstrFile with a pointer to a buffer you have allocated,
after constructing the CFileDialog, but before calling DoModal.
Additionally, you must set m_ofn.nMaxFile with the number of characters in the buffer pointed
to by m_ofn.lpstrFile.
*/
const MAXBUFFER = 60*1024;
char* pszBuffer = new char[MAXBUFFER];
ZeroMemory(pszBuffer, MAXBUFFER);
dlg.m_ofn.lpstrFile = pszBuffer;
dlg.m_ofn.nMaxFile = MAXBUFFER;

if (dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
while (pos)
{
CString strCopyFrom, strCopyTo, strFilename;

strCopyFrom.Format("%s", (const char*) dlg.GetNextPathName(pos));

strFilename = strCopyFrom.Mid(strCopyFrom.ReverseFind('\\')+1);

strCopyTo = g_proc.BuildFilename("img-in\\%s", (const char*)strFilename);

BOOL bSucc = ::CopyFile(strCopyFrom, strCopyTo, FALSE); //kopieren und evtl. überschreiben
ASSERT(bSucc);
}
}

delete pszBuffer;
}