The hottest thing these days on the Internet is ActiveX controls, which not only can be included as objects in a container application's document, but also can be embedded into Web pages to provide executable content on the Internet. Although creating an ActiveX control can be a complicated task, the basics are easy to master, especially considering Visual C++ 5.0 includes the MFC ActiveX ControlWizard, which performs much of the work for you. Then, you need only to add the specific program lines for your control. This chapter introduces you to creating ActiveX controls with ActiveX ControlWizard.
As far as the Internet is concerned, on the surface ActiveX controls are a lot like Java applets in that ActiveX controls enable Web developers to include mini-applications in their Web pages. However, although the results might look similar, on the inside ActiveX controls are nothing at all like Java applets.
What do ActiveX controls look like? Virtually, anything at all. Figure 29.1, for example, shows an application using Address ActiveX controls. The application actually contains two controls, each an instance of the Address control. One instance of the control is being used to retrieve an old address from the user and the other is used to get the new address.
The Address control provides a form in which a user can type in address information.
The Address control is a fairly simple example. ActiveX controls can be as sophisticated as you want them to be. A case in point is Microsoft's Calendar control (see Figure 29.2), which demonstrates various aspects of using and creating controls. The control's display includes 3D graphics, as well as a number of options that can be set by the user.
The Calendar control is a more sophisticated example of what ActiveX controls can do.
When you come right down to it, although most ActiveX controls perform simple tasks, an ActiveX control can also be a full-fledged application. From a user's point of view, the only difference between an ActiveX control and some other type of application is that ActiveX controls cannot survive on their own. That is, whereas you can run an application directly, an ActiveX control must be included as part of another application, Web page, or other type of ActiveX container. This is a pretty minor limitation, though, because an application running in its own window and an application containing an ActiveX control can look and act virtually identical to one another.
If you've ever tried to program ActiveX controls before, you know that, unless you're an OLE expert, the programming involved can be a nightmare. OLE, on which ActiveX is based, is described in programming references thousands of pages long. Just trying to plow through this documentation to make sense of it is a full-time job even for professional programmers. Microsoft knows this, and so needed a way to bring the programming of ActiveX controls down to a level that the average person can understand. Without making ActiveX more accessible, there was no way Microsoft would be able to compete with Sun Microsystem's Java.
Enter MFC and ActiveX ControlWizard, which hides almost all of the difficult details of creating ActiveX controls, enabling the programmer to spend his time making controls, rather than trying to figure out indecipherable programming manuals. Creating ActiveX controls with MFC and ControlWizard can actually be easier than creating applets with Java. Anyone with basic programming knowledge can get started with ControlWizard immediately.
The first step in creating a control is to run MFC ControlWizard to create the skeleton program code for the control. This program code provides all of the basics needed to compile and run an ActiveX control. Before the control will do what you want, however, you have to add your own custom program lines, which you'll do throughout the remainder of this chapter. For now, complete the following steps to create the basic Calculator control.
ON THE WEB
http://www.quecorp.com/semfc The complete source code and executable file for this version of the ActiveXServ application is located in the Chap29\Calculator, Part 1 folder at this book's Web site.
1. Start a new MFC ActiveX ControlWizard project workspace called Calculator.
2. Give the new project the following settings in the ControlWizard dialog boxes. The New Project Information dialog box should then look like Figure 29.3.
Step 1: Default Settings
Step 2: Default setting
These are the project settings for the Address control.
Believe it or not, you've now created the basic Calculator application. To compile the control, choose Developer Studio's Build, Build command.
After compiling the control, Visual C++ also registers it with the system. To prove that Calculator is already a runnable control, you can load the control into
Microsoft's test container application. To do this, choose Developer Studio's Tools, ActiveX Control Test Container command. When you do, the
test container application appears.
In the test application, choose the Edit, Insert OLE Control command. The Insert Control dialog box appears (see Figure 29.4). Double-click
Calculator Control to embed the new Calculator control into the test application's document. The control appears in the application's window, as shown in Figure
29.5.
You use the Insert Control dialog box to choose the control to embed.
Here's the basic Calculator control embedded in the test application's document.
As you can see, right now the Calculator control doesn't look like much. In fact, its appearance is created totally by the default source code provided by ControlWizard. To change the control's appearance and functionality, you must modify the skeleton control that was created by the wizard. In the following section, you take care of that task. About the only thing you can do with the default control is move it around the window, resize it, and display its About dialog box.
To display the About dialog box, first make sure that the control is selected (click it), and then choose the test application's Edit, Invoke Methods
command. The Invoke Control Method dialog box appears, as shown in Figure 29.6. In the Name text box, you can select the method that you want to
invoke. Currently, the control has only a single method called AboutBox. Just click the Invoke button to invoke the method. The control's About dialog box
appears. Close the dialog box, and then click the Close button to dismiss the Invoke Control Method dialog box. Now, you can close the test application.
The Invoke Control Method dialog box enables you to test your control's methods.
There are a few special things that you need to know to modify the basic ActiveX control created by ControlWizard. However, much of what you already know about MFC can be applied toward the control. For example, just like most MFC programs, in order to draw the control's appearance, you need to write program lines for the OnDraw() function. In this section, you'll create a user interface for the Calculator control by completing the following steps.
ON THE WEB
http://www.quecorp.com/semfc The complete source code and executable file for this version of the Calculator control is located in the Chap29\Calculator, Part 2 folder on this book's Web site.
1. Load the CalculatorCtl.h header file, and add the lines shown in Listing 29.1 to the class's declaration, right after the line CCalculatorCtrl();.
Listing 29.1 lst29_01.cpp New Data Members for the CCalculatorCtrl Class
CEdit m_value1; CEdit m_value2; CEdit m_result; CButton m_calculate;These lines declare the four controls that make up the Calculator control's user interface.
2. Use ClassWizard to add the OnCreate() function to the CCalculatorCtrl class, as shown in Figure 29.7.
The OnCreate() function responds to the WM_CREATE Windows message.
3. Click the
Edit Code button, and add the lines shown in Listing 29.2 to the OnCreate() function, right after the TODO: Add your specialized creation code here comment:Listing 29.2 lst29_02.cpp Program Lines for the OnCreate() Function
m_value1.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, CRect(10, 60, 110, 90), this, IDC_VALUE1); m_value2.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, CRect(10, 100, 110, 130), this, IDC_VALUE2); m_result.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, CRect(120, 100, 220, 130), this, IDC_RESULT); m_calculate.Create("Calculate", WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, CRect(120, 60, 220, 90), this, IDC_CALCULATE);These lines create the controls that make up the Calculator control's user interface.
4. Choose Developer Studio's
View, Resource Symbols command. The Resource Symbols dialog box appears, displaying the resource IDs currently defined in your project.5. Click the
New button. The New Symbol dialog box appears. In the Name box, type IDC_VALUE1 (see Figure 29.8), and click OK.You just added the IDC_VALUE1 resource ID to the project. In Listing 29.2, you can see that this ID is assigned to the first edit control, called m_value1.
You need to create resource IDs for Calculator's edit and button controls.
6. Repeat Step 5 to create IDs for the remaining controls. The IDs should be IDC_VALUE2, IDC_RESULT, and IDC_CALCULATE and should have the values 102, 103, and 104, respectively. Click the Close button to dismiss the Resource Symbols dialog box.
7. In the OnDraw() function, delete the line pdc->Ellipse(rcBounds);.
This is the line that draws the control's default display, which is a shaded ellipse. The Calculator control will create its own custom display.
8. Add the lines shown in Listing 29.3 to the end of the OnDraw() function, right after the line pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));:
Listing 29.3 lst29_03.cpp New Program Lines for the OnDraw() Function
// Print labels for the controls. pdc->TextOut(10, 40, "Enter Values Below"); pdc->TextOut(150, 130, "Result"); // Display the Calculator control's caption in a large font. CFont font; font.CreateFont(30, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "TimesRoman"); CFont* oldFont = pdc->SelectObject(&font); pdc->TextOut(10, 0, "Calculator"); pdc->SelectObject(oldFont);
You've now created the Calculator control's user interface. To compile the control, choose Developer Studio's Build, Build command. After
compiling the control, Visual C++ also registers it with the system. To see the Calculator control's new user interface, load the control into Microsoft's test
container application. After embedding the control into the test application's document, you'll need to resize the control, as shown in Figure 29.9.
To register a control without rebuilding it, choose the
Tools, Register Control command in Developer Studio's menu bar. This command enables you to register a control located at this book's Web site.
The Calculator control's display includes edit and button controls.
As do other types of ActiveX objects, ActiveX controls can feature properties and methods. As you already know, properties are similar to data members, and methods are similar to functions. In most cases, you enable the user to manipulate the control's properties and methods, so that he can customize how the control looks and acts in his application or Web page. In this section, you'll add two types of properties to the Calculate control. The first type of property is a data member that can be handled using a notification method. The second type of property is a value that is manipulated by Get() and Set() property methods. Complete the following steps to add properties to the Calculator control.
ON THE WEB
http://www.quecorp.com/semfc The complete source code and executable file for this version of the Calculator control is located in the Chap29\Calculator, Part 3 folder at this book's Web site.
1. Press Ctrl+W to display ClassWizard, and select the wizard's Automation page.
2. Click the Add P
roperty button. The Add Property dialog box appears. Fill in the dialog box as shown in Figure 29.10. Click the OK button to add the property to the control.You just created a property called MultValues, which is a Boolean value. The value of the property is stored in a data member called m_multValues. The MultValues property will control whether the Calculator control sums or multiplies the values entered by the user.
This is how you create a property that's associated with a notification method.
3. Click the Add P
roperty button again. This time fill in the Add Property dialog box as shown in Figure 29.11. Make sure the Get/SetMethods option is checked, and click the OK button to add the property to the controls.You just created a property called ControlCaption, which is a string value. (Actually, it's a BSTR value, which is a special string type used with automation data manipulation functions.) The value of the property is controlled by Get() and Set() property methods, enabling the control to check changes to the property. The ControlCaption property will control the main caption displayed in the Calculator control.
This is how you create a property that is accessed through Get() and Set() property methods.
4. Click the Add P
roperty button. Fill in the Add Property dialog box as shown in Figure 29.12. Make sure the Get/SetMethods option is checked, and click the OK button to add the property to the controls. Finally, close ClassWizard by clicking the OK button.You just created a property called ButtonCaption, which is a string value. The value of the property is controlled by Get() and Set() property methods, enabling the control to check changes to the property. The ButtonCaption property controls the caption displayed by the Calculator control's button.
The ButtonCaption property determines the button's label.
5. Load the CalculatorCtl.h header file, and add the following lines to the class's declaration, right after the line CButton m_calculate, which you placed there previously:
CString m_buttonCaption; CString m_controlCaption;These lines declare string objects as data members of the CCalculatorCtrl class. These strings will hold the values of the ButtonCaption and ControlCaption properties. When you create properties that are controlled by Get() and Set() property methods, you must manually add storage for the properties to your control's class.
6. Load the CalculatorCtl.cpp file, locate the OnDraw() function, and replace the line pdc->TextOut(10, 0, "Calculator") with the following:
pdc->TextOut(10, 0, m_controlCaption);
Now the control's caption will be the string stored in the ControlCaption property (which is stored in the m_controlCaption data member), rather than the static string "Calculator."
7. Locate the OnCreate() function, and replace the lines
m_calculate.Create("Calculate", WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, CRect(120, 60, 220, 90), this, IDC_CALCULATE);with
m_calculate.Create(m_buttonCaption, WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, CRect(120, 60, 220, 90), this, IDC_CALCULATE);Now the button's caption will be the string stored in the ButtonCaption property (which is stored in the m_buttonCaption data member), rather than the static string "Calculator."
8. Locate the DoPropExchange() function, and add the following lines right after the TODO: Call PX_ functions for each persistent custom property comment:
PX_Bool(pPX, "MultValues", m_multValues, FALSE); PX_String(pPX, "ControlCaption", m_controlCaption, "Calculator"); PX_String(pPX, "ButtonCaption", m_buttonCaption, "Calculate");MFC ActiveX controls, much like MFC dialog boxes, feature a mechanism that automatically initializes and transfers the values of the control's properties. These values are even saved automatically so that they can be restored whenever the control is reloaded. Each such property, called a persistent property, must have a PX_ line in the DoPropExchange() function. You'll learn more about persistent properties later in this chapter, in the section entitled "Understanding Persistent Properties."
9. Add the following line to the GetControlCaption() method, right after the TODO: Add your property handler here comment:
strResult = m_controlCaption;
This line sets the returned string to the current value of the ControlCaption property, which is stored in the m_controlCaption data member.
10. Add the following line to the GetButtonCaption() method, right after the TODO: Add your property handler here comment:
strResult = m_buttonCaption;
This line sets the returned string to the current value of the ControlCaption property, which is stored in the m_controlCaption data member.
11. Add the following lines to the SetControlCaption() method, right after the TODO: Add your property handler here comment:
m_controlCaption = lpszNewValue; InvalidateControl();These lines store the new value of the ControlCaption property and redraw the control so that it shows the current ControlCaption string.
12. Add the following lines to the SetButtonCaption() method, right after the TODO: Add your property handler here comment:
m_buttonCaption = lpszNewValue; m_calculate.SetWindowText(m_buttonCaption);These lines store the new value of the ControlCaption property and redraw the button's text so that it shows the current ButtonCaption string.
You've now added properties to the Calculator control. To compile the control, choose Developer Studio's Build, Build command. After
compiling the control, Visual C++ also registers it with the system. To see the Calculator control's new properties in action, load the control into Microsoft's test
container application. After embedding the control into the test application's document, you'll need to resize the control.
Currently, the control is displaying the default values for the ControlCaption and ButtonCaption properties. Further, the MultValues property is set to its
default value of FALSE. To see that this is true, first make sure the control is selected (click it, if it's not), and then choose the test application's View, P
roperties command. The Properties dialog box appears. Choose a property in the Property box, and its current setting appears in the Value
box (see Figure 29.13).
The Property dialog box enables you to view and edit property settings.
To change a property, choose it in the Property box, type its new setting in the Value box, and then click the Apply button. When you do,
the control changes to show the new property setting. Figure 29.14 shows what happens if you change the ControlCaption property to "Adding Machine." You
can change the ButtonCaption property in exactly the same way.
When you change a property, the control immediately uses the new property value.
As you learned previously, persistent properties automatically retain their values. For this to work, you must write, for each property, a PX_ program line in the DoPropExchange() function. In the Calculator control, the DoPropExchange() function looks like Listing 29.4. As you can see in the function, each property has a PX_ program line. Each of these PX_ lines reflects the data type for the property. For example, the Boolean MultValues property is represented by the PX_Bool() line.
Listing 29.4 lst29_04.cpp The Calculator Control's DoPropExchange() Function
void CCalculatorCtrl::DoPropExchange(CPropExchange* pPX) { ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor)); COleControl::DoPropExchange(pPX); // TODO: Call PX_ functions for each persistent custom property. PX_Bool(pPX, "MultValues", m_multValues, FALSE); PX_String(pPX, "ControlCaption", m_controlCaption, "Calculator"); PX_String(pPX, "ButtonCaption", m_buttonCaption, "Calculate"); }
There are sixteen PX_ functions that you can use:
These functions have four arguments, which are a pointer to the CPropExchange object (passed into DoPropExchange() as a parameter), the property name, the property's storage variable, and the property's default value. If the property were previously set, its value is restored. Otherwise, the property gets the default value.
Two of the Calculator control's properties, ControlCaption and ButtonCaption are managed by Get() and Set() methods. The MultValues property, however, has no Get() or Set() methods. Instead, there is a notification method, called OnMultValuesChanged(), which is called whenever the MultValues property changes. The default OnMultValuesChanged() looks like Listing 29.5. The default function calls SetModifiedFlag() so that the control knows that it needs to save its persistent properties. You can use the notification function for other purposes, as well, such as validating the new property value.
Listing 29.5 lst29_05.cpp The Default Implementation of OnMultValuesChanged()
void CCalculatorCtrl::OnMultValuesChanged() { // TODO: Add notification handler code SetModifiedFlag(); }
At this point in its programming, the Calculator control's visual user interface is complete. However, the control still doesn't do anything useful. When the user enters two values and clicks the control's button, the control should perform the calculation and display the result. In this section, you add the program lines that accomplish this task by completing the following steps.
ON THE WEB
http://www.quecorp.com/semfc The complete source code and executable file for this version of the Calculator control is located in the Chap29\Calculator, Part 4 folder at this book's Web site.
1. Load the CalculatorCtl.h header file, and add the following line to class's message map declaration, right before the DECLARE_MESSAGE_MAP() line:
afx_msg void OnButtonClicked();
This line declares the OnButtonClicked() function, which will respond to button clicks.
2. Load the CalculatorCtl.cpp file, and add the following line to the class's message map (found near the top of the file), right after the ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties) line:
ON_BN_CLICKED(IDC_CALCULATE, OnButtonClicked)
This line associates the OnButtonClicked() function with button-click notifications. That is, when the user clicks the control's Calculate button, MFC will call OnButtonClicked(), where the program can respond to the button click.
3. Add the function shown in Listing 29.6 to the end of the file.
This function, which responds to the Calculate button, retrieves the values the user typed into the text boxes, converts those values to integers, performs the required calculation (as determined by the value of the MultValues property), and displays the result.
Listing 29.6 lst29_06.cpp The OnButtonClicked() Notification Function
void CCalculatorCtrl::OnButtonClicked() { CString s1, s2; m_value1.GetWindowText(s1); m_value2.GetWindowText(s2); int value1 = atoi(s1); int value2 = atoi(s2); int result; if (!m_multValues) result = value1 + value2; else result = value1 * value2; char s[20]; wsprintf(s, "%d", result); m_result.SetWindowText(s); }
You've now added the program lines needed for the Calculator control to perform its intended function. To compile the control, choose Developer Studio's B
uild, Build command. After compiling the control, Visual C++ also registers it with the system. To see the Calculator control in action, load the control
into Microsoft's test container application. After embedding the control into the test application's document, resize the control so that it's fully displayed.
Now, type two integers into the controls text boxes, and click the Calculate button. The control sums the two values and displays the result (see Figure 29.15).
Next, choose the test application's View, Properties command. When the Properties dialog box appears, change the MultValues property to 1,
as shown in Figure 29.16. Then, click the Calculate button again. This time the control finds the product of the two values, rather than the sum.
In its default state, the Calculator control sums the entered values.
By changing the MultValues property to TRUE (a nonzero value), the control will multiply values, rather than add them.
The Calculator control already has one method that can be accessed by a developer using the control in an application or a Web page. That method is the one that displays the control's About dialog box. However, the AboutBox() method was created by ControlWizard. Now, you'll learn to add your own methods to the Calculator control by completing the following steps to accomplish this important task.
ON THE WEB
http://www.quecorp.com/semfc The complete source code and executable file for this version of the Calculator control is located in the Chap29\Calculator, Part 5 folder at this book's Web Site.
1. Press Ctrl+W to display ClassWizard, and then select the wizard's Automation page.
2. Click the
Add Method button. The Add Method dialog box appears.3. Fill in the Add Method dialog box, as shown in Figure 29.17. Click the OK button to add the new ValidateValues() method to the control.
Here's how you add a method to a control.
4. Click the
Edit Code button, and replace the lines in the ValidateValues() function with the lines shown in Listing 29.7:Listing 29.7 lst29_07.cpp Program Lines for the ValidateValues() Method
// Initialize the method's return code. BOOL valuesOK = TRUE; // Retrieve strings from the text boxes. CString s1, s2; m_value1.GetWindowText(s1); m_value2.GetWindowText(s2); // Check that at least the first character of // the first string is a digit. TCHAR c = s1.GetAt(0); if (c < '0' || c > '9') valuesOK = FALSE; // Check that at least the first character of // the second string is a digit. c = s2.GetAt(0); if (c < '0' || c > '9') valuesOK = FALSE; // If the values are no good, tell the user. if (!valuesOK) MessageBox("Invalid values"); return valuesOK;
You've now added a new method to the Calculator control. To compile the control, choose Developer Studio's Build, Build command. After
compiling the control, Visual C++ also registers it with the system. To test the Calculator control's new method, load the control into Microsoft's test container
application. After embedding the control into the test application's document, resize the control so that it's fully displayed.
Now, type two integers into the control's text boxes, and choose the test application's Edit, Invoke Methods menu command. When the Invoke
Control Method dialog box appears, choose ValidateValues in the Name box, and click the Invoke button. The test application calls the
ValidateValues() method and displays the return value at the bottom of the Invoke Control Method dialog box. Because you typed valid digits into the text
boxes, the return value is TRUE (see Figure 29.18).
If you type valid integers, the ValidateValues() method returns TRUE.
Now, close the Invoke Control Method dialog box, and change one of the entries in the control's text boxes to an invalid value, such as the word "one." Use the Invoke Control Method dialog box to invoke the ValidateValues() method again, and you'll see a message box informing you that you've entered invalid values (see Figure 29.19). Moreover, the Invoke Control Method dialog box displays a return value of FALSE.
The control can now warn users when they've entered invalid values.
As you know, ActiveX controls can be used both as embedded objects in an application's document (such as when you added the control to the test application's window) and a control in a Web page. The first step in building a Web page that contains ActiveX controls is including the controls in the HTML document. Microsoft created the new <OBJECT> tag for just this task.
The <OBJECT> tag is a lot like other HTML tags you might have run into before such as <IMG> and <APPLET>. The <OBJECT> tag is a little more sophisticated, however, and harder to decipher, because it must include the ActiveX control's GUID, which is a special ID for the control that's guaranteed to be unique. An operating system, such as Windows 95, uses the GUID in its registry to positively identify the control. An <OBJECT> tag for the Calculator control might look like Listing 29.8.
Listing 29.8 lst29_08.cpp The <OBJECT> tag
<OBJECT classid="clsid:73E7511B-72B9-11D0-847F-444553540000" id=Calculator height = 150 width = 235> </OBJECT>
The <OBJECT> tag in Listing 29.8 tells the HTML document to find, load, and display the Calculator control. This form of the <OBJECT> tag contains a minimum amount of information needed to load and manipulate the control. The first part of the tag is the classid, which also happens to be comprised mainly of the control's GUID:
classid="clsid:73E7511B-72B9-11D0-847F-444553540000"
Because CLSIDs are unique from one control project to another, yours will be different than the one shown here.
How can you get the control's ID? This is pretty easy to do because Visual C++ has already registered the control in your system Registry. To find the ID, first run the REGEDIT.EXE tool, found in your main Windows directory. When you do, you see the window shown in Figure 29.20. Double-click the HKEY_CLASSES_ROOT folder, and then find the CALCULATOR.CalculatorCtrl.1 entry in the list that appears. Double-click this entry, and then click the CLSID entry. The control's GUID appears in the window's right pane (see Figure 29.21).
The REGEDIT tool enables you to view and edit the Registry.
The CLSID entry displays the control's GUID.
Returning to the <OBJECT> tag, the second part of the tag specifies the control's ID, which is a name that can be used within the HTML document to identify and communicate with the control. You would, for example, use the ID to manipulate the control from a script language like VBScript. The ID looks like this:
id=Calculator
Finally, the height and width parameters enable you to determine the size of the control when it's loaded:
height = 150 width = 235>
Listing 29.9 is an HTML document that loads and displays the Calculator control. If you load this HTML document into Microsoft Internet Explorer (currently, only Internet Explorer supports ActiveX controls), you'll see the window shown in Figure 29.22. Go ahead and use the control to sum some numbers.
Remember that you need to replace the classid parameter with the correct GUID for your version of the control. Otherwise, Internet Explorer will not be able to load the control. You also may need to change Internet Explorer's security settings to load a control that has not been properly certified.
Listing 29.9 Calculator1.html An HTML Document that Loads and Displays the Calculator Control
<HTML> <HEAD> <TITLE>MFC Calculator Control</TITLE> </HEAD> <BODY> <CENTER> <FONT SIZE=3>Visual C++ 5.0</FONT><BR> <FONT SIZE=6><B>Calculator ActiveX Control</B></FONT> <P> <OBJECT classid="clsid:73E7511B-72B9-11D0-847F-444553540000" id=Calculator height = 150 width = 235> </OBJECT> </CENTER> </BODY> </HTML>
Internet Explorer can display HTML documents that contain ActiveX controls.
To set the control's properties from the HTML document, you need to add parameters to the <OBJECT> tag. For example, to set the control's main caption, you need to add a line like this:
<param name="ControlCaption" value="Adding Machine">
The name part of the <PARAM> tag should be set to the name of the property, whereas the value part should be set to the value you want assigned to the property. Listing 29.10 is an HTML file that sets up the Calculator control as an adding machine. Notice that, because the default value of the MultValues property determines that the calculator will perform addition, it doesn't need to be set in the HTML file.
Listing 29.10 Calculator2.html An HTML File that Sets Up the Calculator Control for Addition.
<HTML> <HEAD> <TITLE>MFC Calculator Control</TITLE> </HEAD> <BODY> <CENTER> <FONT SIZE=3>Visual C++ 5.0</FONT><BR> <FONT SIZE=6><B>Calculator ActiveX Control</B></FONT> <P> <OBJECT classid="clsid:73E7511B-72B9-11D0-847F-444553540000" id=Calculator height = 150 width = 235> <param name="ControlCaption" value="Adding Machine"> <param name="ButtonCaption" value="Add"> </OBJECT> </CENTER> </BODY> </HTML>
Figure 29.23 shows the control as its displayed by the HTML code in Listing 29.10. Listing 29.11, on the other hand, sets up the control for multiplication. This time, all three properties need to be set. Figure 29.24 shows the result.
Here's the Calculator control created by Listing 29.10.
Listing 29.11 Calculator3.html An HTML File that Sets Up the Calculator Control for Multiplication
<HTML> <HEAD> <TITLE>MFC Calculator Control</TITLE> </HEAD> <BODY> <CENTER> <FONT SIZE=3>Visual C++ 5.0</FONT><BR> <FONT SIZE=6><B>Calculator ActiveX Control</B></FONT> <P> <OBJECT classid="clsid:73E7511B-72B9-11D0-847F-444553540000" id=Calculator height = 150 width = 260> <param name="MultValues" value="1"> <param name="ControlCaption" value="Multiplying Machine"> <param name="ButtonCaption" value="Multiply"> </OBJECT> </CENTER> </BODY> </HTML>
Here's the Calculator control created by Listing 29.11.
The Calculator control presented in this chapter is just a simple example of what can be done with ActiveX controls. As an exercise, you might want to beef up the Calculator controls so that it can handle more operations than just addition and multiplication. Or, you might want to take what you've learned and create a brand new control that'll help you put together the kinds of Web pages that you want.