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.
/* 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.
/* 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.
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