重拾VB6(17):Using ActiveX Components

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

来自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. Types of ActiveX Components

An ActiveX component is a reusable piece of programming code and data made up of one or more objects created using ActiveX technology.

(1)ActiveX controls

(2) Objects in an ActiveX-enabled application: Applications that support ActiveX technology,such as Microsoft Excel,Microsoft Word,and Microsoft Access,provide objects that you can manipulate programmatically from within your Visual Basic application.

(3) Code components provide libraries of programmable objects,which can run in the same process as your application,allowing faster access to the object.

(4) ActiveX documents: let you create interactive Internet applications. You can create forms that can be contained within Internet Explorer. ActiveX documents can show message Boxes and secondary forms and contain ActiveX controls. ActiveX documents can also function as code components.

In addition to Internet applications using ActiveX documents,you can create both client-based and server-based Internet applications using a combination of Visual Basic code and HTML pages.

(5) In addition to components in existing ActiveX-enabled applications,code component libraries,ActiveX controls,and ActiveX documents,you can create your own components.

2. In-Process and Out-of-Process Servers

(1) ActiveX components interact with your application — and with each other — through a client/server relationship. The client is the application code or component that uses the features of a component. The server is the component and its associated objects.

(2) Depending on how an ActiveX component has been implemented,it may run in the same process as its client applications,or in a different process.

Applications that use in-process servers usually run faster than those that use out-of-process servers because the application doesn't have to cross process boundaries to use an object's properties,methods,and events.

Using in-process components is one way to optimize the performance of your application. Another way to optimize performance is to use early binding.

(3) In general,if an ActiveX component has been implemented as part of an executable file (.exe file),it is an out-of-process server and runs in its own process. If it has been implemented as a dynamic-link library,it is an in-process server and runs in the same process as the client application.

(4)

Component Server Type
ActiveX-enabled application Out-of-process
Code component Either in-process or out-of-process
ActiveX control In-process
ActiveX document Either in-process or out-of-process

3. Creating a Reference to an Object

(1) If an ActiveX component provides a type library,you need to add a reference to the type library in your Visual Basic project before you can use the library's objects.

If the object's class is included in a type library,you can make your application run faster by creating an object reference using a variable of that specific class. Otherwise,you must use a variable of the generic Object class,which results in late binding.

(2) If an object is externally creatable,you can assign an object reference to a variable by using the New keyword,CreateObject,or GetObject in a Set statement from outside the component. If the object is a dependent object,you assign an object reference by using a method of a higher-level object in a Set statement.

(3) To create a reference to an object defined in a type library:Project->References->

Type libraries can have a .tlb or .olb file-name extension. Executable (.exe) files and dynamic link libraries (dlls) can also supply type libraries.

(4) If a object isn't associated with a type library,you won't be able to use the Object Browser to view the properties,and events of the object. You need to know what properties,and events the object provides,including any methods for creating a reference to a dependent object.

(5) Ambiguous References and Reference Priority: If two type libraries contain constants or classes with identical names,Visual Basic uses the definition provided by the type library listed higher in the Available References Box.

The best way to handle potentially ambiguous references is to explicitly specify the type library that supplies the constant or class when you use it.

You may be tempted to handle potentially ambiguous references by changing the order in which Visual Basic searches for references. However,changing the priority order can cause unexpected problems in your applications if there are other ambiguous references. In general,it's better to explicitly specify the type library in any references.

4. Declaring an Object Variable

(1) If you declare an object variable with the New keyword,Visual Basic will automatically create a new object the first time you use the variable.

(2) Note Using variables declared using the New keyword can slow your application. Every time Visual Basic encounters a variable declared using New,it must test whether or not an object reference has already been assigned to the variable.

(3) The main difference between declaring a variable of a specific class and declaring a variable of the generic Object class is in how ActiveX binds the variable to the object. When you declare a variable of the generic Object class,ActiveX must use late binding. When you declare an object variable of a specific class,ActiveX uses early binding, which can speed object references.

5. Assigning an Object Reference to a Variable

(1) After you declare an object variable,you must assign an object reference to the variable before you can use the object's properties,and events. You can assign a new object reference in several ways: New,GetObject.

(2) Set objectvariable = CreateObject("progID",["servername"])

Regardless of whether or not an ActiveX component supplies a type library,you can use the CreateObject function in a Set statement to create a new object and assign an object reference to an object variable.

The object you want to access must be externally creatable.

The progID argument is usually the fully qualified class name of the object being created; However,progID can be different from the class name.

The optional servername argument can be specified to create an object on a remote machine across a network.

The following code example starts Microsoft Excel (if Microsoft Excel is not already running) and establishes the variable xlApp to refer to an object of the Application class.

Dim xlApp As Excel.Application

Set xlApp = CreateObject("Excel.Application")

(3) The GetObject function is most often used to assign a reference to an existing object,although you can also use it to assign a reference to a new object.

If the ActiveX component is running Result
Set X = GetObject(,"MySrvr.Application")
X references an existing Application object.
Set X = GetObject("","MySrvr.Object") X references a new,externally creatable object.
If the ActiveX component is not running Result
Set X = GetObject(,"MySrvr.Object") An error is returned.
Set X = GetObject("","MySrvr.Object") The ActiveX component (MySrvr) is started,and X references a new object.

(4) You can also use GetObject to assign a reference to an object in a compound document file. A compound document file contains references to multiple types of objects.

The following example starts the spreadsheet application,if it is not already running,and opens the file Revenue.xls:

Dim xlBook As Excel.Workbook
Set xlBook = GetObject("C:/Accounts/Revenue.xls")

6. Speeding Object References

Early binding dramatically reduces the time required to set or retrieve a property value,because the call overhead can be a significant part of the total time. For method calls,the improvement depends on the amount of work the method does. Short methods,where the call overhead is comparable to the time required to complete the task,will benefit the most.

原文链接:https://www.f2er.com/vb/262997.html

猜你在找的VB相关文章