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.3 - Reacting to a Mouse Click and Rolling the Die

There are actually two things that you want your control to do when the user clicks the mouse on the control: to inform the container that the control has been clicked and to roll the die and display the new internal value.


FIG. 11.6 By adding one property and changing two functions, you have transformed the empty shell into a control that displays a 3.

11.3.1 Notifying the Container

Let’s first tackle using an event to notify a container. Events are how controls notify the container of a user action. Just as there are stock properties, there are stock events. These events are already coded for you:

· Click is coded to indicate to the container that the user clicked.

· DblClick is coded to indicate to the container that the user double-clicked.

· Error is coded to indicate an error that can’t be handled by firing any other event.

· KeyDown is coded to indicate to the container that a key has gone down.

· KeyPress is coded to indicate to the container that a complete keypress (down and then up) has occurred.

· KeyUp is coded to indicate to the container that a key has gone up.

· MouseDown is coded to indicate to the container that the mouse button has gone down.

· MouseMove is coded to indicate to the container that the mouse has moved over the control.

· MouseUp is coded to indicate to the container that the mouse button has gone up.

The best way to tell the container that the user has clicked over the control is to fire a Click stock event. The first thing to do is to add it to the control with ClassWizard. Follow these steps:

1. Bring up ClassWizard by choosing View, ClassWizard, and click the ActiveX Events tab. Make sure that the selected class is CDierollCtrl.

2. Click the Add Event button and fill in the Add Event dialog box, as shown in Figure 11.7.

3. The external name is Click; choose it from the drop-down list box and notice how the internal name is filled in as FireClick.

4. Click OK to add the event, and your work is done. Close ClassWizard.


FIG. 11.7 ClassWizard helps you add events to your control.

You may notice the ClassView pane has a new addition: two icons resembling handles. Click the + next to _DDierollEvents to see that Click is now listed as an event for this application.

Now when the user clicks the control, the container class will be notified. If you are writing a backgammon game, for example, the container can respond to the click by using the new value on the die to evaluate possible moves or do some other backgammon-specific task.

The second part of reacting to clicks involves actually rolling the die and redisplaying it. Not surprisingly, ClassWizard helps implement this. When the user clicks over your control, you catch it with a message map entry, just as with an ordinary application. Bring up ClassWizard and follow these steps:

1. Select the Message Maps tab this time and make sure that your control class, CDierollCtrl, is selected in the Class Name combo box.

2. Scroll through the Messages list box until you find the WM_LBUTTONDOWN message, which Windows generates whenever the left mouse button is clicked over your control.

3. Click Add Function to add a function that will be called automatically whenever this message is generated—in other words, whenever the user clicks your control. This function must always be named OnLButtonDown(), so ClassWizard doesn’t give you a dialog box asking you to confirm the name.

4. ClassWizard has made a skeleton version of OnLButtonDown() for you; click the Edit Code button to close ClassWizard, and look at the new OnLButtonDown() code. Here’s the skeleton:

void CDierollCtrl::OnLButtonDown(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

COleControl::OnLButtonDown(nFlags, point);

}

5. Replace the TODO comment with a call to a new function, Roll(), that you will write in the next section. This function will return a random number between 1 and 6.

m_number = Roll();

6. To force a redraw, next add this line:

InvalidateControl();

7. Leave the call to COleControl::OnLButtonDown() at the end of the function; it handles the rest of the work involved in processing the mouse click.

11.3.2 Rolling the Die

To add Roll() to CDierollCtrl, right-click on CDierollCtrl in the ClassView pane and then choose Add Member Function from the shortcut menu that appears. Roll() will be a public function that takes no parameters and returns a short.

What should Roll() do? It should calculate a random value between 1 and 6. The C++ function that returns a random number is rand(), which returns an integer between 0 and RAND_MAX. Dividing by RAND_MAX + 1 gives a positive number that is always less than 1, and multiplying by 6 gives a positive number that is less than 6. The integer part of the number will be between 0 and 5, in other words. Adding 1 produces the result that you want: a number between 1 and 6. Listing 11.9 shows this code.

Listing 11.9 DierollCtl.cpp—CDierollCtrl::Roll()

short CDierollCtrl::Roll(void)

{

double number = rand();

number /= RAND_MAX + 1;

number *= 6;

return (short)number + 1;

}

NOTE: If RAND_MAX + 1 isn’t a multiple of 6, this code will roll low numbers slightly more often than high ones. A typical value for RAND_MAX is 32,767, which means that 1 and 2 will, on the average, come up 5,462 times in 32,767 rolls. However, 3 through 6 will, on the average, come up 5,461 times. You’re neglecting this inaccuracy.

Some die-rolling programs use the modulo function instead of this approach, but it is far less accurate. The lowest digits in the random number are least likely to be accurate. The algorithm used here produces a much more random die roll.

The random number generator must be seeded before it is used, and it’s traditional (and practical) to use the current time as a seed value. In DoPropExchange(), add the following line before the call to PX_Short():

srand( (unsigned)time( NULL ) );

Rather than hard-code the start value to 3, call Roll() to determine a random value. Change the call to PX_Short() so that it reads as follows:

PX_Short( pPX, "Number", m_number, Roll());

Make sure the test container is not still open, build the control, and then test it again in the test container. As you click the control, the displayed number should change with each click. Play around with it a little: Do you ever see a number less than 1 or more than 6? Any surprises at all?

 

Next>>
 
© Dewsoft Overseas