Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Web Database Developer's Guide with Visual Basic 5

World Wide Web Database Developer's Guide with Visual Basic 5




-13-

ActiveX and ActiveX Controls

The incorporation of <OBJECT>s in HTML 3.2 signaled the beginning of a new era in Web applications development. That might sound like a strong statement, but think about it for a moment. The <OBJECT> tag enables developers to incorporate virtually any object into a Web page. As a result, the possible ways to expand the functionality and interactiveness of your Web pages is becoming limited more by a developer's imagination than by technological barriers.



As you will begin to see in this chapter and later chapters, one of the easiest yet most powerful ways to extend the functionality of your Web applications is by using ActiveX technologies in general and ActiveX controls in particular. ActiveX controls are complex objects that can be inserted into your Web pages and manipulated and controlled by using VBScript. By using HTML <OBJECT>s and ActiveX controls with VBScript, developers quickly can incorporate components that suit their needs and build the type of powerful client-side applications that until just recently were simply not technologically possible.

What Is ActiveX?

ActiveX isn't any single thing; it is a set of new technologies unveiled by Microsoft in early 1996. Simply stated, ActiveX is a language-neutral set of integration technologies that enable software components to work together in a networked environment. This means that, regardless of the language used for development, the components still interoperate and communicate properly. ActiveX defines the packaging of software components and how those components should communicate. As a result, ActiveX components can be

These qualities of ActiveX enable developers to easily create interactive applications and Websites using an open development framework.

The set of ActiveX technologies forms a framework for creating interactive Web content through the use of software components, scripts, and existing applications. Specifically, ActiveX technologies enable content providers and applications developers to easily create dynamic Web content and Web server extensions by using ActiveX controls, active scripting, and active document interfaces. You will learn more about these elements later in this chapter.

Why Use ActiveX?

Perhaps a better question would be, "Why wouldn't you use ActiveX?" This is a question for which we don't have a good answer. ActiveX simply enables developers to do too many good things to ignore. Consider these capabilities:

ActiveX Background and History

ActiveX is the result of a natural evolution and growth of existing Microsoft technologies. These technologies include the following controls:

VBXs and OCXs enable you to quickly and easily expand the function of a program simply by inserting these components into your program to add needed functionality. As a result, the power of your programs can be expanded quickly, easily, and reliably by taking advantage of the communications, control, and functionality provided by these controls.

Because OCX technology is such a powerful programming model, it was only a matter of time before it was applied to Web applications development using rapidly maturing scripting languages such as VBScript. Technologically, however, an impediment still existed to fully applying this model to Web applications development. The steady evolution of controls in the Windows environment resulted in technologies that were somewhat bloated and platform specific:

These problems were contrary to what was needed for controls that could be used on the Web:

Microsoft addressed these needs by taking the best portions of OCX technologies and adding to them.

The resultant set of technologies was named ActiveX. Along the way, it was broadened significantly to incorporate not only ActiveX (formerly OCX) controls, but other technologies, such as active scripting and active documents central to Microsoft's Internet strategy.

ActiveX technologies are centered on the concept of treating applications, as well as traditional Web pages, as active documents that can be hosted in an all-encompassing shell. The idea is that the next generation Web browser will be capable of hosting nearly all interactions with the computer. In effect, the wide chasm that currently exists between operating system and browser will become much smaller as they are merged (at least in the Windows environment).

The Component Object Model (COM)

The Component Object Model (COM) is a client/server, object-based model that is the basic technical foundation cornerstone for ActiveX components. The model is designed to enable software components and applications to interact, even across networks, in a standard and uniform way. The COM standard is really partly a specification and partly an implementation:

Some characteristics of COM follow:


RESOURCE: You can find additional information, as well as complete COM and DCOM specifications, on Microsoft's OLE Development Website at
http://www.microsoft.com/oledev/




How COM Works

As stated previously, COM is a client/server-based model. When a client object wants to use the services provided by a server COM object, it uses one of the core services that are part of the COM library. The COM library is responsible for creating the server COM object and then establishing an initial connection between the client and server objects.

The connection is considered established when the server object returns a pointer to the client. This pointer points to an interface in the server object. The COM library now has completed its role in the process and leaves the objects to communicate directly with one another.

COM objects use interfaces to communicate with one another. An interface is a set of related functions that provide some sort of required services. Interfaces are language-independent. Any language capable of creating structures of pointers, as well as calling functions via pointers, can be used to implement COM interfaces.

