Trouble eliminating flicker

G

grumfish

I'm trying to write a simple Windows Forms based clock. Right now if
flickers pretty badly when updated both from the timer and resizing. I
thought the way I buffered the image before blitting would eliminate it
but it does not. What am I doing wrong?


import System
import System.Drawing
import System.Windows.Forms

class Clock(Form):
private timer as Timer
private buffer as Bitmap
private cx as int
private cy as int
private rad as int

def constructor():
OnResize(EventArgs())
InitializeControls()
timer = Timer()
timer.Tick += TimerEvent
timer.Interval = 200
timer.Start()

def TimerEvent():
DrawBuffer()
Refresh()

def InitializeControls():
self.Text = "Clock"

def OnResize(e as EventArgs):
super.OnResize(e)
self.buffer = Bitmap(ClientRectangle.Width, ClientRectangle.Height)
cx = ClientRectangle.Width / 2
cy = ClientRectangle.Height / 2
if ClientRectangle.Width < ClientRectangle.Height:
rad = ClientRectangle.Width * .45
else:
rad = ClientRectangle.Height * .45
DrawBuffer()
Refresh()

def PolarToRect(a as double, m as double):
pass

def DrawBuffer():
g = Graphics.FromImage(buffer)
g.Clear(Color.White)
rect = Rectangle(cx-rad, cy-rad, rad*2, rad*2)
g.DrawEllipse(Pen(Color.Black, 3), rect)

def OnPaint(pe as PaintEventArgs):
pe.Graphics.DrawImageUnscaled(buffer, 0, 0)

Application.Run(Clock())



I also get this exception when I minimize the form but I can't figure
out what is causing it.

System.ArgumentException: Invalid parameter used.
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height,
PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at clock.Clock.OnResize(EventArgs e) in
D:\Projects\sharpdevelop\clock\Main.boo:line 26
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32
width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
 
B

Bob Powell [MVP]

These are assumptions because I don't know enough about Boo to make a real
diagnosis but I would be disinclined to call the onsize from the constructor
because the window hasn't been created yet.

Use SetStyle to set up double buffering...

Don't paint the background...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top