您的位置:首页 > 其它

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

2010-09-06 10:34 357 查看
今天在论坛看到有人问如何在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;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: