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.4 - Creating a Better User Interface

Now that the basic functionality of the die-roll control is in place, it’s time to neaten it a little. It needs an icon, and it needs to display dots instead of a single digit.

11.4.1 A Bitmap Icon

Because some die-roll control users might want to add this control to the Control Palette in Visual Basic or Visual C++, you should have an icon to represent it. AppWizard has already created one, but it is simply an MFC logo that doesn’t represent your control in particular. You can create a more specialized one with Developer Studio. Click the ResourceView tab of the Project Workspace window, click the + next to Bitmap, and double-click IDB_DIEROLL. You can now edit the bitmap 1 pixel at a time. Figure 11.8 shows an icon appropriate for a die. From now on, when you load the die-roll control into the test container, you will see your icon on the toolbar.

11.4.2 Displaying Dots

The next step in building this die-roll control is to make the control look like a die. A nice 3D effect with parts of some of the other sides showing is beyond the reach of an illustrative unit like this one, but you can at least display a dot pattern.


FIG. 11.8 The ResourceView of Visual C++ enables you to build your own icon to be added to the Control Palette in Visual Basic.

The first step is to set up a switch statement in OnDraw(). Comment out the three drawing lines and then add the switch statement so that OnDraw() looks like Listing 11.10.

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

void CDierollCtrl::OnDraw(

CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)

{

pdc->FillRect(rcBounds,

CBrush::FromHandle((HBRUSH)

GetStockObject(WHITE_BRUSH)));

// CString val; //character representation of the short value

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

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

switch(m_number)

{

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:

break;

}

}

Now all that remains is adding code to the case 1: block that draws one dot, to the case 2: block that draws two dots, and so on. If you happen to have a real die handy, take a close look at it. The width of each dot is about one quarter of the width of the whole die’s face. Dots near the edge are about one-sixteenth of the die’s width from the edge. All the other rolls except 6 are contained within the layout for 5, anyway; for example, the single dot for 1 is in the same place as the central dot for 5.

The second parameter of OnDraw(), rcBounds, is a CRect that describes the rectangle occupied by the control. It has member variables and functions that return the control’s upper-left coordinates, width, and height. The default code generated by AppWizard called CDC::Ellipse() to draw an ellipse within that rectangle. Your code will call Ellipse(), too, passing a small rectangle within the larger rectangle of the control. Your code will be easier to read (and will execute slightly faster) if you work in units that are one-sixteenth of the total width or height. Each dot will be four units wide or high. Add the following code before the switch statement:

int Xunit = rcBounds.Width()/16;

int Yunit = rcBounds.Height()/16;

int Top = rcBounds.top;

int Left = rcBounds.left;

Before drawing a shape by calling Ellipse(), you need to select a tool with which to draw. Because your circles should be filled in, they should be drawn with a brush. This code creates a brush and tells the device context pdc to use it, while saving a pointer to the old brush so that it can be restored later:

CBrush Black;

Black.CreateSolidBrush(RGB(0x00,0x00,0x00)); //solid black brush

CBrush* savebrush = pdc->SelectObject(&Black);

After the switch statement, add this line to restore the old brush:

pdc->SelectObject(savebrush);

Now you’re ready to add lines to those case blocks to draw some dots. For example, rolls of 2, 3, 4, 5, or 6 all need a dot in the upper-left corner. This dot will be in a rectangular box that starts one unit to the right and down from the upper-left corner and extends five units right and down. The call to Ellipse looks like this:

pdc->Ellipse(Left+Xunit, Top+Yunit,

Left+5*Xunit, Top + 5*Yunit);

The coordinates for the other dots are determined similarly. The switch statement ends up as show in Listing 11.11.

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

switch(m_number)

{

case 1:

pdc->Ellipse(Left+6*Xunit, Top+6*Yunit,

Left+10*Xunit, Top + 10*Yunit); //center

break;

case 2:

pdc->Ellipse(Left+Xunit, Top+Yunit,

Left+5*Xunit, Top + 5*Yunit); //upper left

pdc->Ellipse(Left+11*Xunit, Top+11*Yunit,

Left+15*Xunit, Top + 15*Yunit); //lower right

break;

case 3:

pdc->Ellipse(Left+Xunit, Top+Yunit,

Left+5*Xunit, Top + 5*Yunit); //upper left

pdc->Ellipse(Left+6*Xunit, Top+6*Yunit,

Left+10*Xunit, Top + 10*Yunit); //center

pdc->Ellipse(Left+11*Xunit, Top+11*Yunit,

Left+15*Xunit, Top + 15*Yunit); //lower right

break;

case 4:

pdc->Ellipse(Left+Xunit, Top+Yunit,

Left+5*Xunit, Top + 5*Yunit); //upper left

pdc->Ellipse(Left+11*Xunit, Top+Yunit,

Left+15*Xunit, Top + 5*Yunit); //upper right

pdc->Ellipse(Left+Xunit, Top+11*Yunit,

Left+5*Xunit, Top + 15*Yunit); //lower left

pdc->Ellipse(Left+11*Xunit, Top+11*Yunit,

Left+15*Xunit, Top + 15*Yunit); //lower right

break;

case 5:

pdc->Ellipse(Left+Xunit, Top+Yunit,

Left+5*Xunit, Top + 5*Yunit); //upper left

pdc->Ellipse(Left+11*Xunit, Top+Yunit,

Left+15*Xunit, Top + 5*Yunit); //upper right

pdc->Ellipse(Left+6*Xunit, Top+6*Yunit,

Left+10*Xunit, Top + 10*Yunit); //center

pdc->Ellipse(Left+Xunit, Top+11*Yunit,

Left+5*Xunit, Top + 15*Yunit); //lower left

pdc->Ellipse(Left+11*Xunit, Top+11*Yunit,

Left+15*Xunit, Top + 15*Yunit); //lower right

break;

case 6:

pdc->Ellipse(Left+Xunit, Top+Yunit,

Left+5*Xunit, Top + 5*Yunit); //upper left

pdc->Ellipse(Left+11*Xunit, Top+Yunit,

Left+15*Xunit, Top + 5*Yunit); //upper right

pdc->Ellipse(Left+Xunit, Top+6*Yunit,

Left+5*Xunit, Top + 10*Yunit); //center left

pdc->Ellipse(Left+11*Xunit, Top+6*Yunit,

Left+15*Xunit, Top + 10*Yunit); //center right

pdc->Ellipse(Left+Xunit, Top+11*Yunit,

Left+5*Xunit, Top + 15*Yunit); //lower left

pdc->Ellipse(Left+11*Xunit, Top+11*Yunit,

Left+15*Xunit, Top + 15*Yunit); //lower right

break;

}

Build the OCX again and try it out in the test container. You will see something similar to Figure 11.9, which actually looks like a die!

FIG. 11.9 Your rolling-die control now looks like a die. 

 

Next>>
 
© Dewsoft Overseas