[.NET] 如何利用GDI画一个箭头的动画

今天在论坛看到有人问如何在Win Form上画一个箭头

刚好我也需要用到一些GDI的学习,就把这个当成自己的练习做出来

 

我的思路很简单,首先利用OnPaint事件,画一个没有动画的箭头

然后再来思考要怎么样让箭头变成动画

我的想法是利用Thread,改变画图座标就可以达成这个效果

同时想到需要在最后才显示两个箭头,那就多了个判断的Flag

再来做一个Loop,让箭头一直画,这样就完成了一个简单的动画

 

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; using System.Threading; namespace WinTest2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool isRun; bool isDrawArrow; int lineX = 10; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red), 3), 10, 30, lineX, 30); if (isDrawArrow) { e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red), 3), 90, 20, 100, 30); e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red), 3), 90, 40, 100, 30); } } private void Form1_Load(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart(Animation)); t.Start(); isRun = true; isDrawArrow = false; } void Animation() { while (isRun) { this.Invalidate(); Thread.Sleep(200); for (int i = 0; i < 10; i++) { lineX = 10 + i * 10; this.Invalidate(); Thread.Sleep(200); } isDrawArrow = true; this.Invalidate(); Thread.Sleep(2000); isDrawArrow = false; } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { isRun = false; } } }

你可能感兴趣的:(thread,.net,object,Class,animation)