Funny animation scene with VB6
Frequent friends who often watch TV will notice that many cartoons are made very beautiful. Can you use VB6 to design similar scenes, the answer is sure, the following code can slowly draw random shapes, you can set The tree density of the density, and can slowly "knot" out of this tree out of the designated number of red "fruit" - very interesting. (1) Programming principle We can draw "Tree Trunk and Tree" on the form with appropriate width, then draw a "trunk and branches" with Shape control, and placed in the "branch" . The color and size of these two controls can be freely set. In the following code, a new technique of "no middle" dynamically created control array is also displayed.
(2) Programming practice starts VB6, establish a standard EXE project, add two command buttons command1 (capen = "Draw a tree"), Command2 (CAPTION = "Show Fruit"), a tag control (CAPTION = "branch Density: "), and a text control text1 (used to set the number of branches), adjust the above control to the appropriate position, double-click the form, write the following code: Option Explicit Dim CreateLines As Integer Dim Lines as integer mline () as line whether 'branches Dim fruit () As Shape' fruit Dim CreateFruit As Integer Dim Apple As Integer Dim Evaluate As Boolean 'is already drawn a number of Dim Clear As Integer Dim Eraser As Integer Dim ShoWApple As Boolean' fruit has been shown Private Declare Sub Sleep lib "kernel32" (BYVAL DWMILLISECONDS AS Long) 'Timer Private Sub Command1_Click ()' draws tree branches if evata = true then 'If you have already drawn branches for clear = 2 to Ubound (mline) Set mline (clear) = Nothing set fruit (Clear) = Nothing Next Controls.remove ("Motherline") for ERASER = 2 to Ubound (mlinea "Controls.Remove (" Linea "& Eraser) Controls.remove (" Fruta "& eraser) Next Endiffs So cleaped them 'otherwise draws the number of branches' branches directly in Text1, lines = text1.text redim mline (1 to lines)' Define branch array set mline (1) = controls.add ("vb.line" , "Motherline") 'Initialization Tree with mLine (1) .x1 = FORM 1. Scalewidth / 2 .x2 = form1.scalewidth / 2 'According to the intersection. Y1 = form1.scaleHeight .y2 = form1.scaleHeight-1000' height than the form small 1000 units. Visible = true 'visible. Borderwidth = 8 'Trunk Width 8. Bordercolor = VbbBlack' Starts Draws for Createlines = 2 to Lines Set Mline ("Vb.Line", "Linea" & Createlines) = = 0 THEN 'Draw a random straight line (branch) to the top 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. brordercolor = vbgreen 'with green fill. Borderwidth = 3' width is 3 end with else with mline (createlines) 'Picture to the top right Random line. 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 ifvens sleep (50) 'Painting every 0.05 seconds every 0.05 seconds And showing a trunk next redim fruit (2 to lines) 'draws the fruit of each branch, but doesn't display it right away until you click "Outlook Fruit" button for CreateFRuit = 2 to Lines Set Fruit (CreateFRuit) = Controls.Add ("vb.shape", "Fruta" & createfruit) with fruit (createfruit) .Width = 200.Height = 200 'The size of the fruit .left = mline (createfruit) .x2-100 .top = mline (Createfruit). Y2-100 'Results. Fillcolor = RGB (255, 0)' with red fill. FillStyle = 0 'Border type. SHAPE = 3' rounded fruit .zorder 0 end with next evata = True 'Setting the branch already draws the sign showapple = false' set the display fruit sign command2.caption = "Show Fruit" 'Set the result button title End Sub Private Sub Command2_Click () 结 结 i i = 如果 If the fruit is not displayed, then all display all for Apple = LBound (Fruit) to Ubound Fruit (Apple) .visible = True Doevent Sleep (50) 'shows a fruit next showapple = true' Reset ShowApple = True 'every 0.05 seconds. COMMAND2.CAPTION = "Cancel Fruit" Else' If the fruit is already displayed, then They all hide for apple = lbound (fruit) to Ubound (Fruit) Fruit (Apple) .visible =