COM objects can have multiple interfaces. After a client object obtains a pointer to a server object's interface, the client can invoke any of the functions or services provided by that interface. After an object finishes using services provided by the server object, the client simply informs the server that it is all done and terminates the connection between the two.

What Is a COM Interface?

COM interfaces are small sets of language-independent functions that are related in some way--logically or semantically. These interfaces provide the mechanism by which server objects make services available to client objects.

COM interfaces are given unique names by using globally unique identifiers (GUIDs). These identifiers are 128-bit numbers that are used to avoid name clashes that easily could occur by using human-readable names. GUIDs are guaranteed to be (nearly) unique by the COM specification. This specification indicates that 10 million GUIDs per second could be generated over the next 3,500 years (approximately) and that each of these GUIDs would be unique. This is unlikely to be the case with any human-readable form of naming.

COM server objects can supply more than one type of service; a separate interface is provided for each service. When a client object obtains a pointer from the server, it provides access only to the services provided by that specific interface. Clients can obtain pointers to other server interfaces through the IUnknown interface. IUnknown is a fundamental interface through which all other interfaces are derived, and it must be supported by all COM objects.

The IUnknown interface has a function called QueryInterface. Because all interfaces are derived from IUnknown, this function is present in all interfaces. QueryInterface has knowledge of all interfaces in the server object and therefore can provide a client with a pointer to any server interface to which it requires access.

COM Server Object Types

COM specifies three types of server objects:

Regardless of where a server object is running (locally or remote), client objects always communicate with it in the same way. A client object accesses the services provided by a server object via a pointer to an interface on the server. For servers running in-process, the pointer can access the interface directly. For servers running locally or remotely, however, the pointer accesses a COM-supplied proxy server running in the same process space as the client. The proxy then generates a call to the local or remote server object on behalf of the client. COM server objects use a stub object supplied by COM. This stub receives calls from client objects and translates these to calls on the appropriate interface.

COM Foundation Components

COM primarily is concerned with the business of creating objects and facilitating standard communication between those objects. However, COM also provides a few other fundamental system-level objects:

The ActiveX Object Model

The ActiveX Object Model consists of the following two pieces, both of which are in-process (DLL-based) COM types:

All interfaces defined in the ActiveX Object Model are dual interfaces. In dual interfaces, the objects inherit and follow some simple rules. Therefore, they can be used by late-bind OLE Automation controllers (through the use of IDispatch::GetIdsOfNames and IDispatch::Invoke). They also can be used by early-bind controllers (for example, using a vtable).

The MSHTML Viewer Component

MSHTML is the HTML viewer part of the Microsoft Internet Explorer 3.0 (IE 3.0) browser. It can be hosted in both OLE document object containers and OLE control containers. It also can be used as a standalone OLE Automation component.

MSHTML implements the OLE Automation Object Model described by the HTML Scripting Object Model. With this object model, you can develop rich, interactive, multimedia HTML content. Client-side scripts (such as VBScript running in the browser), as well as externally running programs (such as VB 4 and VB 5), can use the object model.

The IExplorer Browser Object

The IExplorer Browser object is an in-process COM server. The IExplorer Browser is the core of what users see when using Internet Explorer 3.0. The Browser object also is a document object container. This container can host any document objects (such as Word, Excel, and even HTML) with the added benefit that it fully supports hyperlinks to any document type. Because it is a COM class object that implements all the interfaces needed to be used as an OLE control, the IExplorer Browser is itself an OLE control.

The IExplorer's IShellExplorer interface is the primary OLE Automation-compatible interface exposed by the IExplorer Browser OLE control. The IInternetExplorer interface is a superset of the IShellExplorer interface. It can be contained within any OLE control container, such as Internet Explorer 3.0, which raises some interesting possibilities. Imagine the added functionality you could give users by enabling them to embed and control a browser from within your Web applications (a browser-within-a-browser) as well as your VB applications.

ActiveX Controls

ActiveX controls are reusable software components developed by software vendors as well as VB developers like yourself. You easily can add these controls to Web pages by using the <OBJECT> tag. When used with VBScript, these controls can dramatically increase the interactiveness, functionality, and usefulness of your Website.

Straight COM objects combined with a few ActiveX interfaces can become Internet-enabled without modifying the existing object. You can create ActiveX controls from a variety of existing object types, such as the following:

