开发者

C# - Rectangle animation flickers

开发者 https://www.devze.com 2023-04-12 03:07 出处:网络
I\'m trying to create simple animation of rectangle. Animation is very simple rectangle has st开发者_开发问答arting size 1 x 400 px, and using Timer I\'m incrementing 4px to its width every 25 ms. But

I'm trying to create simple animation of rectangle. Animation is very simple rectangle has st开发者_开发问答arting size 1 x 400 px, and using Timer I'm incrementing 4px to its width every 25 ms. But animation flickers I set Form to double buffered but it doesnt help at all. It seem I have to set this proprety to rectangle itself but there is no double buffered property in rectangle class :(. Is there a way around it ? or entirely different approach maybe to do this simple animation ? thanks in advance

Form code:

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();
        }
    }
}

Designer code:

    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);

    }


Usually, you'd switch on double buffering, however it seems that this might not be possible: @Hans Passant offers this concerning PowerPacks.Shape

It's fairly flawed. It uses its own window that's overlaid onto the form with the WS_EX_TRANSPARENT style turned on. That style makes it invisible, but also prevents any kind of double-buffering from working properly. Double-buffering the form has no effect, wrong window.

It is otherwise a rather expensive way to draw shapes. The cheap and flicker-free way is using e.Graphics.FillRectangle() in the form's OnPaint() override or Paint event handler.

0

精彩评论

暂无评论...
验证码 换一张
取 消