C# - 矩形动画闪烁

发布于 2024-12-09 10:07:42 字数 2451 浏览 0 评论 0原文

我正在尝试创建简单的矩形动画。动画是非常简单的矩形,起始大小为 1 x 400 px,使用计时器,我每 25 毫秒将其宽度增加 4px。但是动画闪烁我将 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);

    }

I'm trying to create simple animation of rectangle. Animation is very simple rectangle has starting 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);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-12-16 10:07:42

通常,您会打开双缓冲,但是这似乎是不可能的:@Hans Passant 提供<一href="https://stackoverflow.com/questions/6585973/net-powerpacks-rectangleshape-flickers-on-form-resize/6586277#6586277">这涉及 PowerPacks.Shape

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文