📄 ch19.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1"> <TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 19 -- Building Your Own Widgets--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>Teach Yourself Visual C++ 6 in 21 Days</H1><CENTER><P><A HREF="../ch18/ch18.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch20/ch20.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">- 19 -<BR>Building Your Own Widgets--Creating ActiveX Controls</H1><H1></H1><UL> <LI><A HREF="#Heading1">What Is an ActiveX Control?</A> <UL> <LI><A HREF="#Heading2">Properties</A> <LI><A HREF="#Heading3">Methods</A> <LI><A HREF="#Heading4">Events</A> </UL> <LI><A HREF="#Heading5">Creating an ActiveX Control</A> <UL> <LI><A HREF="#Heading6">Building the Control Shell</A> <LI><A HREF="#Heading7">Modifying the CModArt Class</A> <LI><A HREF="#Heading8">Adding Properties</A> <LI><A HREF="#Heading9">Designing and Building the Property Page</A> <LI><A HREF="#Heading10">Adding Basic Control Functionality</A> <LI><A HREF="#Heading11">Adding Methods</A> <LI><A HREF="#Heading12">Testing the Control</A> </UL> <LI><A HREF="#Heading13">Summary</A> <LI><A HREF="#Heading14">Q&A</A> <LI><A HREF="#Heading15">Workshop</A> <UL> <LI><A HREF="#Heading16">Quiz</A> <LI><A HREF="#Heading17">Exercises</A> </UL></UL><P><HR SIZE="4"><BR>The software industry has seen a revolution over the past couple years. How softwareis built and packaged has moved from a model where all applications are large, monolithicpieces of executable code to a model where most applications consist of small buildingblocks. These small building blocks, often called components, can be created usingany of several different languages and can take many different forms. One of themost popular components is the ActiveX control. If you know how to create your ownActiveX controls, you can build your own components and provide them to other programmers.Today, you will learn</P><UL> <LI>How to use the Visual C++ wizards to build ActiveX controls. <P> <LI>How to add properties and methods to your controls using the Class Wizard. <P> <LI>How to test your control using the tools provided with Visual C++.</UL><H2><A NAME="Heading1"></A>What Is an ActiveX Control?</H2><P>An ActiveX control is a set of functionality packaged in a COM (Component ObjectModel) object. This COM object is self-contained, although it does not have the abilityto run by itself. An ActiveX control can only run within a ActiveX container, suchas a Visual C++ or Visual Basic application.</P><P>As you learned on Day 9, "Adding ActiveX Controls to Your Application,"ActiveX controls provide a series of interfaces used by the container applicationto trigger the various sets of functionality contained in the control. Many of theseinterfaces are used for triggering events in the control or in the containing application.Others are for specifying the property page of the control or for communicating whetherthe control has been activated. All in all, so many interfaces are built into mostActiveX controls that coding the functionality for each of these interfaces yourselfwould take quite some time. Luckily, the Visual C++ App and Class Wizards add muchof this functionality for you, allowing you to focus on the specific functionalitythat the control is supposed to have.</P><P>Among the aspects of the control you create that you still must plan yourselfare what properties, methods, and events you will expose for your control. You canadd these elements to your control through the Class Wizard, but if any of the propertiesor events require special code on your part, then you must add it yourself. As shouldbe expected with any methods that you add to your controls, you have to supply allof the code. The Class Wizard will add the surrounding structure and code to allowthe containing application to see and call the method, just as it will add all thecode necessary to call any event handlers for your applications.</P><P><H3><A NAME="Heading2"></A>Properties</H3><P>Properties are attributes of controls that are visible to, and often modifiableby, the container application. The four basic types of properties are ambient, extended,stock, and custom. Ambient properties are provided by the container application tothe control--such things as background color or the default font to be used--so thatthe control looks like part of the container application. Extended properties arenot actually properties of the control but instead are provided and implemented bythe container application, such as tab order. The control may extend these propertiessomewhat; for example, if the control contains two or more standard controls, itmay control the tab order within the overall control, returning the tab order controlto the application once the control has completed its internal tab order. Stock propertiesare implemented by the ActiveX control development kit, such as control font or controlbackground color. The final type of properties, custom properties, are what you aremost concerned with because these properties are specific to your control and aredirectly related to the functionality of your control.</P><P>You can specify any properties you need in your control using the Automation tabon the Class Wizard. When you add a new property to your control through the ClassWizard, you'll specify several aspects of the property.</P><P>The first aspect is the external property name, which is the name shown to thecontaining application for the property. Another aspect that you can specify is theinternal variable name, which is used in your code, but only if the property is implementedas a member variable. You also specify the variable type for the property.</P><P>If you specify that the property is to be implemented as a member variable (theproperty is a member variable of the control class), then you can specify the nameof the notification function, which is called when the property is changed by thecontaining application. If the property is not a member variable of the control class,you need to specify that it is altered and viewed through Get and Set methods, wherethe containing application calls a Get method to get the current value of the propertyand calls a Set method to change the value of the property. If the property is maintainedthrough Get and Set methods, then you can specify the names of these two methods.</P><P>For all these aspects of a property, the Add Property dialog suggests appropriatenames for everything once you enter the external name for the property. If you wantto accept the default names, the only things you need to specify are the externalname, the type, and whether the property is a member variable or uses Get and Setmethods. If you choose a stock property from the list of available stock properties,the rest of the elements are automatically specified for you. Once you specify allof this information, the Class Wizard adds all of the necessary code and variablesto your control project.</P><P><H3><A NAME="Heading3"></A>Methods</H3><P>Methods are functions in the control that can be called by the container application.These functions are made available to other applications through the IDispatch interface,which we discussed on Day 9. Because of the way the IDispatch works in calling themethods in a control, the variables passed to the method have to be packaged in astructure that is passed to the control. This structure is machine independent sothat it doesn't matter whether your control is running with Windows 95/98 on an IntelPentium II or on a Windows NT with a MIPS or Alpha processor; the structure willlook the same. It is the responsibility of each side of the function call to convertthe parameters as necessary to fit them into the structure correctly or to extractthem from the structure. This process of packaging the method parameters is calledmarshaling.</P><P>When you add a new method to your control through the Class Wizard on the Automationtab, the Class Wizard adds all of the necessary code to perform the marshaling ofthe parameters, as well as all other supporting functionality, including buildingthe IDispatch interface and table.</P><P>When you add a new method to your control through the Class Wizard, you are askedto provide the external name for the method called by the container application.Your method will get a default internal name, which you can override by enteringyour own internal name. Other aspects of your control methods that you have to specifyare the method's return type and the parameters for the method. Once you finish enteringall this information, the Class Wizard adds all the necessary code to the control.</P><P><H3><A NAME="Heading4"></A>Events</H3><P>Events are notification messages that are sent from the control to the containerapplication. They are intended to notify the application that a certain event hashappened, and the application can take action on that event if desirable. You cantrigger two types of events from your control, stock or custom events. Stock eventsare implemented by the ActiveX control development kit and are available as functioncalls within the control. These stock events enable you to trigger events in thecontainer application for mouse or keyboard events, errors, or state changes.</P><P>Along with the stock events, you can add your own custom events to be triggeredin the container application. These events should be related to the specific functionalityof your control. You can specify arguments to be passed with the event to the containerapplication so that the application can have the data it needs for reacting to theevent message.</P><P>When you need to trigger any of these events, all you do is call the internalevent function that fires the event, passing all the necessary parameters to thefunction. The Class Wizard will have added all of the necessary code to trigger theevent message from the internal function call.</P><P>Events are one of the three elements that you do not add to your controls throughthe Automation tab in the Class Wizard. Events are added through the ActiveX Eventstab in the Class Wizard.</P><P><H2><A NAME="Heading5"></A>Creating an ActiveX Control</H2><P>The ActiveX control that you will build as the example today is the squiggle drawingmodule that you packaged as a library module and then as DLLs on Day 16, "CreatingYour Own Classes and Modules," and Day 17, "Sharing Your Functionalitywith Other Applications--Creating DLLs." In converting this module into an ActiveXcontrol, you'll expose the maximum number of squiggles that the control will draw,as well as the maximum length of the squiggles, as properties that the containerapplication can set. Every time the control is clicked, you'll program it to createa new squiggle drawing. You'll also add a method to load a squiggle drawing intothe control that was created with the previous versions of the squiggle module. Finally,you'll have the control fire an event to let the container application know thatthe control has loaded the drawing.</P><P><H3><A NAME="Heading6"></A>Building the Control Shell</H3><P>You've probably noticed by now that one of the options on the new project dialogis an MFC ActiveX Control Wizard. This is another project wizard just like the AppWizardfor creating application and DLL projects. You can use it to build a shell for anyActiveX controls that you want to build. It will create all of the necessary filesand configure the project so that the compiler will build an ActiveX control whenyou compile.</P><P>When you start the Control Wizard, you are asked some simple questions about yourcontrol project, such as how many controls will be in the project and whether thecontrols will have runtime licenses.</P><BLOCKQUOTE> <P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -