⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Type</I></TD>		<TD ALIGN="LEFT"><I>Name</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">property</TD>		<TD ALIGN="LEFT">X</TD>		<TD ALIGN="LEFT">The first number to multiply.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">property</TD>		<TD ALIGN="LEFT">Y</TD>		<TD ALIGN="LEFT">The second number to multiply.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">method</TD>		<TD ALIGN="LEFT">DoIt</TD>		<TD ALIGN="LEFT">A method that multiplies the two numbers and returns the result.</TD>	</TR></TABLE></P><P>The following sections explain the process of creating the COM object.</P><P><H4>Creating an ActiveX Library</H4><P>The first step in creating a COM object is to create a DLL project that will containthe COM object's code. Delphi uses the term &quot;ActiveX Library&quot; to referto all COM library projects. This description isn't entirely accurate, but it's closeenough. Perform these steps to create an ActiveX Library:</P><DL>	<DT></DT>	<DD><B>1. </B>Close all projects. Choose File|New from the main menu to display the	Object Repository.	<P>	<DT></DT>	<DD><B>2. </B>Click on the ActiveX tab to show the ActiveX page (see Figure 15.1).	Double-click the ActiveX Library icon.	<P></DL><P><A HREF="javascript:popUp('tyd1501.gif')"><B>Figure 15.1.</B></A><B> </B><I>TheObject Repository's ActiveX page.</I></P><DL>	<DT><I></I></DT>	<DD><B>3. </B>Choose File|Save and save the project as ComTest.	<P></DL><P>That's all there is to this particular step. Delphi creates a DLL project foryou and is waiting for your next move.</P><P><H4>Creating the Actual Object</H4><P>The next step is to create the COM object itself. This step is also relativelysimple. Perform these steps to do so:</P><DL>	<DT></DT>	<DD><B>1. </B>Choose File|New from the Delphi main menu. The Object Repository is	displayed. Click on the ActiveX page.	<P>	<DT></DT>	<DD><B>2. </B>Double-click on the COM Object icon. Delphi displays the COM Object	Wizard, as shown in Figure 15.2.	<P></DL><P><A HREF="javascript:popUp('28671502.gif')"><B>FIGURE 15.2.</B></A><B> </B><I>TheCOM Object Wizard.</I></P><BLOCKQUOTE>	<P><HR><BR>	<B>THE COM OBJECT WIZARD</B> </P>	<P>	<BR>Let me take a moment to talk about the COM Object Wizard. The Class Name field is	used to specify the class name for your COM object. Type the class name here, but	don't prepend the class name with either a T as you would for a Delphi class, nor	an I as is customary for interfaces. Delphi will take care of creating the class	and interface names automatically. <BR>	</P>	<P>The Instancing field is used to control how multiple instances of the COM object	are handled. Choices include Internal, Single Instance, or Multiple Instance. See	the &quot;COM object wizard&quot; topic in the Delphi help for descriptions of these	instancing options (you can click the Help button on the COM Object Wizard to display	the correct help topic). <BR>	</P>	<P>The Threading Model field is used to specify how client applications can call	your COM object. Choices include Single, Apartment, Free, or Both. Again, see the	Delphi help for descriptions of the threading models. <BR>	</P>	<P>The Implemented Interfaces field is where you add the names of any interfaces	that your COM object will implement. If you have an interface called IMyFileIO and	you want to use that interface in your new COM object, you would type IMyFileIO in	this field.<BR>	</P>	<P>The Description field is used to supply a description for the COM object. The	description is optional, but it's a good idea to provide one.<BR>	When the Include Type Library check box is checked, Delphi will create a type library	for the COM object. Creating a type library enables your COM object to be used by	client applications. <HR></BLOCKQUOTE><P>Okay, let's get back to work:</P><DL>	<DT></DT>	<DD><B>3. </B>Enter Multiply in the Class Name field.	<P>	<DT></DT>	<DD><B>4. </B>Enter Test COM Object in the Description field.	<P>	<DT></DT>	<DD><B>5. </B>Check the Include Type Library check box. The other fields on the dialog	box can be left at their default values.	<P>	<DT></DT>	<DD><B>6. </B>Click OK to close the dialog box.	<P></DL><P>When you click the OK button, Delphi creates a unit for the COM object's classand displays the Type Library Editor, as shown in Figure 15.3. Before continuing,I need to take a moment to talk about the Type Library Editor.</P><P><H4>Using the Type Library Editor</H4><P>The Type Library Editor is used to manipulate a type library. The Type LibraryEditor enables you to add or remove interfaces, add properties and methods to interfaces,remove elements from interfaces, and create host of other COM elements such as enumerations,records, or coclasses. The Type Library Editor makes it easy to add elements to atype library. You'll learn about adding elements in the next section when you addproperties and a method to the COM object.</P><P>On the left side of the Type Library Editor is the Object pane. The Object panecontains a tree view control. At the top of the tree view hierarchy is the type libraryitself. Below the type library are elements contained in the type library. In Figure15.3, you see two elements: the IMultiply interface and the Multiply coclass.</P><P>On the right side of the Type Library Editor is the Information pane. This paneprovides information about the object currently selected in the Object pane. Theinformation presented in the information pane varies with the type of object selected.The Attributes page shows the type library name, its GUID, version, help string,help file, and so on.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Remember earlier when I said that Delphi programmers don't need to	worry much about GUIDs? The COM object you just created already has a GUID, as does	the type library itself. Delphi creates these GUIDs for you automatically. As I said	before, you'll see GUIDs a lot as you work with COM objects, but you don't have to	worry about creating them. <HR></BLOCKQUOTE><P>When the type library node is selected, the Information pane shows a tab labeledUses. When you click on this tab you will see a list of type libraries that thistype library relies on. In almost all cases, this list will include the OLE Automationlibrary, but it can contain others as well. The exact libraries a particular typelibrary relies on depends on the type and complexity of the COM object the type librarydescribes.</P><P>The Text page shows the type library definitions in IDL syntax. IDL is sort ofa scripting language used to create binary type library files. You shouldn't changeany of the text on this tab unless you know exactly what you are doing. You might,however, refer to the Text page for reference. This is probably of more value toexperienced programmers than to beginners.</P><P>Other pages might be displayed in the Information pane depending on the type ofobject selected. For complete details, be sure to read the &quot;Type Library Editor&quot;help topic in the Delphi help.</P><P>You will learn more about the Type Library Editor as you work through the restof the chapter. Now let's get back to creating the COM object.</P><P><H4>Adding Properties and Methods to the COM Object</H4><P>Before going further, you should save the project again. You didn't realize it,but Delphi created a new unit when you created the COM object in the previous step.Choose File|Save All from the main menu and save the unit as MultiplyU.</P><P>Now you are ready to make the COM object do something. Remember, this COM objectis incredibly simplistic, so it won't do much, but it will at least do <I>something</I>.</P><BLOCKQUOTE>	<P><HR><B>ADDING PROPERTIES</B> <BR>	</P>	<P>First you will add properties to the COM object. Here are the steps:</P></BLOCKQUOTE><DL>	<DT></DT>	<DD><B>1. </B>Click on the IMultiply node in the Type Library Editor's Object pane.	Notice that the Information pane shows the interface's name, GUID, and version. Notice	also that the Parent Interface field shows the ancestor of IMultiply as IUnknown.	If you recall, I said earlier that IUnknown is the base (or parent) interface from	which all other interfaces are derived. Delphi automatically assumes a base interface	of IUnknown. You can change the base interface to some other interface if you want	by choosing an interface from the list of available interfaces. Other interfaces	in the list are themselves derived from IUnknown or one of its descendants.	<P>	<DT></DT>	<DD><B>2. </B>Right-click and choose New|Property from the context menu. The Type	Library Editor adds two new nodes to the Objects pane under the IMulitply interface.	The cursor is in editing mode so that you can type the name of the new property.	<P>	<DT></DT>	<DD><B>3. </B>Type X for the property name and then press the Enter key. Both of	the new nodes change to X. There are two nodes to each property because, by default,	a property is assumed to be a read/write property. COM requires a Get method to read	a property and a Put method to write to a property, hence the two entries. Click	on either of the two nodes labeled X. Notice the Invoke Kind field in the Information	pane as you select first one X node and then the other. Notice that the field changes	from Property Set to Property Get.	<P>	<DT></DT>	<DD><B>4. </B>Notice in the Information pane that the Type field says Integer. That's	the data type you want for this property, so you don't need to change the type.	<P>	<DT></DT>	<DD><B>5. </B>Create another new property but this time use a different approach.	Locate the New Property button on the Type Library Editor toolbar. Click the drop-down	arrow next to the New Property button. Choose Read|Write from the list of property	types. The Type Library Editor creates the new property. Name this property Y. You	can accept the default data type of Integer for this property as well. Behind the	scenes Delphi is adding code to the project's units as you add elements.<HR>	<P></DL><P><B>Adding a Method</B></P><P>Next, you add a method. Perform these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Select the IMultiply object in the Object pane and click the New Method	button on the Type Library Editor toolbar.	<P>	<DT></DT>	<DD><B>2. </B>Name the method DoIt. Notice that the Invoke Kind field says Function	(as opposed to Property Get and Property Set).	<P>	<DT></DT>	<DD>Next you must set the method's parameters. The method will have this syntax:	<P></DL><BLOCKQUOTE>	<PRE>function DoIt : Integer;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>3. </B>Click on the Parameters tab in the Information pane. Change the Return	Type field to Integer (choose Integer from the combo box). This method doesn't have	any parameters, so you can leave the Parameters list empty. After you have set the	return type, click the Attributes tab to display the Attributes page. This step isn't	strictly necessary, but does serve the purpose of taking you back to where you started.	<P>	<DT></DT>	<DD><B>4. </B>Click the Refresh Implementation button on the Type Library Editor	toolbar.	<P></DL><P>Now that you have added the two properties and methods, it's time to see whatDelphi has been doing behind the scenes. Listing 15.1 shows the class's unit as itappears after performing the steps up to this point. (Don't worry if your unit doesn'tlook exactly like Listing 15.1. My version of Delphi might have added code in a slightlydifferent order than yours.)</P><P><H4>LISTING 15.1. MultiplyU AFTER ADDING PROPERTIES AND A METHOD.</H4><PRE>unit MultiplyU;interfaceuses  Windows, ActiveX, ComObj, ComTest_TLB;type  TMultiply = class(TTypedComObject, IMultiply)  protected    function DoIt: Integer; stdcall;    function Get_X: Integer; stdcall;    function Get_Y: Integer; stdcall;    procedure Set_X(Value: Integer); stdcall;    procedure Set_Y(Value: Integer); stdcall;    {Declare IMultiply methods here}  end;implementationuses ComServ;function TMultiply.DoIt: Integer;beginend;function TMultiply.Get_X: Integer;beginend;function TMultiply.Get_Y: Integer;beginend;procedure TMultiply.Set_X(Value: Integer);beginend;procedure TMultiply.Set_Y(Value: Integer);beginend;initialization  TTypedComObjectFactory.Create(ComServer, TMultiply, Class_Multiply,    ciMultiInstance, tmSingle);end. </PRE><P>This is the shell of the COM object. Notice that the TMultiply class is derivedfrom both TTypedComObject and IMultiply. (To C++ programmers, this might look likemultiple inheritance. It's not exactly multiple inheritance, but it is similar insome ways.) You haven't seen the IMultiply class yet, but you will a bit later. Youmust fill out this shell in order to make the COM object do something. You will dothat next.</P><P><H3><A NAME="Heading6"></A>Adding Code</H3><P>You will now add code to the TMultiply class to make the COM object functional.Perform these steps (refer to Listing 15.2 if necessary):</P><DL>	<DT></DT>	<DD><B>1. </B>Display the MuliplyU.pas unit in the Code Editor. Add these lines to

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -