Home Index  | Back

New Page 1
Lesson 1 -Building Your First Windows Application

1.1 : Creating a Windows Application

1.2 : Creating a Dialog-Based Application

1.3 : Creating DLLs, Console Applications, and More

1.4 : Changing Your AppWizard Decisions
 1.5 : Understanding AppWizard’s Code
 1.6 : Understanding a MDI Application
 1.7 : Understanding the Components of a Dialog-Based Application

Lesson 2 - Dialogs and Controls

2.1 : Understanding Dialog Boxes

2.2 : Creating a Dialog Box Resource

 2.3 : Writing a Dialog Box Class
 2.4 : Using the Dialog Box Class
Lesson 3 - Messages and Commands

3.1 : Understanding Message Routing

3.2 : Understanding Message Loops

 3.3 : Reading Message Maps
 3.4 : Learning How ClassWizard Helps You Catch Messages
 3.5 : Recognizing Messages
 3.6 : Understanding Commands
 3.7 : Understanding Command Updates
 3.8 : Learning How ClassWizard Helps You Catch Commands and Command Updates

Lesson 4 - Documents and Views

4.1 : Understanding the Document Class

4.2 : Understanding the View Class

4.3 : Creating the Rectangles Application

 4.4 : Other View Classes

4.5 : Document Templates, Views, and Frame Windows

Lesson 5 - Drawing on the Screen

5.1 :Understanding Device Contexts

 5.2 : Introducing the Paint1 Application
 5.3 : Building the Paint1 Application
 5.4 : Scrolling Windows
 5.5 : Building the Scroll Application
Lesson 6 - Building a Complete Application: ShowString

6.1 : Building an Application That Displays a String

 6.2 : Building the ShowString Menus
 6.3 : Building the ShowString Dialog Boxes
 6.4 : Making the Menu Work
 6.5 : Making the Dialog Box Work
 6.6 : Adding Appearance Options to the Options Dialog Box
Lesson 7 -  Status Bars and Toolbars

7.1 : Working with Toolbars

 7.2 : Working with Status Bars
Lesson 8 - Common Controls

8.1 : The Progress Bar Control

 8.2 : The Up-Down Control
 8.3 : The Image List Control
 8.4 : The List View Control
 8.5 : The Tree View Control
 8.6 : The Rich Edit Control
 8.7 : The Date Picker Control
 8.8 : Month Calendar Control
 8.9 : Scrolling the View
Lesson 9 - Property Pages and Sheets

9.1 : Introducing Property Sheets

 9.2 : Creating the Property Sheet Demo Application
 9.3 : Running the Property Sheet Demo Application
Lesson 10 - ActiveX Concepts

10.1 : The Purpose of ActiveX

10.2 : Object Linking

10.3 : Object Embedding

 10.4 : Containers and Servers
 10.5 : Toward a More Intuitive User Interface
 10.6 : The Component Object Model  

10.7 : Automation

 10.8 : ActiveX Controls

Lesson 11 -  Building an ActiveX Control

11.1 : Creating a Rolling-Die Control

11.2 : Displaying the Current Value

11.3 : Reacting to a Mouse Click and Rolling the Die 

 11.4 : Creating a Better User Interface
 11.5 : Generating Property Sheets
Lesson 12 - Database Access

12.1 : Understanding Database Concepts

12.2 : Creating an ODBC Database Program

 12.3 : Choosing Between ODBC and DAO
 12.4 : OLE DB

Lesson -6:Building a Complete Application: ShowString

6.4- Making the Menu Work

When the user chooses Tools, Options, the Options dialog box should display. You use ClassWizard to arrange for one of your functions to be called when the item is chosen, and then you write the function, which creates an object of your dialog box class and then displays it.

6.4.1 The Dialog Box Class

ClassWizard makes the dialog box class for you. While the window displaying the IDD_OPTIONS dialog box has focus, choose View, ClassWizard. ClassWizard realizes there is not yet a class that corresponds to this dialog box and offers to create one, as shown in Figure 6.10.


FIG. 6.10 Create a C++ class to go with the new dialog box.

Leave Create a New Class selected and then click OK. The New Class dialog box, shown in Figure 6.11, appears.


FIG. 6.11 The dialog box class inherits from CDialog.

Fill in the dialog box as follows:

1. Choose a sensible name for the class, one that starts with C and contains the word Dialog; this example uses COptionsDialog.

2. The base class defaults to CDialog, which is perfect for this case.

3. Click OK to create the class.

The ClassWizard dialog box has been waiting behind these other dialog boxes, and now you use it. Click the Member Variables tab and connect IDC_OPTIONS_STRING to a CString called m_string, just as you connected controls to member variables in previous Unit. Click OK to close ClassWizard.

