ActiveX 控制教程-vb6代码 柳伟翻译 (转载请注明作者)
源出处:http://http://www.developer.com/net/vb/article.php/10926_1539541_3 作者:Karl Moore
An ActiveX component is just a general term, encompassing:
- An ActiveX EXE
- An ActiveX DLL
- An ActiveX Control
An ActiveX component is not:
- Active, in any way, shape or form
- A source of fibre that can help you lose weight as part of a calorie controlled diet
So what exactly are ActiveX EXEs and DLLs? Basically, they're chunks of code you use in your Visual Basic projects just by setting a reference to them — a little like how you set a reference to DAO or ADO when you need access to a database.
what are ActiveX controls?
Indeed, every time you set the Text property of a Text Box, you're utilising an ActiveX control. Every time you respond to the Click event of a Command Button, you're utilising an ActiveX control. Every time you run the MoveNext method of the Data control, you're utilising an ActiveX control.
I think you get the picture. In essence, an ActiveX control is anything you might see in the Toolbox.
We're going to create a little option button that flashes a few times when you run a certain method. It's not overly useful, but could help grab a user's attention.
- Start Visual Basic
- Create a New 'ActiveX Control' project
A grey box should appear on your screen. This is your workspace — it's basically a form without a border, caption or minimize/maximise/close buttons.
First off, let's rename our ActiveX control:
- Change the Name property of UserControl1 to 'Flasher'
- Now change the Name property of Project1 to 'Animation'
Excellent! Now...
- Double-click on the Option Button control in the toolbox
- Remove the Caption property of the Option Button
- Change its Name property to 'optFlasher'
We've just added an Option Button to the workspace. Now let's add the Timer control:
- Double-click on the Timer control
- Change its Name property to 'tmrAnimation'
That's great. Now I want you to resize a few of the things on your screen. We'll be doing all this resizing in code later on, but for now:
- Move the Option Button to the very top left, so it just touches the corner edges of your workspace like this:


Now the stuff you currently see in your workspace will become your 'control', the thing your user sees when adding it to their forms.
Hmm, it's about time we added some code. Not much, just a lil'.
- Enter the code window by selecting View, Code
- Type in the following code:
Public Sub Flash()
tmrAnimation.Interval = 300
End Sub
let's add code to that...- In the Object drop-down list (which currently says General), select 'tmrAnimation'
- The Procedure drop-down list next to it should say 'Timer' — if not, select the 'Timer' event from the list
Your screen should look a little like this at the moment:

- Tap in the following code:
Static NoOfFlashes As Integer
' This is just a variable that holds
' a number - the 'Static' prefix just
' means it doesn't forget its value
' when this procedure is over...
optFlasher.Value = Not (optFlasher.Value)
' Sets the value of our Option Button
' to the opposite of its current value...
' so if it's "on", it'll be turned off -
' and vice versa
NoOfFlashes = NoOfFlashes + 1
' Increment the variable to show number
' of times we have "flashed"
If NoOfFlashes = 8 Then
' If we've had eight separate flashes so far
NoOfFlashes = 0
' Reset the NoOfFlashes...
tmrAnimation.Interval = 0
' ... and turn off the timer
End IfThat's it! You've completed the creation of your first ActiveX control.
Now let's put it to the test...
- Click File, Add Project
- Select 'Standard EXE' and click Open
Now we have two different projects open at the same time; our control and this new Standard EXE thing we've just created.
Let's add our new control to the Standard EXE now.
- Drag out the Flasher control (
) on your toolbar onto Form1, like this:

Top Tip: If your Flasher control is greyed-out... it means
your copy of Visual Basic has been attacked by huge killer
bees from the terrifying jungles of Outer Mongolia or you
haven't closed the workspace of your control. Hmm, probably
the latter actually. Close the workspace and try again!
See how your Option Button appears?
Look in the Properties window. Can you see all the Properties your control already has?
A Name property, TabIndex, ToolTipText... and more! These are all assigned by default.
Now...
- Add a Command Button to the form
- Place the following code behind it:
Flasher1.Flash
The method you've just tapped in is the one we coded!
When we added the 'Public Sub Flash' code, it's automatically turned into one heckuva groovy method!
Try hitting F5 and running your application. Now hit the Command Button! See what happens?
The Option Button should flash for a few seconds... great for highlighting a warning of some sort.
t not so great at anything else.
part1 end here.

浙公网安备 33010602011771号