How to reduce the flicker?
I am a beginner in learning c#,I want to make a simple animation. I do it in this way:paint the background ,draw a circle;repaint the background ,draw another circle.....
but the screen looks so flickering.How can i reduce the flicker?Someone told me to use "Double Buffering",but I don't konw how to use it.Could you give me an example or modify my code for me ?
Thanks!!!
My code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace WindowsApplication7
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
Pen pen=new Pen(Color.Red,5);
int i=0;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e1)
{
Graphics g=e1.Graphics;
e1.Graphics.SmoothingMode=SmoothingMode.AntiAlias;
Brush bgBrush=new SolidBrush(Color.Green);
g.FillRectangle(bgBrush,ClientRectangle);
//Pen pen=new Pen(Color.Red,5);
g.DrawEllipse(pen,100,150,50,50);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 10;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(624, 341);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void timer1_Tick(object sender, System.EventArgs e)
{
//MessageBox.Show("SDF");
Graphics g=this.CreateGraphics();
Brush bgBrush=new SolidBrush(Color.Green);
g.FillRectangle(bgBrush,ClientRectangle);
g.DrawEllipse(pen,i+=5,150,50,50);
String drawString = "Sample Text";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
float x = 150.0F;
float y = 50.0F;
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Draw string to screen.
g.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
if(i>this.Width)i=0;
}
}
}