📄 ch15.htm
字号:
<DD><B>5. </B>Double-click the button and you will find that nothing happens. An ActiveX control doesn't have the capability to automatically create an event handler when you double-click the button like a VCL component does. Instead, switch to the Events page and double-click the Value column next to the OnClick event. An event handler is generated. Type this line of code: <P></DL><BLOCKQUOTE> <PRE>MessageDlg(`Hey, it works!', mtInformation, [mbOK], 0);</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>6. </B>Run the program and test the button to ensure that it works. When you have verified that the button works, close the program. <P> <DT></DT> <DD><B>7. </B>Bring up the form and right-click on the button. Choose About from the context menu. The control's About box is displayed. The About box is not customized in any way, but you can go back and do that later if you want (provided you saved the file earlier). <P></DL><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The idea behind one-step ActiveX is to take a working VCL component and create an ActiveX control from that component. Most of the time, you won't have to modify the ActiveX code in any way. However, you certainly can modify the ActiveX code after it has been generated by Delphi if you so desire. Be aware, though, that if you regenerate the ActiveX code from your original VCL component, all changes made to the ActiveX source will be lost.<BR> <strong>NOTE:</strong> You can create ActiveX controls only from windowed VCL controls (controls derived from TWinControl or one of its descendents). The list of VCL controls from which you can build an ActiveX control contains all installed components that specifically meet this criteria. <HR></BLOCKQUOTE><P><B>Unregister the ActiveX Control</B></P><P>After experimenting with your new ActiveX control, you should unregister it sothat it doesn't occupy space in the Registry. To unregister the ActiveX control,do this:</P><DL> <DT></DT> <DD><B>1. </B>Choose Component|Import ActiveX Control from the main menu. <P> <DT></DT> <DD><B>2. </B>Select the ActiveX in the list of installed ActiveX controls and click the Remove button. <P> <DT></DT> <DD><B>3. </B>Click Yes on the confirmation dialog box to have Delphi unregister the ActiveX. <P></DL><P>Alternatively, you can load the ActiveX project (if you previously saved it) andchoose Run|Unregister ActiveX Server from the main menu.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> If all else fails, you can always locate the ActiveX control in the Registry and delete the key for that control. Use the Registry Editor's find function to find the key (search for the control's name or its GUID). Naturally, you want to be careful when editing the Registry manually. <HR></BLOCKQUOTE><H4>Creating ActiveForms</H4><P>Creating an ActiveForm is almost as easy as creating an ActiveX from an existingVCL component. Naturally, you can create a complex ActiveX containing many componentson a single form. Contrary to what its name implies, however, an ActiveForm can beused to create a simple ActiveX control from scratch (a colored button, for example).In other words, ActiveForms are not only for creating fancy forms with dozens ofgadgets. They are for creating single-use ActiveX controls as well.</P><P>In this section, you will create an ActiveForm. The ActiveForm will have two editcontrols, a label and a button. The button will take the contents of the two editcontrols, multiply them together, and display the result in the label. Yes, I knowit doesn't require a lot of imagination to stick with the "multiply two numbers"idea, but my goal is to show you how to create an ActiveForm with the minimum amountof code. Keeping the code to a minimum allows you to focus on the ActiveForm creationprocess without getting bogged down in code.</P><P><B>Create the ActiveForm</B></P><P>Creating an ActiveForm is so easy it's amazing. Follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Close all projects and then choose File|New from the main menu. The Object Repository is displayed. <P> <DT></DT> <DD><B>2. </B>Double-click the ActiveForm icon. The ActiveForm Wizard is displayed. This dialog box is identical to the ActiveX Control Wizard except for the fact that the VCL Class Name field is grayed (it doesn't apply here). <P> <DT></DT> <DD><B>3. </B>Enter MyFormX in the New ActiveX Name field. <P> <DT></DT> <DD><B>4. </B>Change the Implementation Unit field to read MyFormImpl.pas. <P> <DT></DT> <DD><B>5. </B>Change the Project Name field to MyFormProj.dpr. <P> <DT></DT> <DD><B>6. </B>Leave the Thread Model set to Apartment. Check the Include Version Information check box. <P> <DT></DT> <DD><B>7. </B>Click the OK button to continue. <P></DL><P>Delphi creates the required units and displays a form.</P><P><B>Create the Form</B></P><P>An ActiveForm form is just a regular form at this stage. You can add controlsto the form, add code, and respond to events just like you do for a form that belongsto an application. The one difference is that the title bar on an ActiveForm doesnot appear on the control itself. It's just there at design time.</P><P>In this step, you will add components and code to make the ActiveForm functional.As you work through the steps, it might help to refer to Figure 15.8 later in thechapter, which shows the completed form. I'm going to give you the primary componentsin the following steps and let you finish the rest on your own. Perform these steps:</P><DL> <DT></DT> <DD><B>1. </B>Size the form to approximately 175 (width) by 275 (height). <P> <DT></DT> <DD><B>2. </B>Add an Edit component near the top-center of the form (see Figure 15.8). Change its Name property to Num1Edit, its Text property to 0, and its Width to 50 or so (the exact width is not important). Change the AxBorderStyle property to afbRaised. <P> <DT></DT> <DD><B>3. </B>Click on the Edit component and copy it to the Clipboard; paste a new component from the Clipboard. Place the new component below the first. Change its Name property to Num2Edit. <P> <DT></DT> <DD><B>4. </B>Place a Label component below the two edit controls. This label will display the results. Change the label's Name property to ResultLbl and its Caption property to 0. <P> <DT></DT> <DD><B>5. </B>Place a Button component on the form to the right of the Edit components. Change its Name to GoButton and its Caption to Go!. <P> <DT></DT> <DD><B>6. </B>Double-click the button and make the OnClick event handler look like this: <P></DL><BLOCKQUOTE> <PRE>procedure TMyFormX.GoButtonClick(Sender: TObject);begin try ResultLbl.Caption := IntToStr( StrToInt(Num1Edit.Text) * StrToInt(Num2Edit.Text)); except on EConvertError do MessageDlg(`Oops! You entered an invalid value.', mtError, [mbOK], 0); end;end;</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD>This code simply extracts the values of the two edit controls, multiplies them together, and displays the result in the ResultLbl label. The exception handling code displays a message box if the user enters invalid values. An EConverError exception will be raised if the conversion from text to integer fails (if one of the edit controls contains text, for example). <P> <DT></DT> <DD><B>7. </B>Add additional labels to match Figure 15.8. <P> <DT></DT> <DD><B>8. </B>Choose View|Type Library from the main menu. In the Information page, change the Help String field to My Test ActiveForm Library. This is the text that will be displayed in the Import ActiveX dialog box when you install the ActiveForm. <P> <DT></DT> <DD><B>9. </B>Save the project. Accept the default filenames. (You specified them in the ActiveForm Wizard.) Figure 15.8 shows the completed form. <P></DL><P><A HREF="javascript:popUp('28671508.gif')"><B>FIGURE 15.8.</B></A><B> </B><I>Thefinished ActiveForm.</I></P><P><B>Build, Register, and Import the ActiveForm</B></P><P>Now you can build, register, and import the ActiveForm. When built, the ActiveFormis like any other ActiveX control. Because you've done this several times now, I'mnot going to go over every step. Follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Choose Project|Build MyFormProj from the main menu. <P> <DT></DT> <DD><B>2. </B>When the project is built, choose Run|Register ActiveX Server from the main menu. <P> <DT></DT> <DD><B>3. </B>Choose Component|Import ActiveX Control from the main menu. Install My Test ActiveForm Library (Version 1) into the DCLUSR40 package. Install to the ActiveX page or any other page you choose. <P></DL><P>The ActiveForm is now installed as an ActiveX control.</P><P><B>Try the ActiveForm</B></P><P>Now it's time to take the ActiveForm for a test drive. This will be fairly simple:</P><DL> <DT></DT> <DD><B>1. </B>Create a new application. <P> <DT></DT> <DD><B>2. </B>Click the ActiveX tab on the Component palette and choose MyFormX button (the one with the default Delphi icon). <P> <DT></DT> <DD><B>3. </B>Place a MyFormX control on your form. <P> <DT></DT> <DD><B>4. </B>Run the program and test out the ActiveX. <P></DL><P>That's all there is to it. With Delphi, great-looking ActiveX controls are a breezeto create! There simply is no better development environment for creating ActiveXcontrols than Delphi, bar none.</P><P><H3><A NAME="Heading10"></A>Changing the ActiveX Palette Bitmap</H3><P>Ultimately you will want to change the bitmap of the ActiveX from the defaultthat Delphi provides to one of your own design. Changing the bitmap requires followingthese steps:</P><DL> <DT></DT> <DD><B>1. </B>Create a binary resource file (.RES) with Image Editor. <P> <DT></DT> <DD><B>2. </B>Create a 24¥24 bitmap. Give the bitmap a numeric name (2 for example). <P> <DT></DT> <DD><B>3. </B>Link the resource file to the ActiveX project with the $R compiler directive. (Linking resources was discussed on Day 8, "Creating Applications in Delphi" and is discussed further on Day 20, "Creating Components.") <P> <DT></DT> <DD><B>4. </B>Modify the ActiveX class factory creation routine in the implementation unit (the ActiveForm's .PAS file). A typical class factory creation routine looks like this (it's in the initialization section at the bottom of the unit): <P></DL><BLOCKQUOTE> <PRE>TActiveFormFactory.Create( ComServer, TActiveFormControl, TMyFormX, Class_MyFormX, 1, { Change this number. } `', OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL, tmApartment);</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD>Notice the line I have marked with a comment. This parameter of TActiveFormFactory.Create is the resource number of the bitmap you want displayed on the Component palette. If you saved the new bitmap with a name of 2, you would replace the 1 in this code snippet with a 2. <P> <DT></DT> <DD><B>5. </B>Rebuild, reregister, import, and install the ActiveForm again. The new bitmap should now show up in the Component palette. <P></DL><P>Alternatively, you can modify the ActiveForm project's .RES file and change thebitmap named 1 to look like you want.</P><P><H2><A NAME="Heading11"></A>Web Deploying ActiveX Controls and ActiveForms</H2><P>One of the great features of ActiveForms is that you can use them on a Web page.In order to use an ActiveForm on a Web page, you must use the Web Deploy option.Using Web Deploy requires a Web server, so I can't effectively walk you through theWeb deployment process. I can, however, give you a little insight into the process.When you choose Web Deploy, Delphi performs two tasks:</P><UL> <LI>Builds the ActiveX control and copies the file to the Web Deploy target directory <P> <LI>Creates an HTML file that contains the code needed to load the ActiveX control</UL><P>The locations of these files is determined by the Web deployment options. Let'slook at that next.</P><P><H3><A NAME="Heading12"></A>Web Deployment Options</H3><P>Before you can use Web Deploy, you must set the Web deployment options. To setthe Web deployment options, choose Project|Web Deployment Options from the main menu.The Web Deployment Options dialog box is displayed, as shown in Figure 15.9.</P><P><A HREF="javascript:popUp('28671509.gif')"><B>Figure 15.9.</B></A><B> </B><I>TheWeb Deployment Options dialog box.</I></P><P>At the bottom of the Web Deployment Options dialog box is a check box labeledDefault. Check this box if you want the settings you have specified to be the newdefaults fo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -