Webocreation

Monday, November 23, 2009

Using Common Dialog Boxes

Lab 9.2: Using Common Dialog BoxesIn this lab, you will add functionality to an application using the custom dialog box.

Estimated time to complete this lab: 15 minutes

To complete the exercises in this lab, you must have the required software. For detailed information about the labs and setup for the labs, see Labs in this course.

Objectives

After completing this lab, you will be able to:

® Use a common font dialog box in an application.
® Use a common color selection dialog box in an application.
® Modify the color and font in CRichEditView.

Prerequisites

There are no prerequisites for this lab.

Exercises

The following exercises provide practice with the concepts and techniques covered in this chapter.
® Exercise 1: Adding a Font Dialog Box
In this exercise, you will add a font dialog box to an application.
® Exercise 2: Adding a Color Selection Dialog Box
In this exercise, you will add a color-choice dialog box to an application.

Exercise 1: Adding a Font Dialog Box

The code that forms the basis for this exercise is in \Labs\Ch09\Lab02\Baseline. Copy these files to your working directory.

In this exercise, you will add a font dialog box to an application. When a user clicks Font on the Context menu, the application will display the common font dialog box, as shown in the following illustration.



Based on the user’s choice, the font will be set in the view pane where the user clicked.

u Add a data member for the current font

— Add the following data member to the CDiffView class by right-clicking the class name in ClassView and selecting Add Member Variable.

CHARFORMAT m_CharacterFormat;



Change the OnEditFont handler to show a font dialog box
1. When you display a font dialog box, you should initialize it to show the current font. Call CRichEditCtrl::GetDefaultCharFormat to get the format of the pane that your user clicked.
GetRichEditCtrl().GetDefaultCharFormat(m_CharacterFormat);
2. The documentation for the CFontDialog constructor describes only one of the two parameter sets. This lab uses an alternate, undocumented constructor as shown in the following function declaration.
CFontDialog::CFontDialog(
const CHARFORMAT& charformat,
DWORD dwFlags = CF_SCREENFONTS,
CDC* pdcPrinter = NULL,
CWnd* pParentWnd = NULL
);

Use this form to construct the dialog box. Use the default parameters.
CFontDialog dlg(m_CharacterFormat);
3. Show the dialog box as a modal dialog box and proceed if the user has chosen a font or style.
if (dlg.DoModal() == IDOK)
4. Get the chosen character formatting.
dlg.GetCharFormat(m_CharacterFormat);
5. Set the character formatting of the pane to the chosen formatting.
GetRichEditCtrl().SetDefaultCharFormat(m_CharacterFormat);
6. Save DiffView.cpp.
7. Build and run the application to test your font dialog code.
The complete function follows.
void CDiffView::OnEditFont()
{
GetRichEditCtrl().GetDefaultCharFormat(m_CharacterFormat);

CFontDialog dlg(m_CharacterFormat);

if (dlg.DoModal() == IDOK)

{
dlg.GetCharFormat(m_CharacterFormat);
GetRichEditCtrl().SetDefaultCharFormat(m_CharacterFormat);
}

}

The completed code for this exercise is in \Labs\Ch09\Lab02\Ex01.

Exercise 2: Adding a Color Selection Dialog Box

Continue with the files you created in Exercise 1, or if you do not have a starting point for this exercise, the code that forms the basis for this exercise is in \Labs\Ch09\Lab02\Ex01.

In this exercise, you will add a color-selection dialog box to an application. When a user clicks either of the color options on the shortcut menu, the application will display the common color dialog box, which is shown in this illustration.



Based on the user’s choice, the text color or background will be set in the pane of ShowDiff in which the user clicked.

Add data members for display colors

1. Add the following data members to the CDiffView class by right-clicking the class name in ClassView and selecting Add Member Variable.

COLORREF m_ForegroundColor;

COLORREF m_BackgroundColor;
2. In the CDiffView class constructor, add the following code to initialize the variables.
m_ForegroundColor = RGB (0, 0, 0);
m_BackgroundColor = RGB (255, 255, 255);

Modify the OnEditColorForeground and OnEditColorBackground handlers
1. Edit the OnEditColorForeground handler. Remove the AfxMessageBox function call.
2. When you display a color dialog box, you should initialize it to show the current color. Call CRichEditCtrl::GetDefaultCharFormat to get the format of the pane that your user clicked.
GetRichEditCtrl().GetDefaultCharFormat(m_CharacterFormat);
3. The CHARFORMAT structure contains a COLORREF member, which holds the pen color for the format. Set this member tom_ForegroundColor.
m_CharacterFormat.crTextColor = m_ForegroundColor;
4. Use this member to construct a CColorDialog object.
CColorDialog dlg(m_CharacterFormat.crTextColor);
5. Show the dialog box as a modal dialog box and proceed if the user has chosen a color.
if (dlg.DoModal() == IDOK)
6. Get the chosen color, set dwEffects to 0, and then place a value in dwMask.
m_CharacterFormat.crTextColor = dlg.GetColor();
m_CharacterFormat.dwEffects = 0;
m_CharacterFormat.dwMask = CFM_COLOR;
If the user creates a custom color, MFC will store that color in your application.
7. Store the chosen color in m_ForegroundColor.
m_ForegroundColor = m_CharacterFormat.crTextColor;
8. Set the character formatting of the pane with the changed color.
GetRichEditCtrl().SetDefaultCharFormat(m_CharacterFormat);
9. The complete OnEditColorForeground function is as shown in the following example code:
void CDiffView::OnEditColorForeground()
GetRichEditCtrl().GetDefaultCharFormat(m_CharacterFormat);
m_CharacterFormat.crTextColor = m_ForegroundColor;
CColorDialog dlg(m_CharacterFormat.crTextColor);
if (dlg.DoModal() == IDOK)
{
m_CharacterFormat.crTextColor = dlg.GetColor();
m_CharacterFormat.dwEffects = 0;
m_CharacterFormat.dwMask = CFM_COLOR;
m_ForegroundColor = m_CharacterFormat.crTextColor;
GetRichEditCtrl().SetDefaultCharFormat(m_CharacterFormat);
}
}
10. Code the OnEditColorBackground function to use the CRichEditView::SetBackgroundColor data member as follows:
void CDiffView::OnEditColorBackground()
{
CColorDialog dlg(m_BackgroundColor);

if (IDOK == dlg.DoModal())

{
m_BackgroundColor = dlg.GetColor();
GetRichEditCtrl().SetBackgroundColor(FALSE, m_BackgroundColor);
}
}
11. Build and run ShowDiff.
The completed code for this exercise is in \Labs\Ch09\Lab02\Ex02.

No comments:

Post a Comment