Software Design Using C++Message, Dialog, and Edit Boxes in Visual C++Message BoxesYou can pop up a message box anytime you like just by using the AfxMessageBox function.
Look up this function in Visual Studio's Help section to see in detail what it can do.
For example, to show an information message in a message box with an OK button, you
might use:
The first parameter is the message to be displayed. You can use either a character array type of
string or a CString. The second parameter contains various style constants combined with OR.
Almost the same message box can be created by using
Here is a variation on the above example. The
You can also check the return value of
Dialog Boxes in GeneralThe AppWizard section of these web pages (under the multiple document interface links) showed how your program can bring up a dialog box of your own design. In that example the dialog box was brought up when the user clicked on a certain menu item. The basic steps in setting this up were to add a dialog resource, add a class with base class CDialog as our interface to the dialog box,
and then in our code create an object of this class and call the DoModal function on it.
In the particular example mentioned, this code amounted to the following, where the new class
happened to be named CSizeDlg and the dialog box contained an edit box that
had associated with it the variable m_FileSize .
There are also a number of common dialog boxes that are available for your use. For example,
there is a font dialog box (class File Open Dialog BoxesA file dialog box can be used either as a file open box or a file save box. Which you get is decided by using a TRUE or FALSE
as the parameter to the class constructor. TRUE gives
a file open dialog box. The example below shows the creation and display of a file open dialog box.
The class function GetPathName is then used to get the path name of the file selected by the user.
Our example places this pathname into an edit box, though what you do with the pathname
is up to you.
File Save Dialog BoxesThe following is a similar example showing how to create a file save dialog box. The only difference is the use of FALSE as the parameter to the constructor. Once again the complete
pathname was obtained and displayed in an edit box (just to keep the example simple).
Folder Selection Dialog BoxesThis one is considerably different and will not be completely explained here. The basic idea is that a BROWSEINFO structure is used to hold information
needed by the SHBrowseForFolder function, which does the bulk of the work. The
SHGetPathFromIDList is used on the return value from the previous function
in order to get the full path to the selected folder. The example below displays this
full path in an edit box, but you can do whatever you like with this data. Note that
there is a lot more that can be done with the SHBrowseForFolder function.
Look it up under Help in Visual Studio if you want to find out more.
The above assumes that we have set up a type called
Edit BoxesIf you have been reading the AppWizard section of these web pages, you have already seen how to place an edit box into a dialog box. That dialog box might be the one that provides the interface for the application or an application with a single or multiple document interface might bring up the dialog box. (The former is shown on the above-mentioned page under the link for the dialog-based interface. The latter is shown under the link for the multiple document interface.)
Here is something that you might not realize is possible with an edit box:
You can place multiple lines of data into an edit box. Go to
properties for the edit box and turn on the multiline style as well as autoscroll, both
horizontal and vertical, as well as regular horizontal and vertical scroll.
Then, for example, if you want to copy all of the data from a text file into your
edit box, you might use something such as the following function which has been added to the
Practice ExerciseFor practice try this out, consulting the AppWizard section of these web pages as necessary when doing this: Create a simple dialog-based application that has 3 buttons on it. One button should allow the user to open a text file. The contents of the file should then be displayed in an edit box as described above. (That gives practice with the file save dialog and with multiline data in an edit box. For a shorter practice exercise, don't really open the file and put the data into an edit box. Entirely skip the edit box. Rather, just pop up a message box that reports what file was selected to be opened. No reading of the data is really necessary if you just want to try out the file save dialog box.) The next button should pretend to let the user save the contents of this edit box to a (possibly new) file. (If you wish, you can actually save the data to the file selected, but it is suggested that you keep the exercise short and just use a message box to report the file selected but not save any data to this file.) The third button should allow the user to change to a different directory. Change to this new directory and then look up the current directory by using:
This assumes that StringType has been set up as explained above
under the Folder Selection Dialog Boxes topic. It further assumes that you have included
the direct.h header, probably by placing it in the .h file corresponding to your .cpp file.
(This header is needed if you want to use the _chdir and _getcwd functions.)
Next pop up a message box to report the current directory (now found in the variable
CurrentDir ). (Should you wish to keep the exercise shorter, just report
what FullPath contains, via a message box, and skip changing directory and looking
up the current directory.) If you need further help or just want to check your work,
look at the following two links. Note that these two files are for Visual Studio 6.0, though
the code that you need to write is the same if you use Visual Studio .NET.
Back to the main page for Software Design Using C++ |