重拾VB6(19):Using a Component's Visual Interface

前端之家收集整理的这篇文章主要介绍了重拾VB6(19):Using a Component's Visual Interface前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Programmer’s Guide/Part 2: What Can You Do With Visual Basic/Programming With Components/

1. Using a Component's Visual Interface

(1) If a component supports object linking and embedding (OLE),you can link or embed an object into your application without writing any code by using the component's visual interface. You can use a component's visual interface in one of two ways:

a) By adding an OLE container control to your application,then inserting an object into the control.

b) By adding the object's class to the ToolBox,then adding an object of that class to your application just as you would add a control to a form.

(2) An OLE container control can contain only one object at a time. There are several ways to create a linked or embedded object in the OLE container control

2. Contrasting Linked and Embedded Objects

(1) The primary difference between a linked and embedded object is where their data is stored. For example,data associated with a linked object is managed by the application that created it and stored outside an OLE container control. Data associated with an embedded object is contained in an OLE container control and can be saved with your Visual Basic application.

(2) When a linked or embedded object is created,it contains the name of the application that supplied the object,its data (or,in the case of a linked object,a reference to the data),and an image of the data.

(3) Linked Objects: For example,in Figure 10.8,Visual Basic contains a link to the Graph application. Microsoft Word also contains a link to the graph. If the graph's data is changed by either application,the modified graph will appear in both the Visual Basic application and the Microsoft Word document.

(4) With an embedded object,all the data associated with the object is copied to and contained in the OLE container control. Embedded objects can greatly increase file size.

3. Inserting Objects at Design Time with the OLE Container Control

(1) Each time you draw an OLE container control on a form,Visual Basic displays the Insert Object dialog Box.

(2) When you insert an object into the OLE container control at design time,the Class,SourceDoc,and SourceItem properties are automatically set for you.

(3) To insert a linked object using the Insert Object dialog Box,Select the Create from File option button.

(4) When you use a linked object,every user who runs your application must have access (a valid path) to the linked file and a copy of the application that created the file. Otherwise,when your application is run,an image of the original data is displayed,but the user will not be able to modify the data,nor will the user see changes others may have made to the linked data. This may be a concern if your application is running on a network.

(5)If your application contains a linked object,it is possible for that object's data to be changed by another application when your application is not running. The next time your application is run,changes to the source file do not automatically appear in the OLE container control. To display the current data in the OLE container control,use the control's Update method.

(6)If a user wants to save changes to a linked object,the user must save it from the ActiveX component's menu. The OLE container control's SaveToFile method applies only to embedded objects.

(7)Unlike the data in a linked object,data in an embedded object is not persistent. In other words,if you want changes entered by the user to appear the next time your application is run,you must use the SaveToFile method to save the data.

(8)Creating Objects Using the Paste Special Dialog Box:

In Visual Basic,click the OLE container control with the right mouse button,and choose the Paste Special command from the pop-up menu.

4. Creating Objects at Run Time with the OLE Container Control

(1) By using the OLE container control's Object property,you can also use the properties and methods of the linked or embedded object. The Object property is a run-time,read-only property that holds a reference to the object in an OLE container control.

(2) You can create a linked object from a file at run time with the OLE container control's CreateLink method.

(3) To create an embedded object from a file at run time,you can use the CreateEmbed method.

(4) the following code fragment creates an empty embedded object and then activates the application that created it using the default DoVerb action.

oleObj1 . CreateEmbed "" , "Excel.Sheet"
oleObj1 . DoVerb - 5 ' Activate

With the DoVerb method,the user can enter any data into the application at run time. The user can then show this newly entered data in the OLE container control by choosing the ActiveX component's Update command (this menu command should appear on the component's File menu).

(5) Binding a Database to the OLE Container Control: To bind data to one of these databases,specify the source of data (recordset name) in the DataSource property and the field name from that data source in the DataField property of the OLE container control. When displaying an object from a database,the OLE container control allows the user to activate,edit,and update the object. As with any bound control,the updated object is automatically written back to the database when the record position is changed.

