Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Schongar P.VBScript unleashed.1997

.pdf
Скачиваний:
46
Добавлен:
23.08.2013
Размер:
1.59 Mб
Скачать

Chapter 7

Using ActiveX Controls

by Brian Johnson

CONTENTS

Looking at a Brief History of Custom Controls

Examining the Anatomy of an ActiveX Control

Registering Your ActiveX Controls

Putting an ActiveX Control in Your Web Page

Installing and Distributing ActiveX Controls

Examining ActiveX Controls

Animated Button Control

Chart Control

Label

Popup Menu Control

Preloader Control

StockTicker Control

Timer Control

Using Third-Party Controls

Using Signed Controls

Creating ActiveX Controls

Using the Control Wizard to Create the Skeleton Files

Compiling a Control

Registering Your Control

Testing Your Control

Creating Non-MFC ActiveX Controls

Signing Your Objects for Internet Use

Review

Custom controls have revolutionized Windows programming. If you're new to programming, you might not appreciate the impact custom controls have had on software development. In this chapter, you'll examine the ways ActiveX controls can be used to enhance your Web development efforts.

Looking at a Brief History of Custom Controls

ActiveX custom controls, or OCXs, are the 32-bit descendants of the dynamic link library (DLL) and, more recently, the Visual Basic Control (VBX). In order to understand these controls and what they do, it might help to take a brief look at how Windows controls have evolved over the last few years.

Microsoft Windows was developed with the understanding that software developers needed a way to easily take advantage of the features and functions built into the operating system. Developers used DLLs to meet this need. DLLs are programs that provide functions and procedures to other programs. DLLs changed the way programmers thought about adding features to their programs. Instead of saying, "We need some information about memory; let's ask the machine about it," the thinking changed to, "We need some information about memory; let's see what Windows knows." In the first case, programmers were asking for information directly from the processor; in the second case, the program was asking for information from Windows. Not much difference, but it did provide a layer of abstraction to the process of getting the information.

After Visual Basic hit the market, a specification for the VBX control turned a very simple programming system into an extensible, world-class development tool. The VBX control is also a DLL, but with special features that enable people to use the control seamlessly in the Visual Basic development environment.

The Win32 API brought another form of custom control: the OLE custom control (or OCX). We now call these ActiveX controls. An ActiveX control is different from a VBX or DLL because the control is registered with the operating system when it is installed. After an ActiveX control is registered, it can be used as a component by any application developed, using any one of many different development environments (subject to certain licensing restrictions), including VBScript.

Examining the Anatomy of an ActiveX Control

Remember that an ActiveX file is basically the 32-bit cousin of the VBX control and can be used in both Visual Basic and VBScript. An ActiveX control is more advanced and flexible in that it implements OLE in process servers as DLLs. In other words, it supports some very useful OLE features, such as in-place activation, automation, and event notification. Table 7.1 lists the library types used in Windows.

 

Table 7.1. DLLs and custom controls.

Control

Extension

Function

Dynamic link library

.DLL

Enables users to access functions, routines, and

 

 

resources from other applications.

Visual Basic

.VBX

Provides the same custom control capabilities

 

 

as a DLL. Can be used graphically in a

 

 

development environment, such as Visual Basic

 

 

3.0, MSVC++ 1.52, and Delphi 1.0.

ActiveX control

.OCX

Provides the same services as a DLL or VBX.

 

 

In addition, the OCX can take advantage of

 

 

extremely powerful OLE features.

Registering Your ActiveX Controls

Like other OLE objects, when you install an OCX file, it is registered with the operating system in a system database called the Registry. When an OCX file is registered, its unique class ID (CLSID) number is placed in the system Registry. The CLSID number called from your HTML Web page instantiates (or creates an instance of) the object on the page on the client machine.

OLE significantly adds to the flexibility and capabilities of a Web page in a client/server environment like the Internet. The potential might not be obvious to you yet, but consider this for starters: An OLE object or an OCX can do anything that can be done on the client machine. The possibilities are exciting, but there are risks, and we'll talk about some of these later.

All ActiveX controls are referenced by their CLSID numbers stored in the Registry. Table 7.2 contains some of the CLSID numbers you'll use to insert ActiveX controls into your HTML.

Table 7.2. CLSID numbers for common controls.

Control CLSID

Chart FC25B780-75BE-11CF-8B01-

444553540000

Label 99B42120-6EC7-11CF-A6C7-

00AA00A47DD2

Menu Control 52DFAE60-CEBF-11CF-A3A9- 00A0C9034920

Preloader 16E349E0-702C-11CF-A3A9-

00A0C9034920

Popup Menu 7823A620-9DD9-11CF-A662-

00AA00C066D2

StockTicker 0CA4A620-8E3D-11CF-A3A9-

00A0C9034920

Timer 59CCB4A0-727D-11CF-AC36- 00AA00A47DD2

Keep in mind that some custom controls require licenses for development use and distribution. You can't necessarily use these objects in your own Web pages without a proper license. In these cases, the control vendor will provide you with a file that must be kept in the same directory as the control or Web page for the control to work on a user's machine.

