Download Special Edition Using Visual C++ 6
Transcript
Special Edition Using Visual C++ 6 -- Ch 17 -- Building an ActiveX Control
number between 1 and 6. Listing 17.9 shows this code.
Listing 17.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. n
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?
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.
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 17.10 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.
http://www.pbs.mcp.com/ebooks/0789715392/ch17/ch17.htm (11 of 23) [7/29/1999 3:52:48 PM]
Related documents
Oracle Health Insurance Back Office
PDF file - EMME/2 Support Center
IMK-571R Software and Programming Guides
General Terms and Conditions for bank accounts and payment
coen-2003-project-24.. - Computer Engineering
User Guide
User manual - UK Free TV
Cortex-M3 107 Development Board User`s Manual
WinCON - Getting Started Manual
KALOS 2 USER MANUAL
WinCon-8000 Getting Started
Visual C++ 6.0 Tutorial