Example of moving an animation rectangle-Reply to "ps8.0"

Source: Internet
Author: User
Problem Source: http://www.cnblogs.com/del/archive/2008/06/12/1131232.html#1224216
First, there are two ways to implement this problem:

1. edges and edges. 2. The image is first drawn in the memory and then copied to the front-end. This is the so-called double cache.

In this example, we use the traditional "edge paint" method, but we advocate the "Double Cache" method.

This example (the actual animation effect is better than this GIF ):


Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; timer1: ttimer; Procedure handle (Sender: tobject); Procedure button2click: tobject); Procedure button3click (Sender: tobject); Procedure timer1timer (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var pt1, pt2: tpoint; {two points} n: integer = 20; {used for square size} CVS: TCanvas; {canvas} A, B, AI, Bi: single; Procedure tform1.formcreate (Sender: tobject); begin timer1.enabled: = false; timer1.interval: = 25; button1.caption: = 'draw square '; button2.caption: = 'mobile'; button3.caption: = 'stopped'; CVS: = TCanvas. create; CVs. handle: = getdc (handle); self. color: = clwhite; end; Procedure tform1.button1click (Sender: tobject); begin {assign values to two points} pt1.x: = 30; pt1.y: = 30; pt2.x: = clientwidth-30; pt2.y: = clientheight-30; {write down the position of the start point} A: = pt1.x; B: = pt1.y; {calculate the offset of each time} AI: = (pt2.x-pt1.x)/100; {if 100 moves are completed} bi: = (pt2.y-pt1.y)/100; {draw a rectangle at the first position and use a random color} randomize; CVs. pen. color: = random ($ ffffff); CVs. pen. width: = 4; CVs. rectangle (round (a-n), round (B-N), round (a + n), round (B + n); end; Procedure tform1.button2click (Sender: tobject); begin timer1.enabled: = true; end; Procedure tform1.button3click (Sender: tobject); begin timer1.enabled: = false; end; Procedure tform1.timer1timer (Sender: tobject ); begin if a> = pt2.x then begin timer1.enabled: = false; exit; end; {erased previous} CVs. pen. mode: = pmnotxor; CVs. rectangle (round (a-n), round (B-N), round (a + n), round (B + n); {re-draw} CVs. pen. mode: = pmcopy; A: = a + AI; B: = B + Bi; CVs. rectangle (round (a-n), round (B-N), round (a + n), round (B + n); end; Procedure tform1.formdestroy (Sender: tobject); begin CVs. free; end.
 
   
 

Form file:

 Object form1: tform1 left = 0 Top = 0 caption = 'form1 'clientheight = 214 clientwidth = 335 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false position = podesktopcenter oncreate = formcreate ondestroy = formdestroy pixelsperinch = 96 textheight = 13 object button1: tbutton left = 252 Top = 8 width = 75 Height = 25 caption = 'button1' taborder = 0 onclick = button1click end object button2: tbutton left = 252 Top = 39 width = 75 Height = 25 caption = 'button2' taborder = 1 onclick = button2click end object button3: tbutton left = 252 Top = 70 width = 75 Height = 25 caption = 'button3' taborder = 2 onclick = button3click end object timer1: ttimer ontimer = timer1timer left = 168 Top = 80 endend

   

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.