Third-Party Controls

A large cottage industry of programmers has developed with the widespread use of VBX files. These developers were able to create and distribute controls and still make money from them by requiring license (.LIC) files to be installed on the machines of developers but not redistributed with the controls. License files enable programmers to use the controls in the development environment. The same sort of licensing capabilities were created for ActiveX controls, but instead of a license file being added to the developer's Windows/System directory, a license code is added to the Registry. The code stored in the Registry is not redistributable and is subject to the same sorts of restrictions as the LIC file.

Putting an ActiveX Control in Your Web Page

An ActiveX control is inserted into an HTML page by using the <OBJECT> tag, followed by the <CLASSID> tag and the CLSID number:

<OBJECT ID="pmenu1" WIDTH=0 HEIGHT=0

CLASSID="CLSID:52DFAE60-CEBF-11CF-A3A9-00A0C9034920">

Initial parameters are set before the CLSID number, along with an <ID> tag that sets the size and gives the control a name (ID). Control of the OCX is managed by using the ID. The HEIGHT and WIDTH properties of the ActiveX control set a rectangle in which visual controls are displayed. Placement of the control on the Web page is dependent on HTML formatting.

You can set the property values for the object by using the <param> tag inside the <OBJECT> </OBJECT> pair, as in this example:

<param name="angle" value="90">

<param name="BackStyle" value="0">

ActiveX controls are programmed just like any other object on your Web page. You set the properties of the control, and then your script reacts to messages sent by the user interacting with the objects in the browser. Because this is happening mostly on the client machine, the speed of the operation is limited only by your client's hardware.

Each ActiveX control has a set of properties and actions that can be set and reacted to. Suppose that you use the Label control to create some text on your page. You might set up the label like this:

<OBJECT ID="Label1" WIDTH=104 HEIGHT=27

CLASSID="CLSID:99B42120-6EC7-11CF-A6C7-00AA00A47DD2">

<PARAM NAME="_ExtentX" VALUE="2752">

<PARAM NAME="_ExtentY" VALUE="714">

<PARAM NAME="Caption" VALUE="Label Control">

</OBJECT>

You then can respond to the Click event in your code:

Sub Label1_Click()

Dim a

a = Label1.Caption

If a = "Start" Then

a = "Stop"

Else

a = "Start"

End If

Label1.Caption = a

End Sub

In this example, you can see that an ActiveX object works and behaves just like any VBScript intrinsic control. This is one of the great benefits of using ActiveX controls in Web pages. The broad base of current custom control developers will eventually make it very easy to find just the control you need for your particular purpose. If you're using a particularly popular control, such as one of the Microsoft controls discussed in this chapter, the user might already have the control installed on his or her system. The use of such controls is then seamless to the user.

Installing and Distributing ActiveX Controls

Most ActiveX custom controls will have to be downloaded and installed on the user's machine before they can be viewed in the user's browser. ActiveX controls are automatically downloaded to the user's machine if the CODEBASE parameter of the <OBJECT> tag is specified. The CODEBASE parameter specifies a URL to the ActiveX control. The CODEBASE tag is used like this:

<OBJECT

ID=iexr2

TYPE="application/x-oleobject"

CLASSID="clsid:0CA4A620-8E3D-11CF-A3A9-00A0C9034920"

CODEBASE="http://activex.microsoft.com/controls/iexplorer/iestock.

ocx#Version=4,

70,0,1161"

WIDTH=300

HEIGHT=50>

When the user hits the page containing this tag, Internet Explorer checks to see if the control is already registered on the user's machine. If it is not, the security of the control is verified and, depending on the user's security preferences, the user is given the option of installing the control on his or her machine. If the user answers in the affirmative, the control is installed by Internet Explorer.

After distributed ActiveX objects are downloaded and installed, they usually are stored in an object cache on the client machine.

Examining ActiveX Controls

Microsoft has made available a number of controls that you can use in your own VBScript HTML pages. Keep in mind that in order for these controls to work, it's up to the client (your reader) to install them on his or her machine. These controls are freely distributable and can be made available to people interested in seeing and using your implementation of the object on their machines.

Animated Button Control

The Animated Button control takes a small Video for Windows (.AVI) file and allows you to use various combinations or frames for different button states. For example, if a file has 15 frames, the first five frames may be played in the button's default state. The second five frames can be reserved for the Mouseover state and the last five for the Mousedown state. This is a powerful control that offers more control than an animated GIF file. Table 7.3 lists the properties for the Animated button control.

Table 7.3. Animated button control properties.

Property

Description

DefaultFrStart

Starting frame for default state

DefaultFrEnd

Ending frame for default state

DownFrStart

Starting frame for mousedown state

DownFrEnd

Ending frame for mousedown state

FocusFrStart

Starting frame for focus state

FocusFrEnd

Ending frame for focus state

MouseoverFrStart

Starting frame for mouseover event

MouseoverFrEnd

Ending frame for mouseover event

URL

Address of the AVI file used

The Animated button control has only one method, AboutBox. This method will call up the About box for the control. This control has five events. Table 7.4 lists these events, along with a description of each.

