动画渐变按钮在绘画时闪烁



我创建了GradientButton,当鼠标光标在其边界内时,它会改变渐变角度。不幸的是,图形已损坏,因为在渲染过程中有随机闪烁。

为了实现梯度旋转,我在MouseEnter时启动一个线程,然后在MouseLeave处停止它。双缓冲设置为 true,帮助很大,但没有完全解决这个问题。

  public partial class GradientButton : UserControl {
        public Color Color1 { get; set; }
        public Color Color2 { get; set; }
        public bool Down { get; set; }
        public bool MouseOnButton { get; set; }
        [Browsable(true)]
        public  string TextToDraw { get; set; }
        public int Angle { get; set; }
        public GradientButton() {
            InitializeComponent();
            Color1 = Color.YellowGreen;
            Color2 = Color.LightGreen;
        }
        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            var color1 = Color1;
            var color2 = Color2;
            if (Down) {
                var temp = color1;
                color1 = color2;
                color2 = temp;
            }
            if (MouseOnButton) {
                color1 = ControlPaint.Dark(Color2);
            }
            using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, color1, color2, Angle)) {
                e.Graphics.FillRectangle(brush, this.ClientRectangle);
            }
            Rectangle rect1 = ClientRectangle;
            // Create a StringFormat object with the each line of text, and the block
            // of text centered on the page.
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            // Draw the text and the surrounding rectangle.
            e.Graphics.DrawString(TextToDraw, Font, new SolidBrush(ForeColor), rect1, stringFormat);       
        }
        protected override void OnClick(EventArgs e) {
            base.OnClick(e);
        }
        protected override void OnResize(EventArgs e) {
            base.OnResize(e);
            Invalidate();
        }
        protected override void OnMouseDown(MouseEventArgs e) {
            base.OnMouseDown(e);
            Down = true;
            Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e) {
            base.OnMouseUp(e);
            Down = false;
            Invalidate();
        }
        protected override void OnMouseEnter(EventArgs e) {
            base.OnMouseEnter(e);
            MouseOnButton = true;
            Thread t = new Thread(Animate);
            t.Start();
        }
        public void Animate() {
            while (MouseOnButton) {
                Angle += 5;
                Thread.Sleep(25);
                Invalidate();
            }
        }
        protected override void OnMouseLeave(EventArgs e) {
            base.OnMouseLeave(e);
            Angle = 0;
            MouseOnButton = false;
            Invalidate();
        }
    }

GradientButton.Designer.cs

partial class GradientButton {
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Component Designer generated code
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {
        this.SuspendLayout();
        // 
        // GradientButton
        // 
        this.Name = "GradientButton";
        this.Size = new System.Drawing.Size(78, 28);
        this.ResumeLayout(false);
    }
    #endregion


}

使用

SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlsStyles.Opaque, true); 

以防止在 OnPaint 处理之前绘制背景。这应该可以防止背景擦除和绘画之间的闪烁。

最新更新