C# - 矩形动画闪烁

标签 c# winforms animation shapes

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

表单代码:

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

    }

最佳答案

通常,您会打开双缓冲,但是这似乎不可能:@Hans Passant报价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.

关于C# - 矩形动画闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7711501/

相关文章:

c# - 验证文本框只允许小数

winforms - 如何实现ScintillaNET列编辑模式

php - 有没有办法检测浏览器是否支持 CSS3 动画?

c# - 陷入 C# 中循环 XML 的思路中

c# - 检查用户名和密码的类型是用于客户端还是管理员

c# - XDocument 通过其名称属性的值获取 XML 元素

c# - 单实例 Windows 窗体应用程序以及如何获取对它的引用?

android - Android中的自定义动画

html - 为什么我的 css @keyframe 动画不工作?

c# - 如何通过 ConfigurationManager 写入 User.Config 文件?