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 11: Building an ActiveX Control

11.2 - Displaying the Current Value

Before the value can be displayed, the control must have a value. That involves adding a property to the control and then writing the drawing code.

11.2.1 Adding a Property

ActiveX controls have four types of properties:

· Stock. These are standard properties supplied to every control, such as font or color. The developer must activate stock properties, but there is little or no coding involved.

· Ambient. These are properties of the environment that surrounds the control—properties of the container into which it has been placed. These can’t be changed, but the control can use them to adjust its own properties. For example, it can set the control’s background color to match the container’s background color.

· Extended. These are properties that the container handles, usually involving size and placement onscreen.

· Custom. These are properties added by the control developer.

To add the value to the die-roll control, use ClassWizard to add a custom property called Number. Follow these steps:

1. Choose View, ClassWizard, and then click the Automation tab.

2. Make sure that the Project drop-down list box at the upper-left of the dialog box is set to Dieroll (unless you chose a different name when building the control with AppWizard) and that the Class Name drop-down list box on the right has the classname CDieRollCtrl.

3. Click the Add Property button and fill in the dialog box as shown in Figure 11.4.

4. Type Number into the External Name combo box and notice how ClassWizard fills in suggested values for the Variable Name and Notification Function boxes.

5. Select short for the type.

6. Click OK to close the Add Property dialog box and OK to close ClassWizard.

Before you can write code to display the value of the Number property, the property must have a value to display. Control properties are initialized in DoPropExchange().

FIG. 11.4 ClassWizard simplifies the process of adding a custom property to your die-rolling control.

This method actually implements persistence; that is, it enables the control to be saved as part of a document and read back in when the document is opened. Whenever a new control is created, the properties can’t be read from a file, so they are set to the default values provided in this method. Controls don’t have a Serialize() method.

AppWizard generated a skeleton DoPropExchange() method; this code is in Listing 11.7.

Listing 11.7 DierollCtl.cpp—CDierollCtrl::DoPropExchange()

void CDierollCtrl::DoPropExchange(CPropExchange* pPX)

{

ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));

COleControl::DoPropExchange(pPX);

// TODO: Call PX_ functions for each persistent custom property.

}

Notice the use of the version numbers to ensure that a file holding the values was saved by the same version of the control. Take away the TODO comment that AppWizard left for you, and add this line:

PX_Short( pPX, "Number", m_number, (short)3 );

PX_Short() is one of many property-exchange functions that you can call—one for each property type that is supported. The parameters you supply are as follows:

· The pointer that was passed to DoPropExchange()

· The external name of the property as you typed it on the ClassWizard Add Property dialog box

· The member variable name of the property as you typed it on the ClassWizard Add Property dialog box

· The default value for the property (later, you can replace this hard-coded 3 with a random value)

The following are the PX functions:

  • PX_Blob() (for binary large object [BLOB] types)

  • PX_Bool()

  • PX_Color() (OLE_COLOR)

  • PX_Currency()

  • PX_DATAPATH (CDataPathProperty)

  • PX_Double()

  • PX_Float()

  • PX_Font()

  • PX_IUnknown() (for LPUNKNOWN types, COM interface pointer)

  • PX_Long()

  • PX_Picture()

  • PX_Short()

  • PX_String()

  • PX_ULong()

  • PX_UShort()

  • PX_VBXFontConvert()

Filling in the property’s default value is simple for some properties but not for others. For example, you set colors with the RGB() macro, which takes values for red, green, and blue from 0 to 255 and returns a COLORREF. Say that you had a property with the external name EdgeColor and the internal name m_edgecolor and you wanted the property to default to gray. You would code that like the following:

PX_Short( pPX, "EdgeColor", m_edgecolor, RGB(128,128,128) );

Controls with font properties should, by default, set the font to whatever the container is using. To get this font, call the COleControl method AmbientFont().

11.2.2 Writing the Drawing Code

The code to display the number belongs in the OnDraw() method of the control class, CDierollCtrl. (Controls don’t have documents or views.) This function is called automatically whenever Windows needs to repaint the part of the screen that includes the control. AppWizard generated a skeleton of this method, too, shown in Listing 11.8.

Listing 11.8 DierollCtl.cpp—CDierollCtrl::OnDraw()

void CDierollCtrl::OnDraw(CDC* pdc, const CRect& rcBounds,

const CRect& rcInvalid)

{

// TODO: Replace the following code with your own drawing code.

pdc->FillRect(rcBounds,

CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));

pdc->Ellipse(rcBounds);

}

As discussed in the "Scrolling Windows" section of Unit 5, "Drawing on the Screen," the framework passes the function a device context to draw in, a CRect describing the space occupied by your control, and another CRect describing the space that has been invalidated. The code in Listing 17.8 draws a white rectangle throughout rcBounds and then draws an ellipse inside that rectangle, using the default foreground color. You can keep the white rectangle for now, but rather than draw an ellipse on it, draw a character that corresponds to the value in Number. To do that, replace the last line in the skeletal OnDraw() with these lines:

CString val; //character representation of the short value

val.Format("%i",m_number);

pdc->ExtTextOut( 0, 0, ETO_OPAQUE, rcBounds, val, NULL );

These code lines convert the short value in m_number (which you associated with the Number property on the Add Property dialog box) to a CString variable called val, using the new CString::Format() function (which eliminates one of the last uses of sprintf() in C++ programming). The ExtTextOut() function draws a piece of text—the character in val—within the rcBounds rectangle. As the die-roll control is written now, that number will always be 3.

You can build and test the control right now if you would like to see how little effort it takes to make a control that does something. Unlike the other ActiveX applications, a control isn’t run as a standalone application in order to register it. Build the project and fix any typing mistakes. Choose Tools, ActiveX Control Test Container to bring up the control test container, shown in Figure 11.5.

FIG. 11.5 The ActiveX control test container is the ideal place to test your control.

Within the test container, choose Edit, Insert New Control and then choose Dieroll Control from the displayed list. As Figure 11.6 shows, the control appears as a white rectangle displaying a small number 3. You can move and resize this control within the container, but that little 3 stays doggedly in the upper-left corner. The next step is to make that number change when a user clicks the die.

 

Next>>
 
© Dewsoft Overseas