📄 ch15.htm
字号:
Modify the OnClick handler so that it looks like this: <P></DL><BLOCKQUOTE> <PRE>procedure TForm1.Button1Click(Sender: TObject);var Mult : IMultiply; Res : Integer;begin Mult := CreateComObject(CLASS_Multiply) as IMultiply; if Assigned(Mult) then begin Mult.Set_X (20); Mult.Set_Y (60); Res := Mult.DoIt; Label1.Caption := IntToStr(Res); end;end;</PRE></BLOCKQUOTE><PRE></PRE><P>This code first declares a pointer to the IMultiply interface called Mult andan Integer variable to hold the result. Next, the CreateComObject function is calledwith a parameter of CLASS_Multiply. CLASS_Multiply is a constant that contains theGUID for the COM object class (refer to Listing 15.3).</P><P>The return value from CreateComObject is assigned to the Mult pointer. Noticethat I use the as operator to cast the return value to an IMultiply pointer. CreateComObjectreturns an IUnknown pointer, so the as operator is used to cast the IUnknown pointerto an IMultiply pointer.</P><P>After the COM object is created, I assign values to the X and Y properties. Afterthat I call the DoIt method of the COM object and display the result in the Labelcomponent.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> In the real world, I would have written the preceding procedure differently. For example: <PRE>procedure TForm1.Button1Click(Sender: TObject);begin with CreateComObject(CLASS_Multiply) as IMultiply do begin Set_X(20); Set_Y(60); Label1.Caption := IntToStr(DoIt); end;end;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>I wrote the procedure the way I did to illustrate each step. <HR></BLOCKQUOTE><P>Run the program. When you click the form's button, the label should change tosay "1200" (the product of 20 * 60). That's it! Your COM object works.This COM object can be used from Visual Basic, Visual C++, C++Builder, or any otherdevelopment environment that supports COM.</P><P><H2><A NAME="Heading7"></A>Understanding ActiveX</H2><P><I>ActiveX</I> is a relatively new term for a technology that has been aroundfor awhile. Originally ActiveX controls were called <I>OCX controls</I>. The term<I>OCX</I> is still widely used in some circles. An ActiveX control typically hasa filename extension of either DLL or OCX.</P><P>An ActiveX control is essentially a COM object in disguise. The primary differencebetween an ActiveX control and a COM object is that an ActiveX control has a design-timeinterface. An ActiveX control also has code that enables it to be deployed on a Webpage or over a network. ActiveX is a subset of COM, so everything you learned aboutCOM objects in the first part of the chapter applies to ActiveX controls as well.</P><P><H3><A NAME="Heading8"></A>Using Third-Party ActiveX Controls</H3><P>There isn't a lot to know about installing and using third-party ActiveX controls.All you have to do is import the ActiveX into the IDE and begin using the control.To see how this works, let's do a quick exercise. This exercise requires that youhave Microsoft Internet Explorer installed on your system. If you don't have InternetExplorer installed, skip this exercise. (You won't be missing anything because Ishow you how to install an ActiveX control you have built in the section "Build,Register, and Install the Control.") Perform these steps:</P><DL> <DT></DT> <DD><B>1. </B>Choose Component|Import ActiveX Control from the main menu. The Import ActiveX dialog box is displayed. <P> <DT></DT> <DD><B>2. </B>Scroll down through the list of installed components until you find Microsoft Internet Controls (the exact text of the item will depend on the version of Internet Explorer you have installed on your system). Select the item. Figure 15.6 shows the Import ActiveX dialog box after this step. <P></DL><P><A HREF="javascript:popUp('28671506.gif')"><B>FIGURE 15.6.</B></A><B> </B><I>TheImport ActiveX dialog box.</I></P><DL> <DT><I></I></DT> <DD>Notice the Class names list box in the middle of the page. This list box contains a list of ActiveX controls in the selected file (SHDOCVW.DLL in this case). <P> <DT></DT> <DD><B>3. </B>The Palette page field shows ActiveX. This is a the palette page where the new controls will be installed. Click on this field and type ActiveXTest. <P> <DT></DT> <DD><B>4. </B>Leave the Unit dir name and Search path fields on their default settings and click the Install button. The Install dialog box comes up and asks what package you want the controls installed into. (All controls, whether they are VCL or ActiveX, must be in a package.) <P> <DT></DT> <DD><B>5. </B>Click on the Into new package tab. Enter MSIE in the File name field and Internet Explorer Package in the Description field. <P> <DT></DT> <DD><B>6. </B>Click the OK button. Delphi creates a new package called MSIE.dpk and prompts you to build and install the package. Click Yes to install the package. After the package is built, Delphi displays a message box telling you which controls were added to the Component palette. Click Yes to dismiss the message box. <P> <DT></DT> <DD><B>7. </B>Scroll the Component palette to find the ActiveXText tab. You should see two or three controls on that page of the Component palette (again, depending on the version of Internet Explorer you have installed). The components are ready for use. <P></DL><P>Experiment with the new controls and see how they work. You probably won't getvery far without documentation, but at least you get a sense for how installing anActiveX works. (For a more complete explanation of using Internet Explorer as anActiveX, see the section, "Using Internet Explorer as an ActiveX Control"on the Bonus Day, "Building Internet Applications.")</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> You must have a design-time license in order to use an installed ActiveX control. A design-time license is a file with an .LIC extension. In some cases you can import ActiveX controls to the Delphi Component palette without a design-time license, but you will get an error message when you attempt to place the control on your form. <HR></BLOCKQUOTE><P>To remove the Internet Explorer controls from the Component palette, choose Component|InstallPackages from the main menu. Locate the Internet Explorer Package in the Design packageslist box and click the Remove button. The ActiveXTest tab is removed from the Componentpalette.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Deploying an application that uses ActiveX controls requires special attention. Deploying applications using ActiveX controls is covered in detail on the Bonus Day in the section "Deploying Internet Applications." <HR></BLOCKQUOTE><H3><A NAME="Heading9"></A>Creating New ActiveX Controls</H3><P>There are two ways to create an ActiveX control in Delphi:</P><UL> <LI>From an existing VCL component <P> <LI>From scratch using an ActiveForm</UL><P>In this section, you create an ActiveX control using both of these methods.</P><P><H4>Creating an ActiveX Control from an Existing VCL Component</H4><P>Creating an ActiveX control from an existing VCL component is downright simple.After you create a component, you can turn it into an ActiveX control in no timeat all. I haven't talked about creating components yet, so I don't want to go intoa lot of detail on creating components now (covered on Day 20, "Creating Components").What you will do, then, is create an ActiveX control from one of the VCL componentsprovided by Borland.</P><P><B>Generate the ActiveX Project with the ActiveX Control Wizard</B></P><P>The first step is to generate the ActiveX project. As always, Delphi does mostof the work for you. All you have to do is supply a few fields in the ActiveX ControlWizard. Here are the steps:</P><DL> <DT></DT> <DD><B>1. </B>Choose File|Close All to close all projects and then choose File|New from the main menu. The Object Repository is displayed. <P> <DT></DT> <DD><B>2. </B>Click the ActiveX page and then double-click the ActiveX Control icon. The ActiveX Control Wizard is displayed (see Figure 15.7). <P></DL><P><A HREF="javascript:popUp('28671507.gif')"><B>FIGURE 15.7.</B></A><B> </B><I>TheActiveX Control Wizard.</I></P><DL> <DT><I></I></DT> <DD><B>3. </B>Select TButton from the list of classes in the VCL Class Name combo box. The next four fields are automatically filled in with default values. Because this is just a test, you can leave those fields on their default values. The fields are self-explanatory, so I don't need to go over each one. <P> <DT></DT> <DD><B>4. </B>The Threading Model is set to Apartment. Leave this setting as it is. Other threading models include Single, Free, and Both. See the Delphi help for more information on threading models. <P> <DT></DT> <DD><B>5. </B>Check the Include Design-Time License check box. When this option is checked, Delphi will create a design-time license for the control. The design-time license will prevent other programmers from using the control unless they have the license. <P> <DT></DT> <DD><B>6. </B>Check the Include Version Information check box. This will enable you to add version info to the control via the Project Options dialog box. <P> <DT></DT> <DD><B>7. </B>Check the Include About Box check box as well. When this box is checked, Delphi will automatically create an About dialog box for the control. Click OK to close the ActiveX Control Wizard. <P></DL><P>Delphi will create a project file (ButtonXControl1.bpr) and three units for theproject. The first unit is the TButtonX class unit (ButtonXImp1.pas). The secondunit is the type library file for the control, named ButtonXControl1_TLB.pas. Thisfile contains the information Delphi needs to create the type library for the control.The third file, About1.pas, is the unit for the About box. If you want to customizethe About box, you can do that at this time. The About box is just another Delphiform, so feel free to customize it in any way you like.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Version info is required in order for your ActiveX controls to work in Visual Basic. <HR></BLOCKQUOTE><P><B>Build, Register, and Install the Control</B></P><P>Because you aren't making any modifications to the control itself, you can jumpright to building the control and registering it. This is the same process you wentthrough when you registered the COM object you created earlier. An extra step isrequired when implementing ActiveX controls, though, because ActiveX controls havea design-time interface. Try this:</P><DL> <DT></DT> <DD><B>1. </B>Choose Project|Build ButtonXControl1 from the main menu. Delphi builds the ActiveX project. <P> <DT></DT> <DD><B>2. </B>Choose Run|Register ActiveX Server from the main menu. The ActiveX control is registered and Delphi displays a message box telling you that the OCX is registered (ActiveX projects have a default extension of .OCX). Click OK to dismiss the message box. <P> <DT></DT> <DD><B>3. </B>Choose Component|Import ActiveX Control from the main menu. Choose ButtonXControl1 Library (Version 1.0) from the list of installed controls (had you not performed step 2, this entry would not have been present in the list of installed controls). The class name of the button, TButtonX, shows in the Class names list box. <P> <DT></DT> <DD><B>4. </B>Set the Palette page field to ActiveX. Click Install to continue. <P> <DT></DT> <DD><B>5. </B>The Install dialog box is displayed. You are going to install the control into the default Delphi user package DCLUSR40.BPL. The File name field should already contain this package. If it doesn't, choose it from the combo box. The Description field now says Delphi User's Components. Click the OK button to install the control. <P> <DT></DT> <DD><B>6. </B>Click Yes to the message box regarding building and installing DCLUSR40.BPL. Click OK when the message box confirming the installation is displayed. The control is now installed. <P></DL><P><B>Test the ActiveX Control</B></P><P>Now you can test your new ActiveX control. First, create a new project.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> When you create a new project, Delphi will prompt you to save the package file (DCLUSR40.DPK) and the ActiveX control project. Whether you save these files is up to you. My intention was to have you create a quick ActiveX. There's really no need to save the files. If, however, you think you might want to save the files to examine them later, save the files. <HR></BLOCKQUOTE><P>Now follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Locate the ActiveX tab on the Component palette. <P> <DT></DT> <DD><B>2. </B>The last control in the list is the ButtonX control. Select it. <P> <DT></DT> <DD><B>3. </B>Place a ButtonX control on your form. Notice that the button doesn't have a default caption as a regular VCL button does. <P> <DT></DT> <DD><B>4. </B>Change the Caption property to Test. The button's caption changes just as a VCL button's caption would change. <P> <DT></DT> <DD>Notice the list of properties in the Object Inspector. They are mostly the same properties you would see on a VCL button (the ActiveX was created from a VCL TButton after all), but you might have noticed that the Value column looks slightly different. Remember, this is an ActiveX control and is intended to be used in any environment that hosts ActiveX controls. For that reason, some of the property values are expressed in a more generic way. <P> <DT></DT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -