So here is what I am trying to do.I am making a game using only S.W.F and S.D namespaces.When I use a timer with the 1000/30 interval(30 frames),in it’s tick event i have a call to InvokeGraphics().Everything renders more or less fine,except the fact that the ellipse is drawn flickred.I tried using double buffering,and this.SetStyle(),but both failed.Here is the code:
public partial class MainForm : Form
{
int x = 0;
public MainForm()
{
InitializeComponent();
var sz = SystemInformation.PrimaryMonitorSize;
this.FormBorderStyle = FormBorderStyle.None;
this.Size = sz;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
Timer tmr = new Timer();
tmr.Enabled = true;
tmr.Interval = 1000/30;
tmr.Tick += delegate(object sender, EventArgs e)
{
x++;
this.InvokePaint(this,new PaintEventArgs(this.CreateGraphics(),this.Bounds));
};
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if((int)e.KeyChar == 27) Application.Exit();
base.OnKeyPress(e);
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(Color.Firebrick);
// this ellipse flickrs
g.FillEllipse(Brushes.Green,x,64,64,64);
base.OnPaint(e);
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
}
}
Using this.CreateGraphics() does not create a double-buffered painting context. Set the form’s DoubleBuffered property to true. And use a Timer with an Interval of 32 msec to force a refresh, only call Invalidate() in its Tick event handler.