Hand teaches you to learn ActiveX controls

zhaozj2021-02-16  54

Hand teaches you to learn ActiveX controls

Yangshan River

The people who learn VB must not be unfamiliar with the ActiveX control. In most cases, the process of using VB programming is actually using the various properties of the control, and the method of responding to the event. The control is a software component such as Microsoft to prepare a software component prepared by selling to the user, and uses these software vendors to make a variety of functions to achieve our various requirements. The control provided by VB itself can help us complete most applications, but sometimes there is always the need for software vendors, "do it yourself, full of food", which we can use VB power to write your own ActiveX control.

What do you want to write controls? The ActiveX control provides the user to access settings, affects the behavior of the control; provides methods for program calls; providing event responses for capturing events. We design a control called "MoveText" here that the main role of this control moves at a given text, which can be placed in the web page, dynamically display some friendly information. Of course it can also be used by other programming languages ​​that support ActiveX. Our controls should be provided with text, which represents the text that moves on the control, and is also prepared to respond to the user's Click event. Talk to dry, Three, Two, One, Let's Go!

Create an ActiveX control engineering to start VB5.0. Select New Project under File, select ActiveX Control to determine in the engineer list. After the previous step, there will be no border, buttons "appear, this is the" prototype "of VB to write controls we provide, its name is UserControl, very ugly, we modify the Name property in the VB property window. , Change it to "MoveText", which is our control formal name.

Add attributes, wake-up events Right-click the MoveText in the project window, select View Code in the pop-up menu, and enter the code window.

Enter the following code under Option Explicit: Public text as string. Declaring the Text variable of MoveText as a public, and actually regulate it as a control

Item, the type of attribute is String.

Enter a statement under Public Text As String: Public Event Click (), then enter the code in the code window: private sub usercontrol_click ()

RaiseEvent Click

End Sub

In this way, MoveText can add Click events in the event that can respond.

If you want to provide a method, just write a SUB.

Detailed design control behavior In order to generate the function of writing, we can let the text use the foreground color to write over again. After a while, after a period of time, write once in the original position, so that the cycle reciprocates the feeling of writing. The "prototype" of the controls mentioned earlier is actually a window, in order to display text here, we can use the Windows API function to control the precise positioning of color, location, etc. For accuracy, we need to place a Timer on the "Canvas". Specific steps are as follows:

Start the API Text View included with the VB5, browse Win32API.txt, copy the textout, getTextColor, setTextColor, GetBkcolor, etc., the declaration of the declaration of functions such as GetBkcolor to the code window of the VB currently engineered. Plus keyword private before declaration of each function, otherwise the following compile cannot pass. For later code, the following variables are defined after public evening click (): DIM I as long 'This variable control text In the control, the coordinate (location) DIM oldtextColor as long' saves the current text before displaying text with background color view

DIM BKCOLOR AS long 'Save the current background color

Drag the Timer Control from the control panel to the control, set the INTERVAL attribute value of 80. Double-click the Timer icon, enter the code window, enter: Private sub timer1_timer ()

TextOutold 0, I, Text

i = i 1

IF I> = 30 THEN

i = 0

END IF

TextOutnew 0, I, Text

End Sub

The TextOutold and the TextNew process is customized, which is specifically description of the design of the background color output text and the exotic output text, respectively:

Private Sub TextOutold (X as long, y as long, mtext as string)

BKCOLOR = GetBkcolor (HDC)

OldtextColor = GetTextColor (HDC)

SetTextColor HDC, BKCOLOR

Textout HDC, X, Y, MTEXT, LEN (MTEXT) * 2 'Chinese characters two bytes, by 2

SetTextColor HDC, OldtextColor

End Sub

Private sub textOutnew (X as long, y as long, str as string)

TextOUT HDC, X, Y, STR, LEN (STR) * 2

End Sub

The HDC used in this group of functions is a MoveText inherited from the "canvas", without statement. With it, WinAPI can complete their respective features.

6. In order to give the variables used thereto, the appropriate initial value is implemented, and the following code is implemented:

Private sub UserControl_initialize ()

i = 1

TEXT = "Welcome"

End Sub

Fourth, compile control

Select Make Project1.ocx under the File menu, a dialog box is popped up to specify the name location of the compiled .ocx file, it is best to place it in a Windows / System directory, named MoveText.ocx. If the above steps are correct, you get a "authentic" ActiveX control, and VB will automatically register it to your system registry.

V. Test control

In order to test the control you have written, we will create a new project. At this time, there is no control that has just been prepared on the control panel. Let's right-click on the control panel, select Components ..., select the MoveText that has been created in the pop-up control list. If you do not place the MoveText control under Windows / System when compiling, you need to click the Brose ... button to select the file name of the control you just compiled. After that, the MoveText icon will appear in the control panel. Select MoveText, insert in Form1, you will see "Welcome" in the MoveText1 in Form1, if you modify the text attribute value of MoveText, such as "Welcome to your jujube computer enthusiast", you will immediately See this sentence moves on the control. You can even embed it amazed in your page, this is your own control! Appreciate it! Using VB to write VB controls is an exciting thing. While leading VB powerful features, we also see "development software" is just this! As long as you put it with your heart, it's hard to fall, really, I don't lie to you, don't believe, you try!

==

Written in 1998

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

New Post(0)