ActiveX controls provide a means of instantly adding new and dynamic functions to Web applications. These controls are the building blocks for client- or server-driven dynamic content. Controls exist that enable you to integrate virtually any media type (such as sound, video, animation, interactive input devices, and so on) into your Web applications. Later sections of this chapter and subsequent chapters present a variety of controls that you easily can obtain and implement rapidly. The use of these controls will be demonstrated in several examples.

ActiveX Scripting Services

ActiveX scripting defines a syntax- and language-independent architecture that enables you to add scripting and OLE Automation capabilities to programs. ActiveX scripting provides the necessary platform for developing script engines.

ActiveX scripting components can be grouped into two major categories:

ActiveX Documents

ActiveX documents are based on OLE document objects (DocObjects). The DocObjects technology is a set of extensions to OLE documents; it is the compound document technology provided by OLE. Documents that conform to the ActiveX standard can be opened within ActiveX document containers. In other words, they can be viewed within Internet Explorer 3.0. Documents that conform to the ActiveX standard follow:

Note that Binder is a sophisticated viewer capable of hosting a variety of documents so that you can create, edit, save, print, and view different kinds of information collectively. Internet browsers (such as IE 3) that adopt Binder technology not only facilitate the presentation of Internet-based information (such as HTML-based Web pages), but they also enable the browser to present documents from Microsoft Office and Microsoft Office-compatible applications. In the future, you will be able to view, navigate, and distribute virtually any document type (whether it is local or remote) by using browsers that adhere to ActiveX and DocObject container standards.

The ActiveX Server Framework

The ActiveX Server framework is based on the Microsoft Internet Information Server (IIS), which is an integrated component of Windows NT Server. The framework enables Web developers to take advantage of the powerful Microsoft BackOffice suite of products (which includes Microsoft SQL Server and Microsoft Exchange Server). The ActiveX Server framework consists of ActiveX Server scripting and ActiveX Server controls. ActiveX Server scripting can be written with any scripting language, including VBScript, Perl, C, or JavaScript. The CGI also is supported under the ActiveX Server framework. Through CGI, you can extend the functionality of your Web applications by giving clients access to services (such as databases) that they ordinarily would not be able to access.

Using ActiveX Controls in Your Applications

Remember that ActiveX controls enable you to rapidly improve the quality and functionality of your Web applications simply by embedding prepackaged objects in your Web pages. By adding VBScript programs to control these objects and handle various events, you can develop interactive and highly functional client-side applications that simply were not possible before. This section presents a few basics on using objects in your applications. Examples in later chapters discuss using ActiveX controls in greater detail.

Three primary sources exist for ActiveX controls. Microsoft provides many ActiveX controls with IE and a development tool called the ActiveX Control Pad. Table 13.1 summarizes these controls. Third-party vendors produce a dazzling number of ActiveX controls designed to perform specific tasks. Note that the control you need often already exists and can be cheaper to purchase than to write, debug, and maintain yourself. The third source for controls is you (and other benevolent developers who make their controls freely available on the Internet). Why would you write an ActiveX control? Sometimes you want to integrate some type of functionality into your program, but you cannot locate a control on the market that adequately meets your needs. In these cases, you have to do it yourself. Fortunately, VB 5 and VB 5 Control Creation Edition provide everything you need (including control-creation wizards) to develop your own controls.


RESOURCE: Although Microsoft has hinted that ActiveX will make its way into the UNIX world, ActiveX technologies currently are geared primarily toward the Microsoft environment. Internet Explorer 3.0 currently is the only Web browser that offers native support for ActiveX. NCompass Labs, however, has developed plug-ins for Netscape Navigator that provide the capability to use ActiveX controls, VBScript, and ActiveX documents with Navigator.

You can find more information on NCompass Labs and trial downloads at

http://www.ncompasslabs.com/


Controls Available with IE 3.0 and the ActiveX Control Pad

An increasing number of ActiveX controls are available for developers to use in their Web applications. Although a good number of these must be purchased from third-party vendors, Microsoft provides free a fair number of controls. Table 13.1 outlines those controls that ship with IE 3.0 and the ActiveX Control Pad.

Table 13.1. Standard ActiveX Controls that Ship with Internet Explorer and the ActiveX Control Pad.

Control Function

Web Browser Provides a Web browser control (based on IE 3.0) that can display HTML pages, ActiveX controls, and ActiveX documents. It can be embedded within HTML documents to provide a browser within a browser.
Timer Provides a timer control that can be programmed to execute actions/scripts at set time intervals.
Marquee Provides a control built into Internet Explorer that scrolls any HTML file in a horizontal or vertical direction. The amount and delay of scrolling are configurable.

