利用定时器、双缓存在窗体上画图,并实现动画
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace MyForm{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {//启动有窗体定时器 if (this.timer11.Enabled == false) { this.timer11.Start(); } x = 10; y = 50; } private void button2_Click(object sender, EventArgs e) {//关闭有窗体定时器 this.timer11.Stop(); } private void timer1_Tick(object sender, EventArgs e) { x = x 2; w = 40; h = 40; Bitmap bmp = new Bitmap(this.Width, this.Height-50); Graphics buffergraphics = Graphics.FromImage(bmp); Color bc = this.BackColor; buffergraphics.FillRectangle(new SolidBrush(bc), 0, 0, this.Width - 1, this.Height - 1); buffergraphics.DrawEllipse(Pens.Blue, x, y, w, h); //4、将内存画布画到窗口中 Graphics g; g = this.CreateGraphics(); g.DrawImage(bmp, 0, 50); } public int x, y, w, h; private void Form1_Load(object sender, EventArgs e) {//初始化图形数据 this.timer11.Interval = 20; x = 10; y = 50; } private void panel1_Paint(object sender, PaintEventArgs e) { } }}
评论