Use VB6 to design interesting animated scenes

Source: Internet
Author: User
Tags array erro integer reset sleep
A friend who designs a lot of TV must notice that many animated scenes are beautifully crafted, so can you design a similar scene with VB6, the answer is yes, the following code can slowly draw a random shape, you can set the density of tree trees, and can slowly in this tree "knot" A specified number of red "fruits"--very interesting.
(i) Programming principles
We can draw "tree trunks and branches" on the form with the appropriate width of the line control, and then use the shape control to draw the appropriate "fruit" of the size of the circle and place it at the tip of the "branch". The colors and sizes of the two controls can be set freely. In the following code, a new technique for dynamically creating a control array by VB6 is also shown.
(ii) Programming practices
Start VB6, build a Standard EXE project, add two command buttons COMMAND1 (caption= "Draw a Tree"), COMMAND2 (caption= "Show Fruit"), a Label control (caption= "tree density:"), and a text control TEXT1 (to set the number of branches), adjust the control to the appropriate location, double-click the form, and write the following code:
Option Explicit
Dim Createlines as Integer
Dim Lines as Integer
Dim Mline () as line ' branch
Dim Fruit () as Shape ' fruit
Dim Createfruit as Integer
Dim Apple as Integer
Dim Evaluate as Boolean ' has drawn a number
Dim Clear as Integer
Dim Eraser as Integer
Dim Showapple as Boolean ' whether the fruit has been shown
Private Declare Sub sleep Lib "kernel32" (ByVal dwmilliseconds as Long) ' Timer
Private Sub Command1_Click () ' Draw a branch
If Evaluate=true Then ' if it had been drawn out of the branch
For clear=2 to UBound (mline)
Set Mline (Clear) =nothing
Set Fruit (Clear) =nothing
Next
Controls.remove ("Motherline")
For eraser=2 to UBound (mline)
Controls.remove ("Linea" & Eraser)
Controls.remove ("Fruta" & Eraser)
Next
End If ' then clean them up
' Otherwise draw the branches directly according to the number set in the TEXT1.
' The number of branches
Lines=text1.text
ReDim Mline (1 to Lines) ' defines an array of branches
Set mline (1) =controls.add ("Vb.line", "Motherline")
' Initialize the trunk of the tree
With Mline (1)
. X1=form1.scalewidth/2
. X2=form1.scalewidth/2 ' according to the middle of the form
. Y1=form1.scaleheight
. y2=form1.scaleheight-1000 ' height 1000 units smaller than form
. Visible=true ' Visible
. Borderwidth=8 ' Trunk width 8
. Bordercolor=vbblack ' filled with black
End With
' Begin to draw the branches
For createlines=2 to Lines
Set mline (createlines) =controls.add ("Vb.line", "Linea" &createlines)
If createlines Mod 2=0 Then
' Draw a random line (branch) above the left.
With Mline (Createlines)
. X1=mline (CREATELINES/2). X2
. X2= (Mline (CREATELINES/2). X2)-int (rnd*1000)
. Y1=mline (CREATELINES/2). Y2
. Y2=mline (CREATELINES/2). Y2)-int (rnd*1000)
. Visible=true
. Bordercolor=vbgreen ' with green fill
. Borderwidth=3 ' width is 3
End With
Else
With Mline (Createlines)
' Draw a random line above the right
. X1=mline ((CreateLines-1)/2). X2
. X2= (Mline (CreateLines-1)/2). X2) +int (rnd*1000)
. Y1=mline ((CreateLines-1)/2). Y2
. Y2= (Mline (CreateLines-1)/2). Y2)-int (rnd*1000)
. Visible=true
End With
End If
DoEvents
Sleep (50) ' draws and displays a trunk every 0.05 seconds
Next
ReDim Fruit (2 to Lines)
' Draw the fruit of each branch, but not immediately, until you click the ' Bear fruit ' button
For createfruit=2 to Lines
Set Fruit (createfruit) =controls.add ("Vb.shape", "Fruta" &createfruit)
With Fruit (createfruit)
. width=200
. Height=200 ' bear the size of the fruit
. Left=mline (Createfruit). x2-100
. Top=mline (Createfruit). Y2-100 ' result location
. Fillcolor=rgb (255,0,0) ' filled in red
. Fillstyle=0 ' border type
. The fruit of the shape=3 ' round
. ZOrder 0
End With
Next
Evaluate=true ' Set the boughs which had drawn the mark
Showapple=false ' Set Show fruit sign
command2.caption= "Show fruit" set the result button title
End Sub
Private Sub Command2_Click () ' Bear the fruit button pressed
On Error GoTo Erro
If Showapple=false Then
' If the fruit doesn't show, then show them all
For Apple=lbound (Fruit) to UBound (Fruit)
Fruit (Apple). Visible=true
DoEvents
Sleep (50) ' shows a fruit every 0.05 seconds
Next
Showapple=true ' Reset display fruit flag
command2.caption= "Cancel the fruit"
Else
' If the fruit has been shown, then hide them all
For Apple=lbound (Fruit) to UBound (Fruit)
Fruit (Apple). Visible=false
Next
Showapple=false to reset display fruit flag
command2.caption= "Show Fruit"
End If
Erro:
If err.number=9 Then
MsgBox "must first draw a few, in order to bear fruit!"
End If
End Sub
Private Sub Form_Load ()
Me.caption=app.title ' Add application title
me.left= (screen.width-me.width)/2
me.top= (screen.height-me.height)/2 ' form fixture
Evaluate=false
Showapple=false
End Sub
Private Sub text1_validate (Cancel as Boolean)
' Verify that the number of branches is 0 or 1.
If text1.text= "" Or text1.text=1 Then
Cancel=true
MsgBox "must enter the number of branches!" And more than 1 ", vbOKOnly," Error "
End If
End Sub
Private Sub form_unload (Cancel as Integer)
End
End Sub ' Code ends
(iii) running debugging
First click the "Draw a Tree" button, then slowly in the center of the form to create a "tree", black trunk, green branches. Clicking on the "Show fruit" button produces 250 red rounded "fruit" on the tree. Isn't it very interesting?!
This example of the code is relatively simple, programming enthusiasts can completely replace line into a beautiful picture, as for the "fruit" of course can also be arbitrary design.

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.