Perhaps you’re curious about what code was created for you when ClassWizard made the class. The header file is shown in Listing 6.5.

Listing 6.5 OPTIONSDIALOG.H—Header File for COptionsDialog

// OptionsDialog.h : header file

//

///////////////////////////////////////////

// COptionsDialog dialog

class COptionsDialog : public CDialog

{

// Construction

public:

COptionsDialog(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(COptionsDialog)

enum { IDD = IDD_OPTIONS };

CString m_string;

//}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(COptionsDialog)

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

// Generated message map functions

//{{AFX_MSG(COptionsDialog)

// NOTE: The ClassWizard will add member functions here

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

There are an awful lot of comments here to help ClassWizard find its way around in the file when the time comes to add more functionality, but there is only one member variable, m_string; one constructor; and one member function, DoDataExchange(), which gets the control value into the member variable, or vice versa. The source file isn’t much longer; it’s shown in Listing 6.6.

Listing 6.6 OPTIONSDIALOG.CPP—Implementation File for COptionsDialog

// OptionsDialog.cpp : implementation file

//

#include "stdafx.h"

#include "ShowString.h"

#include "OptionsDialog.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

//////////////////////////////////////////////

// COptionsDialog dialog

COptionsDialog::COptionsDialog(CWnd* pParent /*=NULL*/)

: CDialog(COptionsDialog::IDD, pParent)

{

//{{AFX_DATA_INIT(COptionsDialog)

m_string = _T("");

//}}AFX_DATA_INIT

}

void COptionsDialog::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(COptionsDialog)

DDX_Text(pDX, IDC_OPTIONS_STRING, m_string);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(COptionsDialog, CDialog)

//{{AFX_MSG_MAP(COptionsDialog)

// NOTE: The ClassWizard will add message map macros here

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

The constructor sets the string to an empty string; this code is surrounded by special ClassWizard comments that enable it to add other variables later. The DoDataExchange() function calls DDX_Text() to transfer data from the control with the resource ID IDC_OPTIONS_STRING to the member variable m_string, or vice versa. This code, too, is surrounded by ClassWizard comments. Finally, there is an empty message map because COptionsDialog doesn’t catch any messages.

6.4.2 Catching the Message

The next step in building ShowString is to catch the command message sent when the user chooses Tools, Options. There are seven classes in ShowString: CAboutDlg, CChildFrame, CMainFrame, COptionsDialog, CShowStringApp, CShowStringDoc, and CShowStringView. Which one should catch the command? The string and the options will be saved in the document and displayed in the view, so one of those two classes should handle the changing of the string. The document owns the private variable and will not let the view change the string unless you implement a public function to set the string. So, it makes the most sense to have the document catch the message.

NOTE: Often the hardest part of catching these messages is deciding which class should catch them. The decision between View and Document is frequently a very difficult one. If the message handler will need access to a private member of either class, that’s the class to catch the message.

To catch the message, follow these steps:

1. Open ClassWizard (if it isn’t already open).

2. Click the Message Maps tab.

3. Select CShowStringDoc from the Class Name drop-down list box.

4. Select ID_TOOLS_OPTIONS from the Object IDs list box on the left, and select COMMAND from the Messages list box on the right.

5. Click Add Function to add a function to handle this command.

6. The Add Member Function dialog box, shown in Figure 6.12, appears, giving you an op-portunity to change the function name from the usual one. Do not change it; just click OK.


FIG. 6.12 ClassWizard suggests a good name for the message-catching function.

Click Edit Code to close ClassWizard and edit the newly added function. What happened to CShowStringDoc when you arranged for the ID_TOOLS_OPTIONS message to be caught? The new message map in the header file is shown in Listing 6.7.

Listing 6.7 SHOWSTRINGDOC.H—Message Map for CShowStringDoc

// Generated message map functions

protected:

//{{AFX_MSG(CShowStringDoc)

afx_msg void OnToolsOptions();

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

This is just declaring the function. In the source file, ClassWizard changed the message maps shown in Listing 6.8.

Listing 6.8 SHOWSTRINGDOC.CPP—Message Map for CShowStringDoc

BEGIN_MESSAGE_MAP(CShowStringDoc, CDocument)

//{{AFX_MSG_MAP(CShowStringDoc)

ON_COMMAND(ID_TOOLS_OPTIONS, OnToolsOptions)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

This arranges for OnToolsOptions() to be called when the command ID_TOOLS_OPTIONS is sent. ClassWizard also added a skeleton for OnToolsOptions():

void CShowStringDoc::OnToolsOptions()

{

// TODO: Add your command handler code here

} 

Next>>
 
© Dewsoft Overseas