[ Pobierz całość w formacie PDF ] .See Using data-aware ActiveX controls onpage 35-8 for information on how to host such ActiveX controls.35-6 Devel oper s Gui deCo n t r o l l i n g a n i mp o r t e d o b j e c tAutomation object wrappersThe wrappers for Automation objects let you control how you want to form theconnection to your server object:" The ConnectKind property indicates whether the server is local or remote andwhether you want to connect to a server that is already running or if a newinstance should be launched.When connecting to a remote server, you mustspecify the machine name using the RemoteMachineName property." Once you have specified the ConnectKind, there are three ways you can connectyour component to the server:" you can explicitly connect to the server by calling the component s Connectmethod." You can tell the component to connect automatically when your applicationstarts up by setting the AutoConnect property to True." You do not need to explicitly connect to the server.The componentautomatically forms a connection when you use one of the server s propertiesor methods using the component.Calling methods or accessing properties is the same as using any other component:TServerComponent1.DoSomething;Handling events is easy, because you can use the Object Inspector to write eventhandlers.Note, however, that the event handler on your component may haveslightly different parameters than those defined for the event in the type library.Specifically, pointer types (var parameters and interface pointers) are changed toVariants.You must explicitly cast var parameters to the underlying type beforeassigning a value.Interface pointers can be cast to the appropriate interface typeusing the as operator.For example, the following code shows an event handler for theExcelApplication event, OnNewWorkBook.The event handler has a parameter thatprovides the interface of another CoClass (ExcelWorkbook).However, the interfaceis not passed as an ExcelWorkbook interface pointer, but rather as an OleVariant.procedure TForm1.XLappNewWorkbook(Sender: TObject; var Wb:OleVariant);begin{ Note how the OleVariant for the interface must be cast to the correct type }ExcelWorkbook1.ConnectTo((iUnknown(wb) as ExcelWorkbook));end;In this example, the event handler assigns the workbook to an ExcelWorkbookcomponent (ExcelWorkbook1).This demonstrates how to connect a componentwrapper to an existing interface by using the ConnectTo method.The ConnectTomethod is added to the generated code for the component wrapper.Servers that have an application object expose a Quit method on that object to letclients terminate the connection.Quit typically exposes functionality that isequivalent to using the File menu to quit the application.Code to call the Quitmethod is generated in your component s Disconnect method.If it is possible to callthe Quit method with no parameters, the component wrapper also has an AutoQuitproperty.AutoQuit causes your controller to call Quit when the component is freed.If you want to disconnect at some other time, or if the Quit method requiresparameters, you must call it explicitly.Quit appears as a public method on thegenerated component.Cr eat i ng COM cl i ent s 35-7Co n t r o l l i n g a n i mp o r t e d o b j e c tUsing data-aware ActiveX controlsWhen you use a data-aware ActiveX control in a Delphi application, you mustassociate it with the database whose data it represents.To do this, you need a datasource component, just as you need a data source for any data-aware VCL control.After you place the data-aware ActiveX control in the form designer, assign itsDataSource property to the data source that represents the desired dataset.Once youhave specified a data source, you can use the Data Bindings editor to link thecontrol s data-bound property to a field in the dataset.To display the Data Bindings editor, right-click the data-aware ActiveX control todisplay a list of options.In addition to the basic options, the additional DataBindings item appears.Select this item to see the Data Bindings editor, which lists thenames of fields in the dataset and the bindable properties of the ActiveX control.To bind a field to a property,1 In the ActiveX Data Bindings Editor dialog, select a field and a property name.Field Name lists the fields of the database and Property Name lists the ActiveXcontrol properties that can be bound to a database field.The dispID of theproperty is in parentheses, for example, Value(12).2 Click Bind and OK.Note If no properties appear in the dialog, the ActiveX control contains no data-awareproperties.To enable simple data binding for a property of an ActiveX control, usethe type library as described in Enabling simple data binding with the type libraryon page 38-10.The following example walks you through the steps of using a data-aware ActiveXcontrol in the Delphi container.This example uses the Microsoft Calendar Control,which is available if you have Microsoft Office 97 installed on your system.1 From the Delphi main menu, choose Component|Import ActiveX Control.2 Select a data-aware ActiveX control, such as the Microsoft Calendar control 8.0,change its class name to TCalendarAXControl, and click Install.3 In the Install dialog, click OK to add the control to the default user package, whichmakes the control available on the Palette.4 Choose Close All and File|New|Application to begin a new application.5 From the ActiveX tab, drop a TCalendarAXControl object, which you just added tothe Palette, onto the form.6 From the Data Access tab, drop a DataSource and Table object onto the form.7 Select the DataSource object and set its DataSet property to Table1
[ Pobierz całość w formacie PDF ]
zanotowane.pldoc.pisz.plpdf.pisz.plhanula1950.keep.pl
|