The following control ships with the complete installation of Internet Explorer.

ActiveMovie Displays streaming and non-streaming media (video, sound, and synchronized images with sound).


The following controls ship with the complete installation of Internet Explorer as well as the ActiveX Control Pad.

HTML Layout Supports new HTML extensions published by the World Wide Web Consortium (W3C) that enable objects to be positioned exactly on a Web page. It displays 2D HTML regions inside IE 3.0 using new extensions to HTML.
MS Forms 2.0 Label Creates basic text labels.
MS Forms 2.0 Textbox Provides multiline text-entry and text-display windows.
MS Forms 2.0 Combo Box Enables users to choose options from a drop-down list.
MS Forms 2.0 List Box Enables users to choose options from a scrollable list.
MS Forms 2.0 CheckBox Enables users to check an option.
MS Forms 2.0 Option Button Enables users to choose between multiple options.
MS Forms 2.0 Toggle Button Provides a two-state toggle button.
MS Forms 2.0 Command Button Provides a basic pushbutton control.
MS Forms 2.0 Tabstrip Enables users to select multiple pages via tabs. A nice real-estate-saving control.
MS Forms 2.0 ScrollBar Provides basic horizontal and vertical scrollbars.
MS Forms 2.0 Spin Button Provides a button that can be "pushed" up or down.
MS ActiveX Image Progressively displays images in metafile, .JPG, .GIF, .BMP, or wavelet formats.
MS ActiveX Hotspot Adds transparent hotspots to the HTML layout control.

Microsofts ActiveX Control Component Gallery

In addition to the standard controls available with Internet Explorer and the ActiveX Control Pad, Microsoft makes several other controls available on its ActiveX Gallery Website. Table 13.2 lists the standard set of controls available. Note that this listing represents a minimal subset of the rapidly growing number of controls available from Microsoft and third-party vendors. This Website also provides links to several third-party controls available for downloading and evaluation.


Table 13.2. Standard Microsoft ActiveX Controls Available from the ActiveX Gallery Website

