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