ActiveX (i)

Source: Internet
Author: User
Tags implement
Active|activex outline

It can be said that the active platform represents Microsoft's worldview. With ActiveX controls, it is Microsoft's intention to build an organization that includes a transaction monitor that interacts with the user and adapts to COM, to a Web server and to all automation. The desktop, COM based component is called an ActiveX control. An ActiveX control is simply a COM object that conforms to a certain standard and interacts with the client. Based on the application development of the component, the method is the same as assembling electronic device, which can be used to construct the application with the component parts which have been made. This is really a fascinating technique. Although sophisticated, with the widespread use of ActiveX, more and more programming tools support the creation of ActiveX controls. This includes VB. Designing an ActiveX in VB is no more difficult than a common VB application. This article is a step-by-step way to teach you how to create an ActiveX control in VB.


ActiveX (i)
Author: Young Eyebrow


What is an ActiveX control?

A simple answer is: A class that has a graphical interface. You may have used classes to program, which is a good way to implement code reuse. Of course, it offers a lot of other benefits, and it's not going to be listed here. The ActiveX control deepens the concept in a way that allows you to write a widget and then package it, use it in later applications, or provide a simpler programming method for programmers as a solution to specific problems. With ActiveX you can build a "composite" control, which is composed of several other controls. That is, in an ActiveX control you can use not only common controls such as text boxes and picture frames in VB, but you can also use other ActiveX controls to make your own controls, implement the functionality you want, and package it for the back.

Composition of ActiveX controls

An ActiveX control consists of some of its members: properties, methods, and events. What are the logical connections between them? Let's use our body as an example, and look at the body as an ActiveX control. The control should first have some properties, such as whether the eye opens the property. Obviously, this attribute value should have two kinds of conditions: open or closed. When used, you can tell the "body control" to change the property to a new value to determine whether to open your eyes or close your eyes, or to get the current attribute value to know your current physical condition.

A method is a general designation of procedures and functions in a control, and it is no different from any other VB function and procedure, and you can also pass parameters to them and return the desired value. Suppose a class depicts the object of the body, it should have a "look" method, and should be able to accept the "Direction" parameter, then this method should be written like this:

Public Sub-Look (Direction as Integer)
Select Case Direction
Case 0
' Look to the left
Case 1
' Look right
Case 2
' Look forward
Case 3
' Look Back
End Select
End Sub

If you ask for a return value, let's give a "read" example. Then you have to declare "Read" as a function rather than a process:
Public Function Read () as String
' Read some of the actions
Read = "Hello from the World"
End Function

In the body control, we use the "look" method. When this method is invoked, it is specified that the eye should be "seen". Similarly, we have also specified another method, "Read". This method will be used to return something to see.

Is the concept of "method" very simple? If you are not yet able to understand, you can also think of this: your control is like a machine, you move the control switch (input parameters), rotate the handle (call method), then the machine lights flashing, run up (the method), and finally from the machine to jump out something (return value), that's it. But wait ... If your machine wants to tell you something, what should it do? It is time for the "event" to appear.

Finally, the body control provides a "Blink" event to notify the developer of this event when blinking, but does not need to know how the body works and why the event is triggered.

Come on, make a control. Don't think it's hard to make a control, although it's a bit different from the general application writing. As we already know, an ActiveX control is made up of attributes, methods, and events, let's look at how to implement these things in a program, and then connect them together into a whole right machine. The simplest form of a property is a common variable declared with public. For example, if you put the following code in the Declaration section of your control project:

Public Eyeopen as Boolean

This allows you to use this attribute in the code that is behind it. But there are so few things that this attribute can do. It is almost impossible to work properly. Because during the design of a program, any change in property values must notify Visual Basic to mark the instance of the control as needing to be saved. And because property values may appear in more than one place, the development environment must be notified when a property value changes so that it can synchronously display property values at locations such as the Properties window, the Property Pages dialog box, and so on.

Is that a little hard to understand? If you do not understand what to say, do not worry, you first open a project, add a control, try to change the control of some properties, to see the above words, is it clear? Our task now is to make a control like this. From this we can see that there are some differences between control programming and general programming. So how do you implement a few lines? This is to use the property procedure.

When a property value is referenced or set, the property procedure is invoked automatically. Let's add one of these properties: Open the Code window, click on the "Tools" menu, select the "Add Procedure" submenu, the pop-up dialog box, fill in the procedure name "Eyeopen", and then set the type to "properties". When you click OK, VB automatically creates a prototype of the property process for you, and the code is added to the Code window:

Public Property Get Eyeopen () as Variant

End Property

Public Property Let Eyeopen (ByVal Vnewvalue as Variant)

End Property

The rest you have to do is write the attribute processing code to fill the skeleton.

You can see, VB actually wrote for you two, "get" and "let" property process, a little puzzled? It's really simple: "Get" is the procedure that is invoked when a property value is referenced, and "let" is called when the property value is written. (In fact there is a third type of process that we'll encounter later, here's a wake-up call) to make a property process work correctly, you must also have a variable to hold the true attribute value. Add the following line to the declaration section:

Private M_eyeopen as Boolean

Note that the ' m_ ' prefix is typically placed before the internal variables of the user control. Now it's time to populate the process framework. Take a look at the let process, which takes a parameter: By default, the name is the vnewvalue,variant type. But we want a boolean-type variable, not a variant, because the eye is only open and closed in our model. So change this parameter to "New_eyeopen as Boolean". If you want this property to appear as a read-only property, do not change the let process so that the process is empty. Otherwise, when you want to set this property value, you should execute this code:

Public Property Let Eyeopen (New_eyeopen as Boolean)

M_eyeopen = New_eyeopen

PropertyChanged "Eyeopen"

' You can also write some related code to respond to the settings of the property.

End Property

When you write a property value in a program, you call the function, save the property value in a private variable, and then execute the internal method of propertychanged, which tells the Visual Basic that the value of the property has changed. and triggers a writeproperties event. The specific contents of this point will be mentioned later.

And the get process is simpler! It is no different from a standard function:

Public Property Get Eyeopen () as Boolean

Eyeopen = M_eyeopen

End Property

Is that all you've done? No! Forget that the attribute values mentioned above need to be saved, so they can be maintained when the programming session is converted. So how do you save and retrieve attribute values? It's time to use the PropertyBag object.

Using PropertyBag

The PropertyBag object contains two methods: one to read out and one to write to. As mentioned earlier, when any property changes, it triggers the control's WriteProperties event. You can then save the property value in the property bag. The following code implements this functionality:

Propbag.writeproperty "Eyeopen", M_eyeopen, True

Propbag is an instance of the PropertyBag object. The WriteProperty function contains three parameters, the first of which is the property name, followed by the value to be saved, and the final parameter is the default value that will be written without a user-defined attribute. By combining this with the default value setting for the Readpropertiy method, you can set the default value for the property value. If the property value is the same as the default value, then the property value is not really saved. When it is read out, the ReadProperty function finds that there is no content in the property bag and returns the default value. This can save you some system overhead. Note that the name of a member property must be passed as a string. When you internationalize a control, do not change the name of the string, which must match the name of the attribute's declaration.

After the control is restarted, you must overload the values of all the saved attributes. The ReadProperties event is triggered each time the data in the PropertyBag is read. In the process of this event, the task you want to do is to load the attribute value in the property package, call the ReadProperty function to implement the value. Note that the default values are set to be the same in both read and write two functions. For example:

M_eyeopen = PROPB



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.