Monday, 19 September 2011

MFC File dialog

GUI applications use File dialog to allow user to select file in order to perform some operation on it. MFC offers ready to use CFileDialog class (declared in afxdlgs.h).

It is possible to define a filter for file types that will appear in Files list box. File types are listed in combo box which is located at the bottom of the dialog. Filter is defined with a string made of string couples. First string in a couple is the one that will appear in the dialog's combo box and the second one is the extension itself. All string items are separated with vertical tab and the filter string must end with two vertical tabs.

This code will add two items in file type filter combo box:
 EXE Files (*.exe)
 CAB Files (*.cab)

char pszFilter[] = {"EXE Files (*.exe)|*.exe|CAB Files (*.cab)|*.cab||"};

CFileDialog FileDlg(TRUE, ".exe", NULL, 0, pszFilter);

if(FileDlg.DoModal() == IDOK)
{
   CString strSelectedFileFullPath(FileDlg.GetPathName());
   CString strSelectedFileName(FileDlg.GetFileName());
   ...
}
else
{
   ...
}

If we define filter as:
char pszFilter[] = {"MyApp Setup Files (*.exe;*.cab)|*.exe;*.cab||"};
only exe and cab files will appear in file list box and combo box will have a single item:
   MyApp Setup Files (*.exe;*.cab)

Filter defined as:
char pszFilter[] = {"EXE Files (*.exe)|*.exe|All Files (*.*)|*.*||"};
adds two items to combo box:
   EXE Files (*.exe)
   All Files (*.*)

Two useful methods of this class are:
  • GetPathName() - returns full path to the selected file (including file name)
  • GetFileName() - returns a name of the selected file

CFileDialog allows selecting file either on a local machine or from a shared directory on a network. In the latter case path returned by GetPathName() is a network path (starting with \\remote_host_name\shared_dir\...).

1 comment:

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com