博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一个简单的动态画图程序并用缓冲解决闪烁

Posted on 2008-10-30 12:00  yangyanbin  阅读(255)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Timers ;

namespace WindowsControlLibrary1
{

private System.Windows.Forms.Timer timer1;
    public partial class UserControl1 : UserControl
    {
        Color color1=Color .Red ;
        Color  color2=Color .Green ;
        private int angle = 0;
        public UserControl1()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

            satrt();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            LinearGradientBrush b = new LinearGradientBrush(this.ClientRectangle, color1, color2, angle, false);
            //LinearGradientBrush b = new LinearGradientBrush(this.ClientRectangle, color1, color2, LinearGradientMode .Horizontal );
            e.Graphics.FillEllipse (b, this.ClientRectangle);
        }
        protected void satrt()
        {
            this.timer1.Tick += new EventHandler(OnTimerEvent);
            this.timer1.Interval = 10;
            this.timer1.Enabled = true;
        }
        protected void OnTimerEvent(object sender, EventArgs e)
        {
            angle += 5;
            if (angle >= 360) angle = 0;
            this.Refresh();
        }
        public Color Co1
        {
            set { color1 = value; Invalidate(); }
            get { return color1; }
        }
        public Color Co2
        {
            set { color2 = value; Invalidate(); }
            get { return color2; }
        }
       

private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            //
            // UserControl1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "UserControl1";
            this.ResumeLayout(false);

        }

    }
}