Table 7.4. Animated button control events.

Event

Description

ButtonEvent_Click

The user clicks the button.

ButtonEvent_DblClick

The user double-clicks the button.

ButtonEvent_Enter

The mouse pointer passes over the

 

button.

ButtonEvent_Leave The mouse pointer leaves the button area.

ButtonEvent_Focus

The button gets focus.

One drawback to the Animated button control is that the AVI file format isn't well compressed. This can make the size of the files that you would like to use a little too large for use on the Internet. As bandwidth increases for home users, this will become less of a problem.

Chart Control

The Chart control works just like any chart you might use in Excel or Visual Basic. You provide a matrix of numeric values, and a chart is created that models those values. Using the Chart control, you can provide your user with up-to-the- minute feedback regarding a set of numbers. Table 7.5 shows the properties of the Chart control.

Table 7.5. Chart control properties.

Property

Description

BackStyle

0 for transparent; 1 for opaque

ChartType

One of the following: 0=Simple Pie

 

1=Pie with wedge out

 

2=Point Chart

 

3=Stacked Point

 

4=Full Point

 

5=Simple Line

 

6=Stacked Line

 

7=Full Line

 

8=Simple Area

 

9=Stacked Area

 

10=Full Area

 

11=Simple Column

 

12=Stacked Column

 

13=Full Column

 

14=Simple Bar

 

15=Stacked Bar

 

16=Full Bar

 

17=HLC Stock

 

18=HLC Stock WSJ

 

19=OHLC Stock

 

20=OHLC Stock WSJ

ColorScheme

One of three predefined values: 1, 2, or 3

ColumnIndex

Index value of the current column

Columns

Number of columns in the matrix

DataItem

Data value for a RowIndex, ColumnIndex

 

pair

DisplayLegend

0 shows legend; 1 hides legend

HorizontalGrid

0=No Grid, 1=Lines

GridPlacement

One of the following: 0=None 1=Lines

RowIndex

Index value of the current row

Rows

Number of rows in the matrix

Scale

Percentage scale; default is 100%

VerticalGrid

0=No Grid, 1=Lines

URL

Data file to be read into control

You can set the values in the chart at load time and change them at runtime. The Chart control supports only one method, AboutBox, and doesn't generate any events.

Listing 7.1 shows how you can use the Chart control in an HTML document. Entering numeric values in the text boxes

changes the style of the chart in the page. (See Figure 7.1.)

Figure 7.1 : The Chart Wizard in Internet Explorer.

Listing 7.1. Using the Chart control in a reference document.

<HTML>

<HEAD>

<TITLE>Chart Explorer</TITLE>

</HEAD>

<BODY BGCOLOR=WHITE>

<CENTER>

<FONT FACE="Comic Sans MS">

<H1>Chart Explorer</H1>

<HR COLOR="BLUE" WIDTH=85%>

<TABLE BORDER BORDERCOLOR=RED>

<TR><TD COLSPAN=2>

<OBJECT ID="chart1" WIDTH=220 HEIGHT=155

CLASSID="CLSID:FC25B780-75BE-11CF-8B01-444553540000">

<PARAM NAME="_ExtentX" VALUE="5821">

<PARAM NAME="_ExtentY" VALUE="4101">

<PARAM NAME="Rows" VALUE="4">

<PARAM NAME="Columns" VALUE="3">

<PARAM NAME="ChartType" VALUE="0">

<PARAM NAME="Data[0][0]" VALUE="9">

<PARAM NAME="Data[0][1]" VALUE="10">

<PARAM NAME="Data[0][2]" VALUE="11">

<PARAM NAME="Data[1][0]" VALUE="7">

<PARAM NAME="Data[1][1]" VALUE="11">

<PARAM NAME="Data[1][2]" VALUE="12">

<PARAM NAME="Data[2][0]" VALUE="6">

<PARAM NAME="Data[2][1]" VALUE="12">

<PARAM NAME="Data[2][2]" VALUE="13">

<PARAM NAME="Data[3][0]" VALUE="11">

<PARAM NAME="Data[3][1]" VALUE="13">

<PARAM NAME="Data[3][2]" VALUE="14">

<PARAM NAME="HorizontalAxis" VALUE="0">

<PARAM NAME="VerticalAxis" VALUE="0">

<PARAM NAME="hgridStyle" VALUE="0">

<PARAM NAME="vgridStyle" VALUE="0">

<PARAM NAME="ColorScheme" VALUE="0">

<PARAM NAME="BackStyle" VALUE="1">

<PARAM NAME="Scale" VALUE="100">

<PARAM NAME="DisplayLegend" VALUE="1">

<PARAM NAME="BackColor" VALUE="16777215">

<PARAM NAME="ForeColor" VALUE="32768">

</OBJECT>

</TD>

</TR>

<TR>

<TD ALIGN=RIGHT>Chart Type:</TD><TD><INPUT TYPE="TEXT"

NAME="CT"></TD>

</TR>

<TR>

<TD ALIGN=RIGHT>Chart Colors:</TD><TD><INPUT TYPE="TEXT"

NAME="CC"></TD>

</TR>