[C#] Timer + Graphics To Get Simple Animation (简单的源码例子,适合初学者)
>_<" 这是一个非常简单的利用C#的窗口工程创立的程序,用来做一个简单的动画,涉及Timer和Graphics,适合初学者,高手略过~
PS:请忽略菜单栏的东西~背景改成了白色!
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Drawing.Drawing2D; 11 using System.Drawing.Imaging; 12 13 namespace DrawHS 14 { 15 public partial class DrawHS : Form 16 { 17 public int x = 0, y = 0;//物体坐标 18 19 public DrawHS() 20 { 21 InitializeComponent(); 22 } 23 24 private void timer1_Tick(object sender, EventArgs e) 25 { 26 x++; y += 10;//运动 27 Refresh();//刷新窗口,刷新之后调用Paint函数 28 } 29 30 private void DrawHS_Paint(object sender, PaintEventArgs e) 31 { 32 Graphics g = e.Graphics;//绘图 33 Rectangle rect = new Rectangle(x, y, 100, 100); 34 LinearGradientBrush lBrush = new LinearGradientBrush(rect, 35 Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); 36 g.FillRectangle(lBrush, rect); 37 } 38 } 39 }