Chapter 21

ActiveX Controls

In the minds of most people, the term ActiveX conjures up visions of ActiveX controls displaying fancy animations and video streams in Web pages. In truth, ActiveX controls are just one piece of the puzzle called ActiveX. But as the penultimate COM technology and the most visible member of the ActiveX family, ActiveX controls enjoy a special distinction that sets them apart from run-of-the-mill COM objects.

ActiveX controls began their life in 1994 as "OLE controls." The first version of the OLE control specification, which is now referred to as OCX 94, outlined the structure of what would one day be known as ActiveX controls and was intended to provide developers with a generic, COM-based architecture for building reusable Microsoft Windows controls. The OLE control of 1994 was a replacement for custom control DLLs and Visual Basic controls (VBXs). The specification was revised in 1996 (OCX 96), and later that same year, OLE controls were officially renamed ActiveX controls and Microsoft Internet Explorer gained the ability to host ActiveX controls in Web pages. To this day, ActiveX controls serve a dual purpose. (See Figure 21-1.) Application developers can use them to enhance their programs, and Web developers can use them to create interactive Web content—"interactive" because unlike Active Server Pages and Common Gateway Interface (CGI) scripts, ActiveX controls execute on the client side of an HTTP connection.

Figure 21-1. Two instances of an ActiveX calendar control displayed side by side—in Internet Explorer and in an application program.

Microsoft provides a number of tools for writing ActiveX controls, but none offer the balance of power and ease of use that Visual C++ and mfc do. Writing an ActiveX control from scratch can require weeks or even months of development time. You can write the same control with mfc in a matter of hours. In fact, you can write a simple control that works equally well in an application or a Web page in minutes with the tools that Visual C++ provides. One of those tools is the MFC ActiveX ControlWizard, which generates the initial source code for a control project. But make no mistake: it's MFC that's the belle of the ball, and without MFC or a similar class library to help out, writing an ActiveX control would be right up there on a list of fun things to do with having your fingernails pulled out.

In this chapter, I'll begin with an overview of ActiveX controls and an explanation of how they work. I'll conclude with a few topics that aren't treated at length elsewhere in the chapter. In between, you'll learn how MFC supports ActiveX controls. Then you'll write your own control and an application that uses the control. You'll even test the control in a Web page. When you're done, I think you'll agree that even a subject as complex as ActiveX controls can be downright enjoyable when MFC is there to do the bulk of the work.