5. Letting the User Specify Objects at Run Time

You use the OLE container control's InsertObjDlg method to display the Insert Object dialog Box,or you can use the PasteSpecialDlg method to display the Paste Special dialog.

Private Sub cmdInsert_Click ()
' Display Insert Object dialog Box.
oleObj1 . InsertObjDlg
' Check to make sure object was created with the
' OLEType property.
If oleObj1 . OLEType = vbOLENone Then
MsgBox "Object Not Created."
End If
End Sub

Private Sub oleObj1_Click ()
' Determine if the data contained on the Clipboard
' can be pasted into the OLE container control.
If oleObj1 . PasteOK Then
' Display Paste Special dialog Box.
oleObj1 . PasteSpecialDlg
' Check to make sure object was created.
If oleObj1 . OLEType = vbOLENone Then
MsgBox "Object Not Created."
End If
End If
End Sub

6. Determining How an Object is Displayed in the OLE Container Control

(1) You can use the OLE container control's DisplayType property to indicate if the object will appear as an icon (set DisplayType = 1),or if the object's data will be displayed in the control (set DisplayType = 0).

(2) You use the SizeMode property to determine how an object's icon or data image is displayed in the OLE container control when the control is not UI (user-interface) active.

7. Activating an Object in the OLE Container Control

(1) While the OLE container control's DoVerb method activates an object at run time,you can use the AppIsRunning property to determine whether the application supplying the object is activated and running. You can set AppIsRunning to True to start the ActiveX component,which causes objects to activate more quickly. You can also set this property to False to close the application or take another appropriate action when the object loses focus.

(2) Some embedded objects can be edited (activated) from within the OLE container control. This is called in-place activation,because users can double-click an object in your application and interact with application supplying the object,without switching to a different application or window.

(3) For objects that support in-place activation,you can set the AutoActivate property so that users can activate an object at any time.

(4) If you want to display the ActiveX component's menus at run time when the user clicks the OLE container control,you must define at least one menu item for the form and set its Visible property to False.

8. Responding to Moving or Sizing the Container

An ObjectMove event occurs when the user moves or resizes the object contained in the OLE container control.

Private Sub oleObj1_ObjectMove( Left As Single , Top As _
Single , Width As Single , Height As Single)
' This method resizes the OLE container control to
' the new object size.
oleObj1 . Move oleObj1 . Left , oleObj1 . Top , _
Width , Height
' This method moves the OLE container control
' to the new object position.
oleObj1 . Move Left , Top , _
oleObj1 . Width , oleObj1 . Height
' Repaints the form.
Me . Refresh
End Sub

9. Saving and Retrieving Embedded Data

(1) Data associated with an embedded object is not persistent; To save updated data from an object to a file,you use the OLE container control's SaveToFile method. Objects in the OLE container control can be saved only to open,binary files.

Private Sub cmdSaveObject_Click ()
Dim FileNum as Integer
' Get file number.
FileNum = FreeFile
' Open file to be saved.
Open "TEST.OLE" For Binary As # FileNum
' Save the file.
oleObj1 . SaveToFile FileNum
' Close the file.
Close # FileNum
End Sub

(2) When you use the SaveToFile or ReadFromFile methods,the file position is located immediately following the object. Therefore,if you save multiple objects to a file,you should read them in the same order you write them.

Private Sub cmdOpenObject_Click ()
Dim FileNum as Integer
' Get file number.
FileNum = FreeFile
' Open the file.
Open "TEST.OLE" For Binary As # FileNum
' Read the file.
oleObj1 . ReadFromFile FileNum
' Close the binary file.
Close # FileNum
End Sub

(3) The Updated event is invoked each time the contents of an object is changed. This event is useful for determining if an object's data has been changed because it was last saved. To do this,set a global variable in the Updated event indicating the object needs to be saved. When you save the object,reset the variable.

(4) If a user wants to save changes to a linked file,the user must choose the Save command from the ActiveX component's File menu because the SaveToFilemethod applies only to embedded objects.

原文链接:/vb/262991.html

猜你在找的VB相关文章