?? ch24.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1"> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 24 -- Creating ActiveX Controls</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1><CENTER><P><A HREF="../ch23/ch23.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../apa/apa.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 24 -<BR>Creating ActiveX Controls</FONT></H1><P>As discussed in Hour 20, "Using ActiveX Controls," ActiveX controlsare components that can be used to easily add new functionality to your application.In this hour, you will learn about<UL> <LI>Some of the internal plumbing that is required for an ActiveX control<BR> <BR> <LI>The support provided by MFC for ActiveX control development<BR> <BR> <LI>How to test ActiveX controls using tools supplied with Visual C++</UL><P>To help demonstrate these topics, you also will create and test an ActiveX controlthat you can use in your own projects.<H2><FONT COLOR="#000077"><B>What Is an ActiveX Control?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The <I>Component Object Model</I>,or <I>COM</I>, is a specification that defines how software components should cooperatewith each other. ActiveX technologies are all built on top of the COM specification.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>In COM, an <I>interface</I>is a group of related functions that are implemented together. All interfaces arenamed beginning with <TT>I</TT>, such as <TT>IDataObject</TT>.</P><P>At a minimum, an ActiveX control must be a COM object. This means that an ActiveXcontrol must support <TT>IUnknown</TT>, the interface supported by all COM objects.This allows for a great deal of latitude when deciding how a control is to be implemented;previously, the OLE custom control architecture required support of at least 14 interfaces.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Support for the <TT>IUnknown</TT> interface is provided automatically when you use MFC and ControlWizard to build your control. <HR></BLOCKQUOTE><P>Reducing the number of required interfaces allows ActiveX controls to be muchsmaller than the older OLE controls. It also makes it feasible to use ActiveX controlsto implement functionality where the size of the control is an important factor.Web pages can be more intelligent when a control is downloaded and activated to yourbrowser. For example, Microsoft's Internet Explorer has support for downloading ActiveXcontrols from a Web page. Although this opens a lot of exciting functionality, thesize of the control to be downloaded must be kept as small as possible.<H2><FONT COLOR="#000077"><B>ActiveX Control Properties, Events, and Methods</B></FONT></H2><P>Interaction with an ActiveX component takes place via properties, events, andmethods.<UL> <LI>A <I>property</I> is an attribute associated with the control.<BR> <BR> <LI>An <I>event</I> is a notification message passed to the container by the control.<BR> <BR> <LI>A <I>method</I> is an exposed function that can be applied to the control via <TT>IDispatch</TT>.</UL><P>You'll learn about each of these interaction methods in the next few sections.<H3><FONT COLOR="#000077"><B>Properties</B></FONT></H3><P>Properties are exposed by ActiveX controls, as well as by the client where thecontrol is located. There are four basic types of properties:<UL> <LI><I>Ambient</I> <I>properties</I> are provided to the control by the container. The control uses these properties in order to "fit in" properly. Commonly used ambient properties include the container's background color, default font, and foreground color.<BR> <BR> <LI><I>Extended</I> <I>properties</I> are implemented by the container but appear to be generated by the control. For example, the tab order of various controls in a container is an extended property.<BR> <BR> <LI><I>Stock</I> <I>properties</I> are control properties implemented by the ActiveX control development kit. Examples of stock properties are the control font, the caption text, and the foreground and background colors.<BR> <BR> <LI><I>Custom</I> <I>properties</I> are control properties that you implement.</UL><H3><FONT COLOR="#000077"><B>Events</B></FONT></H3><P>An event is used to send a notification message to the control's container. Typically,events are used to notify the container when mouse clicks or other events take place.There are two basic types of events:<UL> <LI><I>Stock events</I> are implemented by the ActiveX control development kit and are invoked just like a function call, such as <TT>FireError</TT>.<BR> <BR> <LI><I>Custom events</I> are implemented by you, although the MFC class library and ClassWizard handle much of the work for you.</UL><H3><FONT COLOR="#000077"><B>Methods</B></FONT></H3><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B><I>OLE Automation</I>, nowoften referred to as just <I>Automation,</I> is a way of enabling a COM object tobe controlled by another party. Automation is provided through the <TT>IDispatch</TT>interface.</P><P>Automation was originally developed so that interpreted languages such as VisualBasic could control COM objects. Now most ActiveX controls use Automation to allowall sorts of programs--even those built using scripting languages such as JScriptand VBScript--access to the methods of ActiveX controls.<H2><FONT COLOR="#000077"><B>An ActiveX Control Example</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>S<I>ubclassing</I> is a methodof borrowing functionality from an existing window or control. By subclassing anexisting window or control, you can concentrate on adding only the new features offeredby your control. The control from which the functionality is borrowed is known asthe <I>superclass</I>.</P><P>As an example of creating an ActiveX control, you will now create an ActiveX controlnamed OleEdit that subclasses the existing Windows edit control. The OleEdit controlis similar to the basic Windows edit control, except that it exposes properties thatallow it to accept only numbers, letters, or a combination of both.</P><P>The control works by performing extra processing when the <TT>WM_CHAR</TT> messageis received by the control. Windows sends <TT>WM_CHAR</TT> to notify the edit controlthat the user has pressed a key on the keyboard. Ordinarily, the edit control wouldsimply add the new character to the edit control. When the <TT>WM_CHAR</TT> messageis received by the OleEdit control, it is processed as shown in Figure 24.1.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 24.1.</B></A> <BR><I>Handling <TT>WM_CHAR</TT> in OleEdit.</I></P><P>The property flags <TT>m_fTextAllowed</TT> and <TT>m_fNumbersAllowed</TT> areexposed as properties that can be changed by the OleEdit control's container.<H3><FONT COLOR="#000077"><B>Creating the Project</B></FONT></H3><P>To begin creating the OleEdit control, use the ControlWizard. Using ControlWizardto build a control is very much like using AppWizard to build applications. To startControlWizard and create the OleEdit control, follow these steps:<DL> <DD>1. Select New from the File menu. The New dialog box is displayed.<BR> <BR> 2. Select the Projects tab. A list of project types is displayed.<BR> <BR> 3. To create an ActiveX control, select MFC ActiveX ControlWizard as the project type.<BR> <BR> 4. Specify OleEdit as the project name; a default location for your project will automatically be provided for the location.<BR> <BR> 5. Make sure the Create New Workspace radio button is selected, and click OK to create the project.<BR> <BR> 6. The initial page from the ControlWizard is shown in Figure 24.2. This page enables you to specify the basic characteristics of the project, such as the number of controls handled by this server, whether help files should be generated, and so on. Accept all the default options presented on this page by clicking the Next button.</DL><P><A NAME="02"></A><A HREF="02.htm"><B>Figure 24.2.</B></A> <I><BR>The first page of the ActiveX ControlWizard.</I><DL> <DD>7. The second ControlWizard page is shown in Figure 24.3. This page lets you change the names associated with the control and its OLE interfaces, as well as define properties for the control. There is also a drop-down list that allows a base class to be specified for the control. Select Edit from the drop-down list to make the OleEdit control a subclass of the standard edit control.</DL><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 24.3.</B></A> <I><BR>The second page of the ActiveX ControlWizard.</I><DL> <DD>8. Click the Finish button. As with other ControlWizard projects, a list of the files to be created is displayed. Click OK, and the skeleton project is created.</DL><H3><FONT COLOR="#000077"><B>MFC Support for ActiveX Controls</B></FONT></H3><P>A set of MFC classes is used as a framework for all ActiveX controls built usingControlWizard. ClassWizard creates three classes that are specific to your project,using these three classes as base classes:<UL> <LI><TT>COleControlModule</TT> is the class that manages the ActiveX control module. This class plays a role similar to the <TT>CWinApp</TT> class used in applications built using AppWizard. For the OleEdit project, the derived class is named <TT>COleEditApp</TT>.<BR> <BR> <LI><TT>COleControl</TT> is the base class that represents the actual control window. This class is derived from <TT>CWnd</TT> and includes extra ActiveX functionality for communicating with containers. For the OleEdit project, the derived class is named <TT>CTestCtrl</TT>.<BR> <BR> <LI><TT>COlePropertyPage</TT> is the base class used to manage the property page for the control. For the OleEdit project, the derived class is named <TT>CTestPropPage</TT>.</UL><H3><FONT COLOR="#000077"><B>Drawing the Control</B></FONT></H3><P>All visible OLE controls must be capable of drawing themselves. Even controlsthat aren't visible when active should draw something as an aid during program development.The OleEdit control is visible at runtime, and it should appear to be a standardedit control.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>You might think that because OleEdit is subclassed from the standard edit control, it can draw itself. Unfortunately, very few controls actually draw themselves properly; the edit control is not one that handles drawing properly. For most controls, you must be prepared to handle the drawing yourself. <HR></BLOCKQUOTE><P>When an ActiveX control project is initially created, the control's <TT>OnDraw</TT>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -