Visual Basic animation programming technology

zhaozj2021-02-11  171

Visual Basic Movie programming technology uses Image and Timer controls in Visual Basic to facilitate animation. This article will introduce the basic principles of Visual Basic animation programming and implementation of three different types of animation programming techniques.

First, the basic principle animation is an simulation of motion, its implementation method is to quickly display a set of related images on the screen. Therefore, the foundation of the animation is the display of the image and makes the image quickly, timed or changed. In Visual Basic, load the image file in BMP, ICO, and WMF formats with the LoadPicture function, and give the function return value to the porture property of the Image object, you can display images in the Image object. There are three basic methods of moving or change image, namely: 1. Move the image with the image object, the LEFT and TOP properties indicate the current left corner position of the Image object; 2. Call the LoadPicture function loads different images and assigns a different image to the image object, which will display different images in the object, that is, the image change is achieved; 3. Modify the width and / or height attribute of the Image object to zoom the image. Time control can be implemented using the Timer control. The Interval property of the Timer object sets the time interval, that is, the time interval for calling the Timer event process. Processing the movement or change of the IMAGE object during the Timer event process can be achieved. The Interval value of the Timer object determines the change or movement speed of the animation, and its unit is milliseconds (1/1000 seconds). The enabled attribute of the Timer object determines if the Timer event is valid. Setting enabled attributes to True will start Timer events (such as start moving); for false, the Timer event will be invalid (such as stopping animation).

Second, the omnipotent moving painting is not a moving picture means that the animation object does not move, but the image is constantly changing, and the typical example is turning the book. The method of realizing omnipotent moving pictures is to set the image object and the Timer object, call the loadPicture function during the Timer event process to load different images, and give the image object's Picture property, so that different images are displayed in the object, ie Implement image changes. Here is an example of turning the book animation. When the program starts, display a book opened in the form, with the left mouse button, will start the book; then use the left mouse button to click on the book, the book will stop repeating. The bitmap file book1.bmp ~ book4.bmp exhibits a different location of the book page that is turned on, which is stored in the directory where the current project is located. Set Image Object Image1 and Timer Timer1 in the form (Form1) that needs to display the animation, and set their properties to the following table, the attributes not listed use the default. Object Property Settings Image1Picture ... (Project File Path) Book1.Bmptimer1enableDfalse

The Interval150 program code is as follows: Option Explicit 'variable must be defined first before you can use DIM IMAGENO%' Current bitmap number DIM Isplaying as boolean 'animation to start

Private Sub Form_Load () isplaying = false 'starts, the animation does not start image1.tooltiptext = "Start" The "Start" prompt image = 1' animation from Book1.bmp Start End Sub

Private Sub Image1_Click () 'If IsPlaying Then click on the book with the left mouse button' animation has been started, stop IsPlaying = FalseTimer1.Enabled = IsPlayingImage1.ToolTipText = "start" Else 'animation is not started, start IsPlaying = TrueTimer1.Enabled = ISPLAYINGIMAGE1.ToolTIPTEXT = "Stop" The "Stop" prompt End IFEND SUBPRIVATE SUB TIMER1_TIMER () 'Timer Event ImageNo% = iMAGENO% 1' Animation Next Frame IMAGENO%> 4 THEN ' If the animation has arrived at the last frame, iMAGENO% = 1 'starts from the first frame end if

`Current Frame in Image Object Image1.Picture = LoadPicture (app.path &" ook "& imageno% &" .bmp ") End Sub code specifies the path where the current application is located.

Third, single frame bit mobile draw single frame bit mobile painting, refers to an animation that is constantly changing in the same image, and the cloud is blown by the wind. The method of preparing a single frame bit mobile picture is to call the image object in the TIMER event process to move the image. Here is an example of cloud movement. When the program starts, display a cloud in the form, click on the cloud with the left mouse button, then the cloud will start fluttering, if you touch the boundary of the form, the cloud will change the direction of movement; then use the left mouse button to click Cloud, then cloud The move will be stopped. Bitmap file cloud.BMP is stored in the directory where the current project is located. Set Image Object Image1 and Timer Timer1 in the form (Form1) that needs to display the animation, and set their properties to the following table, the attributes not listed use the default. Object Properties Settings Value Image1Picture ... (Project File Path) Cloud.Bmptimer1enableDfalse

The Interval150 program code is as follows: Option Explicit 'variable must be defined first before you can use DIM ISPLAYING AS BOOLEAN' animation to start DIM DETAX%, Detay% 'moves along the X, Y axis

When Private Sub Form_Load () isplaying = false 'starts, the animation does not start image1.tooltiptext = "Start" The "Start" prompt DETAX% = 100' along the moving position increment along the X axis 100 units Detay% = 100 'along the Y-axis movement location increment is 100 units End Sub

Private Sub Image1_Click () 'If IsPlaying Then click on the book with the left mouse button' animation has been started, stop IsPlaying = FalseTimer1.Enabled = IsPlayingImage1.ToolTipText = "start" Else 'animation is not started, start IsPlaying = TrueTimer1.Enabled = IsplayingImage1.tooltiptext = "Stop" end ifend sub

Private sub timer1_timer () 'Timer Event' Move Image Object Image1.Move Image1.Top Detax%, Image1.top Detay% 'When you touch the boundary, correct the location and reverse if image1.Left image1.width> = Form1.Width Ten 'hit the right bound1.move form1.width - Image1.width, Image1.topdetax% = -detax% elseif Image1.top image1.height> = form1.height kilove Image1.heft, form1.height - image1.heightdetay% = -detay% elseif Image1.Left <= 0 TEN 'hitting the left edge image1.move 0, image1.topdetax% = -detax% elseif Image1.top <= 0 THEN 'Touching the lower boundary1.move image1.left, 0detay% = -Detay% END IFEND SUB Note that the clouds come into the boundary and reverse movement, is achieved by changing the Detax%, Detay% symbols.

Fourth, multi-frame bit moving multi-frame bit mobile painting is the most complex animation, combining the characteristics of unbensorship and single framework. Most of the sports in nature have the characteristics of multi-frame shifts, such as the flying birds, while moving at the position of the bird, its wings are also fanned. Implementing multi-frame bit mobile paintings requires image replacement and location movement of Image objects while simultaneously processing during TIMER events. Here is an example of bird flying. When the program starts, a bird is displayed in the form, click on it with the left mouse button, then the bird will start the fan and wings, such as touching the boundary of the form, the bird will change the flying direction; then use the left mouse button Click on the bird, then the bird will stop flying. Bittap file Bird1.bmp ~ bird4.bmp expresses the birds flying in the different positions of their wings, they are stored in the directory where the current project is located. Set Image Object Image1 and Timer Timer1 in the form (Form1) that needs to display the animation, and set their properties to the following table, the attributes not listed use the default. Object Properties Settings Value Image1Picture ... (Project file location) Bird1.bmptimer1enabledfalse

The interval150 program code is as follows: Option expendit 'variable must be defined first, can you use DIM IMAGENO%' Current bitmap number DIM ISPLAYING AS BOOLEAN 'animation to start DIM DETAX%, Detay%' moves along the X, Y axis

Private Sub Form_Load () isplaying = falseImage1.tooltiptext = "Start" iMageno% = 1DETAX% = 100Detay% = 100nd Sub

Private Sub Image1_Click () If IsPlaying ThenIsPlaying = FalseTimer1.Enabled = IsPlayingImage1.ToolTipText = "start" ElseIsPlaying = TrueTimer1.Enabled = IsPlayingImage1.ToolTipText = "stop" End IfEnd Sub

Private sub timer1_timer () 'Timer event imageno% = imageno% 1' Animation next frame if imageno%> 4 Then 'If the animation has arrived at the last frame, iMAGENO% = 1' starts END IF from the first frame. Current Frame Image1.Picture = LoadPicture (App.Path & "IRD" & IMAGENO% & "IRD")

'Move Image Object Image1.Move Image1.Left Detax%, Image1.top Detay%

'Touch the boundary, correct the location, and reverse if image1.veft image1.width> = form1.width Then' to touch the right bound1.move form1.width - Image1.width, Image1.topdetax% = -Detax% Elseif Image1.top Image1.Height> = form1.height the 'hit on the upper boundary1.move image1.left, form1.height - Image1.HeightDetay% = -Detay% elseif image1.left <= 0 Then' encountered left Boundary image1.move 0, image1.topdetax% = -detax% elseif image1.top <= 0 TEN 'hitting the lower boundary image1.move image1.Left, 0detay% = -Detay% End iFend Sub

5. The expansion or reduction of the zoom animation balloon is a typical example of zooming animation. To modify the Width and / or HEIGHT property of the Image object during the Timer event, you can realize the zoom animation. However, if you want to express concentric scaling of objects, you should also move image objects at the same time. Below is an example of simulating balloons in air scaling (concentric). When the program starts, a balloon is displayed in the form, click on it with the left mouse button, the balloon will begin to expand, if you touch the boundary of the form, the balloon will shrink, narrow to the original size, and expand; The mouse button hits the balloon, the balloon will stop scaling. Bitmap file Balloon.BMP is stored in the current project. Set Image Object Image1 and Timer Timer1 in the form (Form1) that needs to display the animation, and set their properties to the following table, the attributes not listed use the default. Object Property Settings Image1Picture ... (Project File Path) Balloon.BMP

StretchTruetimer1NableDfalse

The Interval150 program code is as follows: Option Explicit `Variable must first define it can be defined before you can use Dim Isplaying as Boolean` animation to start DIM width0%, height0% `Balloon original size DIM DETAX%, Detay%` Return along the X, Y axis

Private Sub Form_Load () isplaying = false image1.tooltiptext = "Start" width0% = image1.width 'Save the balloon's initial size height0% = image1.heightdetax% = 100Detay% = 100nd Sub

Private Sub Image1_Click () If IsPlaying ThenIsPlaying = FalseTimer1.Enabled = IsPlayingImage1.ToolTipText = "start" ElseIsPlaying = TrueTimer1.Enabled = IsPlayingImage1.ToolTipText = "stop" End IfEnd SubPrivate Sub Timer1_Timer () `Timer event` move the Image object, remain concentric Zoom Image1.Move Image1.Left - DETAX% / 2, Image1.top - Detay% / 2 'Add Image1.Width = Image1.Width Detax% Image1.Height = Image1.Height Detay%

`Touch the boundary, then reduce if image1.Left image1.width> = form1.width or_image1.top image1.height> = form1.height or _image1. lefD <= 0 or image1.top <= 0 Thendetax% = -Detax% Detay% = -Detay% end if

`Returned to the original, then expand IF image1.width <= width0% or image1.Height <= height0% thendetax% = -detax% Detay% = -Detay% end iFend Sub

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

New Post(0)