【问题标题】:C# - Rectangle animation flickersC# - 矩形动画闪烁
【发布时间】:2011-10-10 19:36:30
【问题描述】:

我正在尝试创建简单的矩形动画。动画是非常简单的矩形,起始大小为 1 x 400 像素,使用 Timer 我每 25 毫秒将其宽度增加 4 像素。但是动画闪烁我将 Form 设置为双缓冲,但它根本没有帮助。看来我必须将此属性设置为矩形本身,但矩形类中没有双缓冲属性:(。有没有办法解决它?或者完全不同的方法可能做这个简单的动画?提前谢谢

表格代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        animation_timer.Start();
    }

    private void animation_timer_Tick(object sender, EventArgs e)
    {
        rect.Width+=4;
        if (rect.Width > 778)
        {
            animation_timer.Stop();
        }
    }
}

设计师代码:

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.shapeContainer1 = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
        this.rect = new Microsoft.VisualBasic.PowerPacks.RectangleShape();
        this.animation_timer = new System.Windows.Forms.Timer(this.components);
        this.SuspendLayout();
        // 
        // shapeContainer1
        // 
        this.shapeContainer1.Location = new System.Drawing.Point(0, 0);
        this.shapeContainer1.Margin = new System.Windows.Forms.Padding(0);
        this.shapeContainer1.Name = "shapeContainer1";
        this.shapeContainer1.Shapes.AddRange(new   

        Microsoft.VisualBasic.PowerPacks.Shape[] {
        this.rect});
        this.shapeContainer1.Size = new System.Drawing.Size(784, 562);
        this.shapeContainer1.TabIndex = 0;
        this.shapeContainer1.TabStop = false;
        // 
        // rect
        // 
        this.rect.FillColor = System.Drawing.Color.Black;
        this.rect.FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;
        this.rect.Location = new System.Drawing.Point(5, 66);
        this.rect.Name = "rect";
        this.rect.Size = new System.Drawing.Size(1, 400);
        // 
        // animation_timer
        // 
        this.animation_timer.Interval = 25;
        this.animation_timer.Tick += new       
        System.EventHandler(this.animation_timer_Tick);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(784, 562);
        this.Controls.Add(this.shapeContainer1);
        this.DoubleBuffered = true;
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

【问题讨论】:

标签: c# winforms animation shapes


【解决方案1】:

通常,您会打开双缓冲,但似乎这不可能:@Hans Passant 提供 this concerning PowerPacks.Shape

这是相当有缺陷的。它使用自己的窗口,该窗口覆盖在打开了 WS_EX_TRANSPARENT 样式的表单上。这种风格使它不可见,但也阻止了任何类型的双缓冲正常工作。双缓冲表单无效,窗口错误。

否则这是一种相当昂贵的绘制形状的方法。廉价且无闪烁的方法是在表单的 OnPaint() 覆盖或 Paint 事件处理程序中使用 e.Graphics.FillRectangle()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 2011-12-02
    • 2012-03-12
    • 2011-03-30
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多