2011-10-10 183 views
2

我正在尝试创建矩形的简单动画。动画非常简单,矩形的起始大小为1 x 400像素,并且使用定时器,我每25毫秒将宽度增加4px。但动画片闪烁我设置窗体双缓冲,但它根本没有帮助。它似乎我必须将此属性设置为矩形本身,但在矩形类中没有双缓冲属性:(。是否有解决方法?或者完全不同的方法也许可以执行此简单动画?提前致谢C# - 矩形动画闪烁

表单代码:

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

    } 
+1

也许这会帮助吗? http://stackoverflow.com/questions/64272/how-to-eliminate-flicker-in-windows-forms-custom-control-when-滚动 – wiero

+0

我不知道你的项目的要求是什么。也许你可以主持一个WPF用户控件。 –

回答

2

通常情况下,你会在双缓冲切换,不过似乎这也许是不可能的:@Hans Passant提供this concerning PowerPacks.Shape

这是相当瑕疵。它使用自己的窗口覆盖窗体并打开WS_EX_TRANSPARENT样式。这种风格使其看不见,但也阻止任何双缓冲工作正常。双缓冲窗体无效,错误的窗口。

这是一种非常昂贵的绘制形状的方法。便宜且无闪烁的方法是在窗体的OnPaint()覆盖或Paint事件处理程序中使用e.Graphics.FillRectangle()。

+1

好的答案,+1 :) –