(http://www.microsoft.com/activex/gallery). 

Control Function
Animated Button Displays various frame sequences of an .AVI file, depending on the button state. It uses the Windows Animation common control. The .AVI file must be RLE compressed or 8-bit compressed.
Chart Enables you to draw various types of charts with different styles. Great for providing graphical representations of data, such as that derived from database queries.
Gradient Shades a given area with a range of colors (making the transition from a specified color to another specified color).
Label Enables you to render and display text at any specified angle or along user-defined curves. The control supports Click, Change, MouseDown, MouseOver, and MouseUp events.
Menu Enables developers to place menu buttons on the page that brings up a menu with the standard menu look and feel. Generates events that can be used in VBScripts.
Popup Menu Displays a pop-up menu in response to the PopUp method being called. Generates an event after a menu item is clicked.
Popup Window Displays a specified HTML document in a pop-up window. Can be used to provide tool tips or preview links.
Preloader Downloads a file from a specified URL and puts it into the cache. Invisible at runtime and starts downloading when enabled. When the download finishes, the control fires a Complete event.
Stock Ticker Displays continuously changing data by downloading the specified URL at regular intervals. The data can be in a text or .XRT format.
View Tracker Generates events whenever the control enters or leaves the viewing area. Developers can use this control to automatically modify properties or functionality on the page based on sectional input.


RESOURCE:The Microsoft ActiveX Component Gallery at
http://www.microsoft.com/activex/gallery

has numerous links to Microsoft and third-party controls available for downloading and evaluation. Additionally, the site at

http://www.browserwatch.com

provides several links to controls developed by software vendors and other independent control developers.


Registering a Control

When ActiveX controls (OCX files) are installed on your system, they are registered with the operating system database known as the Registry. All ActiveX controls are referenced in Web pages by their unique class identifier (CLSID). Registering an OCX is a matter of placing this CLSID into the Registry. When you load the OCX file into an ActiveX Chart object, for example, the following CLSID is written into your system's Registry:

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

This CLSID is called whenever a Web page needs to instantiate an instance of this Chart control.

Adding Controls to HTML Documents

Let me preface this section by stating that I don't really advocate adding controls to your HTML pages by hand. It is simply too easy to mistype something--especially CLSID strings. Additionally, a variety of tools exist that support easy insertion of controls into your documents; these tools include the Microsoft ActiveX Control Pad, Microsoft FrontPage, and SoftQuad's HotMeTaL product. I highly recommend that you at least download and learn how to use the ActiveX Control Pad (available at the Microsoft site http://www.microsoft.com/workshop/ author/cpad/). The ActiveX Control Pad not only enables you to easily insert controls, but it also provides a text editor for editing HTML; an object editor for easily inserting controls (and their hideously long CLSIDs); a page editor to create HTML layouts (you'll see more information on these layouts in later chapters); and a script wizard that provides easy access to a control's methods, properties, and events.

With those capabilities in mind, you still should understand how an ActiveX control is represented in your HTML. ActiveX controls are placed in HTML by using the <OBJECT> tag, which also includes the control's CLSID and other information (such as height and width) as tag attributes. You can set various property values for the control by including <param> tags within the <OBJECT>, </OBJECT> tag pair. ActiveX controls also have sets of properties and methods that can be set and reacted to by using VBScript. Listing 13.1 shows how you can place an ActiveX Label control on your page. The VBScript code shows how the Label's Click event is handled and used to display a simple dialog box.

Listing 13.1. Placing an ActiveX Label Control on Your Page.

<OBJECT ID="lblCat"
  CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0"
  STYLE="TOP:104pt;LEFT:6pt;WIDTH:71pt;HEIGHT:17pt;ZINDEX:6;">
        <PARAM NAME="ForeColor" VALUE="16711680">
        <PARAM NAME="BackColor" VALUE="16777215">
        <PARAM NAME="Caption" VALUE="Categories:">
        <PARAM NAME="Size" VALUE="2505;600">
        <PARAM NAME="FontName" VALUE="Arial">
        <PARAM NAME="FontEffects" VALUE="1073741825">
        <PARAM NAME="FontHeight" VALUE="240">
        <PARAM NAME="FontCharSet" VALUE="0">
        <PARAM NAME="FontPitchAndFamily" VALUE="2">
        <PARAM NAME="FontWeight" VALUE="700">
</OBJECT>

Sub lblCat_Click()
   MsgBox "You clicked my label!"
End Sub

As you can see from this example, it is worth your while to learn how to use the ActiveX Control Pad or some other tool instead of typing all this for every control you decide to use.

Installing and Displaying Controls

In the scheme of things, not too many of the existing ActiveX controls are included with Internet Explorer or the ActiveX Control Pad. Instead, most controls are third-party controls or controls that you've developed. The upshot of this is that the controls don't initially exist on your users' machines, and in some way must exist for your users to get the controls and register them in their system's Registry.

Fortunately, ActiveX controls are designed so that if they don't already exist on the user's machine, they can be obtained from the source. This capability is supported by adding the CODEBASE attribute to the <OBJECT> tag when the control is added to your HTML. This attribute specifies the URL at which the control is located. When Internet Explorer encounters CODEBASE in an HTML page, for example, it checks the system Registry to see whether the control already is registered. If it isn't, the URL is used to download and register this control (subject to security settings on the user's system). After this one-time download, the control is maintained locally in an object cache on the user's machine.

The following code snippet illustrates how the CODEBASE tag is used to obtain a specific version of a control from Microsoft:

<OBJECT ID=iexr2 TYPE="application/x-oleobject"
CLSID="clsid:0CA4A620-8E3D-11CF-A3A9-00A0C9034920"
CODEBASE="http://activex.micorosoft.com/controls/iexplorer/
Âiestock.ocx#Version=4,70,0,1161">

Summary

This chapter gave you an overview of ActiveX and a glimpse at how this set of technologies gives you the opportunity to rapidly and easily improve the quality, interactiveness, and functionality of your Web applications. You now should understand the important role that ActiveX technologies will play in the future of Web applications development.

The chapter began with a brief overview of what ActiveX is and how it evolved from earlier Microsoft technologies, such as OLE and custom controls. You then looked at the Component Object Model (COM) and gained some insight into how ActiveX components actually are built on this client/server object model. Next, you examined the ActiveX Object Model. The main components of ActiveX technology were outlined: ActiveX controls, ActiveX scripting, ActiveX documents, and the ActiveX Server framework.

The remainder of the chapter was dedicated to ActiveX controls. You were introduced to a number of standard controls that are readily available from Microsoft. You also learned how these controls are registered on your system, added to Web pages, and distributed via the Web.