Tag: CFileDialog
MFC CFileDialog using filters
by paulorb on mar.20, 2017, under C++, MFC
This is a class that is commonly used when working with MFC dialogs,
Using CFileDialog consists of following steps:
1. Create an instance of CFileDialog
2. Set or Modify m_ofn structure.
3. Call DoModal function of CFileDialog
4. When DoModal returns, we can call GetPathName() function to retrieve the selected filename.
First lets create the simple CFiledialog
TCHAR szFilters[] = _T("Text Files (*.dat)|*.NC|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"); // Create an Open dialog; the default file name extension is ".my". CFileDialog fileDlg(TRUE, _T("txt"), _T("*.txt"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters, this); // Display the file dialog. When user clicks OK, fileDlg.DoModal() // returns IDOK. if (fileDlg.DoModal() == IDOK) { }
Now lets think about the filters, first example we will filter *.txt *.dat and *.* file formats:
TCHAR szFilters[] = _T("Doc Files (*.pdf;*.txt;*.dat)||")
This will be one item in the dropdown of the CFileDialog, if we want multiple items we can create something like this:
TCHAR szFilters[] = _T("Text Files (*.pdf;*.jpg)|*.pdf;*.jpg|") _T("PDF Files (*.pdf)|*.pdf||");
In this case we will have one item described as “Text files” with filter *.pdf;*.jpg and the second item on the dropdown will be described as “PDF Files” and will have a filter for *.pdf