ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾C/C++±à³Ì£¾Teach Yourself Visual C++ 6 in 21 Days


Teach Yourself Visual C++ 6 in 21 Days

Previous chapterNext chapterContents


- 9 -
Adding ActiveX Controls to Your Application



In today's application develop market, there are thousands of prebuilt components that you can plug into your applications, extending the functionality of your applications instantaneously. Originally the domain of Visual Basic programmers, now you can use readily available ActiveX controls with just about any Windows programming language, including Visual C++. Today you will learn how you can add ActiveX controls to your Visual C++ applications, taking advantage of their existing functionality. Some of the topics that you will cover today are

  • What ActiveX controls are and how they work.

  • How you can add ActiveX controls to your project workspace.

  • How you can use the ActiveX control in your Visual C++ application.

  • How to call the various methods associated with the ActiveX control.

  • How to handle events that are triggered by the ActiveX control.

What Is an ActiveX Control?

An ActiveX control is a software component that can be plugged into many different programs and used as if it were a native part of the program. It's similar to the concept of separate stereo components. If you buy a new tape deck, you can just plug it into the rest of your stereo and it works with everything else you already have. ActiveX controls bring this same type of interoperability to software applications.

ActiveX used to be called OLE 2.0. OLE 2.0 was Microsoft's technology for combining two or more applications to make them work as one (or at least to switch between the various applications within the same application shell). This idea was an expansion from the original OLE (Object Linking and Embedding) technology, which only enabled you to combine documents created with different applications into a single document. When revamping OLE technologies to work in a distributed environment (such as on the Internet), Microsoft decided to also revamp the name. Thus, ActiveX was born.

ActiveX and the IDispatch Interface

The ActiveX technology is built on top of Microsoft's COM (Component Object Model) technology, utilizing its interface and interaction model for making ActiveX control integration fairly seamless. The COM technology defines how ActiveX objects are constructed and how their interfaces are designed. The ActiveX technology defines a layer that is built on top of COM, what interfaces various objects should support, and how different types of objects should interact.


NOTE: Microsoft's COM technology defines how applications and components can interact through the use of interfaces. An interface is like a function call into an ActiveX component. However, COM specifies how that function call must be built and called, and what supporting functionality must accom-pany the function call.
There are interfaces, like the IUnknown interface, that are required in every COM object, and which are used to query the component to find out what other interfaces are supported by the component. Each interface supports a specific set of functionality; you might have one interface to handle the visual appearance of the control, another to control how the control appearance interacts with the surrounding application, another that triggers events in the surrounding application, and so on.

One of the key technologies in ActiveX controls is automation. Automation enables an application embedded within another application to activate itself and control its part of the user interface or document, making its changes and then shutting itself down when the user moves on to another part of the application that isn't controlled by the embedded application.

This process is what happens when you have an Excel spreadsheet embedded within a Word document. If you click the spreadsheet, Excel becomes active and you can edit the spreadsheet using Excel, even though you're still working in Word. Then, once you finish making your changes to the spreadsheet, Excel closes itself down and you can continue working in Word.

One of the keys to making automation work is a special interface called the IDispatch (also known as the dispinterface) interface. The IDispatch interface consists of a pointer to a table of available methods that can be run in the ActiveX control or embedded application. These methods have ID numbers, called DISPIDs, which are also loaded into a table that can be used to look up the ID for a specific method. Once you know the DISPID for a specific method, you can call that method by calling the Invoke method of the IDispatch interface, passing the DISPID to identify the method to be run. Figure 9.1 shows how the IDispatch interface uses the Invoke method to run methods in the ActiveX object.

FIGURE 9.1. The IDispatch ActiveX interface.

ActiveX Containers and Servers

To embed one ActiveX object within another ActiveX object, you have to implement the embedded object as an ActiveX server, and the object containing the first object must be an ActiveX container. Any ActiveX object that can be embedded within another is an ActiveX server, whether it is an entire application or just a small ActiveX control. Any ActiveX object that can have other ActiveX objects embedded within it is an ActiveX container.


NOTE: Don't confuse the use of the terms container and server with the term client in the previous figure. The client is the object calling the other object's IDispatch interface. As you'll learn in a page or so, both the container and server call the other's IDispatch interfaces, making each one the client of the other.

These two types of ActiveX objects are not mutually excl


Previous chapterNext chapterContents