Webocreation

Monday, November 23, 2009

Creating an Instance of the Dialog Box Class

Creating an Instance of the Dialog Box Class
The final step for creating a dialog box is writing the code to create an instance of the dialog box class.

To create an instance of a dialog box class
1. Include the dialog class's header file.
2. Create the dialog box object.
3. Initialize the data members in the dialog box.
4. Display the dialog box.
5. Use the data from the data members in the dialog box.

The include statement follows:
#include "Dialogs.h"

The following code sample shows you how to create, initialize, display, and use the data in a modal dialog box.

void CDialog1View::OnModifyShowdialog()
{
CDialog1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Create the dialog box object
CColorPhraseDlg dlg;
// Load the dialog box's members before
// displaying it.
dlg.m_phrase = pDoc->GetPhrase();
dlg.m_color = RgbToInt(pDoc->GetColor());
// Display the dialog box.
// If the user clicks on OK, replace the
// document's members with the fields from
// the dialog box, and repaint the view.
if (IDOK == dlg.DoModal())
{
pDoc->SetPhrase(dlg.m_phrase);
pDoc->SetColor(IntToRgb(dlg.m_color));
Invalidate();
}
}

Creating

void CDialog1View::OnModifyShowdialog()
{
CDialog1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Create the dialog box object
CColorPhraseDlg dlg;
// Load the dialog box's members before
// displaying it.
dlg.m_phrase = pDoc->GetPhrase();
dlg.m_color = RgbToInt(pDoc->GetColor());
// Display the dialog box.
// If the user clicks on OK, replace the
// document's members with the fields from
// the dialog box, and repaint the view.
if (IDOK == dlg.DoModal())
{
pDoc->SetPhrase(dlg.m_phrase);
pDoc->SetColor(IntToRgb(dlg.m_color));
Invalidate();
}
}

Creating Property Sheets
Property sheets are used for setting the properties of objects within your application. Property sheets are a special kind of dialog box made up of tabbed pages, called property pages, which are displayed one at a time as the user selects tabs at the top. Arranging a large amount of information in groups on a property sheet makes it easier to understand. To see an illustration of a property sheet, click this icon.



Property sheets consist of two main parts: the containing dialog box, and one or more property pages. You create property sheets by using the CPropertySheet object.

If you do not want to use the default CPropertySheet object, you can also create a dialog box by using the Dialog editor. You can then create a class for it and derive the class from CPropertySheet. The following procedure shows you how to use the derived class method.

To create property pages
1. In ResourceView, right-click the Dialog folder and then click Insert.
2. To invoke the Dialog editor, in the Insert Resource dialog box, expand the Dialog folder and double-click IDD_PROPPAGE_LARGE, IDD_PROPPAGE_MEDIUM or IDD_PROPPAGE_SMALL.
3. Add controls to the property page.
4. Create a class for each dialog box that is derived from CPropertyPage.
ClassWizard will automate this process, but be sure to set the base class to CPropertyPage.


To create a property sheet
1. Create the property page objects.
2. Create a CPropertySheet object that will contain the property page objects.
3. Add the property page objects. Be sure to add the largest page first because it will set the overall size of the dialog box.
The tabs for the top of each property sheet are inserted by the CPropertySheet::AddPage method. See the sample code at the end of this topic for details.
4. If you want the property sheet to be deployed as a modal dialog box, use DoModal. Modeless property sheets are created and deployed like ordinary modeless dialog boxes.

Note If the user of the application clicks the OK button, the property sheet will extract the values from the individual property page objects.

To see sample code that shows how to create a property sheet, click this icon.
void CTabbedColorPhraseView::OnModifyShowtabbeddialogbox()
{
CTabbedColorPhraseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Create each property page object and initialize
// as appropriate.
CPhraseTab phraseTab;
phraseTab.m_phrase = pDoc->GetPhrase();
CColorTab colorTab;
colorTab.m_color = pDoc->GetColor();
// Create the property sheet object and give it a
// title.
CPropertySheet cps("Modify Phrase or Color");
// Add the largest property page first.
cps.AddPage(&phraseTab);
cps.AddPage(&colorTab);
// Since this is being displayed modally instead of
// modelessly, remove the Apply button, which
// appears by default on Property Sheets.
cps.m_psh.dwFlags |= PSH_NOAPPLYNOW;
// If the user clicks on OK and either color or
// phrase has changed, update the document and
// invalidate the view.
if (IDOK == cps.DoModal())
if (pDoc->GetPhrase() != phraseTab.m_phrase ||
pDoc->GetColor() != colorTab.m_color)
{
pDoc->SetColor(colorTab.m_color);
pDoc->SetPhrase(phraseTab.m_phrase);
Invalidate();
}
}

For information about style considerations for property sheets, see The Windows Interface Guidelines for Software Design.

Using Common Dialog Boxes

The Common Dialog Box Library (CommDlg.dll) contains a set of dialog boxes for performing routine tasks, such as opening files and printing documents. The common dialog boxes provide a uniform user interface that helps users carry out these routine tasks without having to learn new techniques with each application.

The following table describes the dialog boxes in the Common Dialog Box Library and provides the names of the base classes.

If you want to Use

Choose standard and custom colors CColorDialog

Open, save, and save as a file CFileDialog
Find and replace text CFindReplaceDialog
Select fonts CFontDialog
Set page setup options CPageSetupDialog
Set print options CPrintDialog
Common dialog boxes offer a large amount of prewritten functionality for your applications.

To use a common dialog box
1. Create an object of the appropriate class; for example, CFontDialog.
2. Modify the appropriate data member, or use accessor functions in the class to query or set property values.
3. Modify the code in the appropriate handler to invoke DoModal from the dialog box class object, if it is a modal dialog box. The only modeless common dialog box is CFindReplaceDialog.

No comments:

Post a Comment