Part 5: Using fancy controls

In this stage, we will use an ActiveX control to provide advanced functionality with very little code.


Keeping the window we designed in the last stage, go back to the dialog editor view. (Resources tab on left-hand pane, Dialog -> IDD_VISUAL_C_TUTORIAL_DIALOG)

From the project menu, click on "Add to project" and "Components and controls."  A file browser window should appear, with 2 directories.

Double-click on "Registered ActiveX Components" and a list of components will be displayed.  Many of these are controls, the same as those on the toolbox.  You can find, buy, or download specific controls for almost any application, and the common ones are listed in here.

Find the "Microsoft Common Dialog Control" listed under M, and click "Insert"  This is a general purpose control, that can ask the user for all sorts of useful information.

Accept the standard name for the control, click OK to create it, and click "Close" to get rid of the controls gallery.  You should now see a common dialog control at the bottom of your toolbox.  

Click once on this new control, and again on the form.  A common dialog control will be created. Notice, when you run your program, you can't see this component.  You'll see why in a minute.
 

Associating a variable with the control

To use this control, we need to associate a control variable with it.
Press Control-W to start the MFC ClassWizard, and select the "Member variables" tab
Select the IDC_COMMONDIALOG1 control from the list of controls
Click on "Add variable" and type the name "m_DialogBox"  The variable created will be of type CCommonDialog1, and will be a synonym for the control itself.


 

Using the dialog box

Double-click on your "Files" button to edit the code associated with the button.  Delete the 2 lines of code already there.
Type the following code:

/* Specify the title to display in the dialog box */
 m_DialogBox.SetDialogTitle("Please find a file");

 /* Specify the type of files to look for */
 m_DialogBox.SetFilter("Text files|*.txt|All Files|*.*");

 /* Display the dialog box */
 m_DialogBox.ShowOpen();

Note that as you type the text, the comments automatically get higlighted in green. Keywords become blue, and code that you shouldn't edit becomed grey.

Press F5 to run your program, and when you click on the button, the dialog box should appear, allowing you to select a file from your computer.
 

Using the returned value

The returned value we are particularly interested in is the filename.
Add the following lines to the end of your program:

 /* Specify the title to display in the dialog box */
 m_DialogBox.SetDialogTitle("Please find a file");

 /* Specify the type of files to look for */
 m_DialogBox.SetFilter("Text files|*.txt|All Files|*.*");

 /* Display the dialog box */
 m_DialogBox.ShowOpen();

 /* If the dialog box returned a filename */
 if (m_DialogBox.GetFileName().IsEmpty()==FALSE)
 {
  /* Transfer the filename to the edit box */
  m_strFileName=m_DialogBox.GetFileName();

  /* Update the contents of the edit box */
  UpdateData(FALSE);
 }

When you run your program now, the filename you select should get transferred into the edit box, from where you can cut and paste it to other applications, edit it, delete it, or whatever.
 

Things to note

That's the end of the tutorial, I'm afraid. Visit your local bookstore for a decent book on the subject to learn more, or just try it and look to google when you get stuck

Conclusion