Browse DevX
Sign up for e-mail newsletters from DevX



Create Adaptable Dialog Boxes in MFC  : Page 3

Adaptable dialog boxes can maximize code maintainability.

View Text
The last of the custom screens, View Text, functions similarly to Edit Text--with a couple of differences. It displays any previously entered text, but the user is not allowed to edit it. The Print button is now visible and functional (see Figure 6). The class declaration and implementation of CViewTextDlg contain the messaging code for the Print button (see Listings 7 and 8). Again, the dialog creator sets the text in the dialog by calling SetText(), which is implemented in CViewTextDlg. If the user hits Print, it displays the message box shown in Figure 7.

Figure 6. Adding Options and Restrictions: From here, you can view the text, but the edit control does not allow text editing. You can print now, so the Print button is visible. Figure 7. The "Print" Dialog: The message box located in the print button handler in CViewTextDlg is activated when the user hits the Print button.

Bring It All Together
The main screen, shown in Figure 1, ties it all together. AppWizard created most of the code for it, so I am not going to show that portion. I'll show just the code I added (see Listing 9). CPolymorphicDialogBoxExampleDlg is the name of the class I used for the main screen. The header file contains the following variable declaration to create a holding place for what the user typed/edited in the screens:

class CPolymorphicDialogBoxExampleDlg : 
public CDialog

    CString DlgText;


The three functions you see in Listing 9 for the "New," "Edit," and "View" buttons are the event handlers I set up using Class Wizard. Notice how they use CreateDlg() to create the custom screens.

This program is just one example of what you can do with MFC customization. You can customize MFC in all sorts of ways to get the look and functionality that you want-and you don't have to sacrifice object-orientation to do it.

Previous Page  

Mark Miller is a professional software developer who has worked in the field for more than five years. He began his career working in C on DOS and Unix and then switched to C++ and MFC programming on Windows in 2000.
Thanks for your registration, follow us on our social networks to keep up-to-date