The Resource View of the MFC - Dialog Boxes (Page 3 of 5 )
Dialog boxes are mostly used to interact with the user; they are called Dialog Boxes for this very reason. Initially, their purpose was to assure communication with the client/user. Of course, they can also stand by themselves as a true application, but that is a little more complicated and exceeds the goal of this series. Also, a Dialog box is modular by default; in plain English, it means that while you have a dialog box opened, you cannot switch to any other window in the application.
Sometimes you will need to switch between a dialog box and another client window; this can be realized by implementing a non-modular dialog box. I won't cover this part because this article is intended as an introduction, but don't hesitate to explore this more deeply should you find it intriguing. Google is your friend, don't forget that!
Our goal is to create a dialog box that will get a text from the user and draw that text in the client area. Then, as an extra, we say that a blue background can be used. Also, we shall encapsulate this in the Enter Data... menu. You have already made up a Dialog box if you open the Dialog part of the resource tree. This represents the Dialog box that appears when you push down the About key in the menu. But don't rush ahead. We'll start with the creation process. Follow the path: Resource View -> Right Click on the Tree view -> Add Resource... -> Dialog -> New. If you've done it correctly, you should have something like this:

Now we can start customizing some part of it. In the Property page, you'll find a multitude of ways you can change the style of the Dialog box. We won't mess with that part yet, so you can comprehend the essence of the remaining part.
First of all, just make sure that the Toolbox is present in your Visual Studio. If the answer is affirmative, then we'll add an Edit Control, a Check Box, and a Static text. You shouldn't have a problem with this part because it is a simple and straightforward process. Just select the proper option from the Toolbox and drag it into the box. Follow this process via some resizing until you get something like what's in the attached screen shot below.

Change the captions for the Static text and the check box as I did in the above image. Finally, we can include the dialog box into our project by right clicking on the Dialog box and choosing "Add a box." Enter a class name for the Dialog box and press Finish. The code is generated and we can start with the needed modifications. The name I have chosen is CTutorial. Also notice that we have this Dialog ID assigned: IDD_DIALOG1.

We assign a variable to the data entered in the CEdit control. Simply right click on the control in the resource view and select Add variable. Make sure you select the options as shown in the picture below. The image is pretty self-explanatory, so I won't discuss it in detail. Repeat the procedure for the check box - you add an m_checkbox bool variable.

As this is done, we can integrate our creation into the application. Go to the place where you added the OnDataoptionsEnterdata on the previous page and add the following line to the corresponding CPP:
#include "Tutorial.h"
This simple step creates our customized Dialog Box when we press the Enter Data... menu and proceed to add the following code at the end:
void CMFC_exampleView::OnDataoptionsEnterdata()
{
CTutorial dialog;
dialog.m_data = CString("Enter data");
dialog.m_checkbox = _useBlue;
INT_PTR result = dialog.DoModal();
if( result == IDOK)
{
this->_print = dialog.m_data;
this->_useBlue = dialog.m_checkbox;
this->Invalidate();
}
}
We declared a CString type _print variable in the CMFC_exampleView that stores the value we got from the Dialog box. For some on-screen result, add the following code in the OnDraw:
if(_useBlue)
{
pDC->SetBkColor(RGB(0,0,255));
}
pDC->TextOutW(0,20,_print);
Yes, we are done for now. You can start the application and see the results for yourself. I hope you enjoy it. The following page will finish up the resources.
Next: Toolbar, Hot-Keys and Icons >>
More BrainDump Articles
More By Gabor Bernat