Production of ActiveX control in Visual Basic (1)

xiaoxiao2021-03-05  22

The production of ActiveX controls in Visual Basic (1) We all know that many existing controls can be used in VB when using Visual Basic. However, only these basic controls can not meet our practical needs in many times, therefore also need more controls that implement other unique features. We can see that a large number of controls are included in the VB components. These controls are included in the ActiveX control library from VB. These controls have greatly supplemented VB's features. In fact, whoever masters the use of controls in VB, who will use VB to write powerful programs. In VB, you can not only use existing ActiveX controls, but more exciting is that you can use VB to create your own ActiveX control. Below we discuss the basic knowledge about the creation of ActiveX controls in VB. ActiveX is the term Microsoft term. He is a set of components including controls, DLLs, and ActiveX documents. He is usually in the dynamic connection library situation, so you must run in a separately executable software called containers, such as VisualBasic, Visualc , Internet Explorer et al. Therefore, ActiveX controls and containers must support some specific interface protocols. ActiveX should include special mechanisms such as attributes, events, and methods, and he is also the core of creating ActiveX controls. Starting with VisualBasic 5.0, we can create controls with VB, and VB6.0 inherits the ability of VB5.0 to develop third-party controls. Creating an ActiveX control has a certain step: (1) Determine the functionality to be implemented. This step is important, here we should determine the appearance of the control, and its properties, events, and methods. (2) Design control of the control through the UserControl object. (3) Add the code to userControl to implement the appearance and behavior of the control. (4) The interface of the design control, that is, attributes, events, and methods. (5) Create a VB's Standard.exe project, using the controlled controlled control. (6) Compile control components to generate .ocx files. In this production step we see the UserControl objects you want to use in VB, he is the basis for creating an ActiveX control. Let me briefly introduce the basic knowledge of the UserControl object. The ActiveX control created by VB includes the userControl control and the basic control constituting the ActiveX control, called constituent controls. These basic controls are actually like CommandButton and Label controls. We first open VB6.0 to select the ActiveX control option, and then displayed as shown in the figure:

Figure 1 There is no difference in the design interface of the UserControl object and the standard project of the standard engineering. We can design controls on the UserControl object, add the code to the Code page, everything is so familiar. UserControl stores in a normal text file format, which contains userControl and his constituent control source code and property value. The extension of these files in VB is .ct1 If the graphic element is used in the control, VB stores her in the .ctx file in the same name. UserControl objects have a lot of properties, events, and methods, like a normal Form form. The properties, methods, and events of the ActiveX control are implemented by members of the UserControl object. These members can be invisible for users by packaging. There are three ways to create an ActiveX control in VB. The first is to create a control from zero. The second is to improve the existing controls. The third is to assemble new controls with existing controls. Here I will use a very simple example to show how to make a self-owned control from zero. The controls we have to do is a control that displays system time. Here is to be prepared for classmates who have never produced controls. In the next article, I will introduce more complex ActiveX control creation methods. Below we will design according to the steps mentioned in the previously mentioned control: Step 1: Because we design a display system time control, you don't need how complicated on the appearance, just a label to display the time, you need a Timer to come Get the display interval of time. Step 2: Use the UserControl object to draw the appearance of the control. Add a label on the UserControl object, named: label1; plus a Timer Control name: Timer1. Set Timer1's Interval property to 1000, indicating a change in each second. We are the properties settings that can be seen in the properties of the UserControl object and FORMs are consistent. As shown in the figure: Figure 2 Step 3: We are going to add code to UserControl. Add the following code during the Timer1_Timer (): private sub timer1_timer () label1.caption = Time End Sub We can see that the system time is displayed in Label1. Step 4: Design control interface, this is the most important place, we add an attribute, an event, and a method for this simple control, intended to explain the method of design control interface. (1) Add modifyInterval properties: It is necessary to explain here. Since our control is very simple, there is no need to test the control with the Standard.exe project, but the best and standard engineering group is based on the complex control. Design, side design, which will increase production efficiency. The "Add Procedure" option is selected in the Tools menu will pop up the dialog box. We filled in ModifyInterval in the name, and "Properties" are selected in the type. as the picture shows:

Determination process in FIG. 3 Click Code Editor's automatic loading add the following code: Public Property Get ModifyInterval () As Variant 'Get ModifyInterval process of obtaining a value ModifyInterval = Timer1.Interval End Property Public Property Let ModifyInterval (ByVal vNewValue As Variant 'Let Process Set ModifyInterval Properties Timer1.Interval = VNewValue PropertyChanged (ModifyInterval) End PropertyPertyChanged () method, its function is that the value of the container property has changed, but it can be understood that this method tells the method when the attribute value changes The value of the container (unit of all attributes) secondary attribute already has a new value, and a WriteProperties event is required to save the new attribute value. In fact, not only during the Property Letter, you need to call the PropertyChanged () method, and you must call this method to save the new attribute value in the UserControl code module. (2) Adding a Click Event Add by the Click event, the process of adding an attribute, open the "Add Procedure" in the tool, select the event, fill in "Click" in the name of "Click" Add code in the code module: private sublick End Sub private sub usercontrol_click () RaiseEvent Click End Sub RaiseEvent is to activate the Click event of Label1 and UserControl. (3) Add a method TestFunction () method as before, here no longer illustrates, add code as follows: public function testfunction () MsgBox ("Test Function SuccessFully!") End Function Call this method is to pop up a dialog. "TEST FUNCTION SUCCESSFULLY!" Here we have basically completed the creation of this simple control, and then compile it. Open the File menu, select the "Generate DataTimer.ocx" option to store the corresponding path

Figure 4 Step 5: Debugging the control just created: We first open a Standard.exe project to select the "Part" option in the "Project" menu, pop up the VB6.0 ActiveX control library, click "Browse" to find Just built a DataTime.ocx file to add to the control library, then you will see more "Engineering 1" options in the library, select the DataTimer control in the control toolbox, add it in the Form This control, select it, you can see the ModifyInterval properties you just added in the property bar, you can also see the DataTimer control has a Click event. Add the code to add a Command () in DataTimer1_Click in the Form: Private Sub UserControl11_Click () Command1.Caption = "You have successfully used the Click event" UserControl11.ModifyInterval = 1000 UserControl11.TestFunction End Sub After the execution we see in Fig. : Figure 5 To this, we have completed the control of the control. By creating this control We can see very simple when you create an ActiveX control with VB, but I don't say that it is so simple to design any controls. I refer to the convenience and efficiency of the creation method. The above example is just the simplest example of a design control, but he has a good general value. In fact, complex control is also designed in accordance with this general step. In the future, I will also use a more complex example to explain the production method of the ActiveX control. Here you should explain that ActiveX is a set of things, including controls, DLL, and ActiveX documents. In future articles, we will also learn from the creation of ActiveX documents. This article is intended to learn how to create an ActiveX control in VB. ActiveX technology can be flexible, efficient, interactive, reused, fully distributed, and is completely unrelated to language. With the development of ActiveX technology, the ActiveX control is increasingly important, then create a control with its own personal characteristics and special features will appear more and more practical. Just master the basic way to create an ActiveX control is not difficult to create more complex controls. However, ActiveX controls only rely on Windows platforms, which does not support the UNIX platform, and ActiveX technology is still constantly improving.

转载请注明原文地址:https://www.9cbs.com/read-32074.html

New Post(0)