Double Buffering

lxman

New member
Joined
Jun 21, 2007
Messages
1
Programming Experience
10+
I have been trying to design a simple analog clock control for VB.net using CF 2.0. I had everything drawing and reacting well by putting my drawing calculations and actions in the paint event. This worked out wonderfully, except, of course, for flickering. This is compounded by the fact that when I use this control, I plan on using 8 or ten of them on a single form, which causes extensive flickering "features." :rolleyes:

So, double buffering seems to be the solution. But I have not been able to achieve a functional control yet. Here's the latest version of my code for the control. The clock shows fine at design time, but at run time, all I get is a black box (which flickers badly by the way :eek:).

Any help is appreciated.

Public Class AnalogClock
Dim HourAngle, MinAngle As Double
Dim OffScrnBmp As System.Drawing.Bitmap

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Me.Height = Me.Width
MyBase.OnPaint(e)
Dim penwidth, halfpen, gap, outercircleradius, innercircleradius, mincircleradius, hourcircleradius As Integer
Dim angle1, angle2 As Double
Dim origin, endpoint, startpoint As Point
penwidth = 2
gap = penwidth * 5
halfpen = penwidth / 2
Dim mypen As New Pen(Color.Black, penwidth)
Dim minortickpen As New Pen(Color.Black, 1)
Dim OffScrnBmp As System.Drawing.Bitmap
Dim OffScrnDC As System.Drawing.Graphics
If OffScrnBmp Is Nothing Then
OffScrnBmp = New System.Drawing.Bitmap(Me.Width, Me.Height)
End If
OffScrnDC = System.Drawing.Graphics.FromImage(OffScrnBmp)

OffScrnDC.DrawEllipse(mypen, halfpen, halfpen, Me.Width - penwidth - 1, Me.Height - penwidth - 1)
origin.X = Me.Width / 2
origin.Y = Me.Height / 2
outercircleradius = Me.Width / 2
innercircleradius = outercircleradius * 0.8
mincircleradius = outercircleradius * 0.75
hourcircleradius = mincircleradius * 0.75
For angle1 = 0 To 2 * System.Math.PI Step System.Math.PI / 2
endpoint.X = origin.X + (System.Math.Sin(angle1) * outercircleradius * 0.95)
endpoint.Y = origin.Y + (System.Math.Cos(angle1) * outercircleradius * 0.95)
startpoint.X = origin.X + (System.Math.Sin(angle1) * innercircleradius)
startpoint.Y = origin.Y + (System.Math.Cos(angle1) * innercircleradius)
OffScrnDC.DrawLine(mypen, startpoint.X, startpoint.Y, endpoint.X, endpoint.Y)
angle2 = angle1 + (System.Math.PI / 6)
endpoint.X = origin.X + (System.Math.Sin(angle2) * outercircleradius * 0.95)
endpoint.Y = origin.Y + (System.Math.Cos(angle2) * outercircleradius * 0.95)
startpoint.X = origin.X + (System.Math.Sin(angle2) * innercircleradius)
startpoint.Y = origin.Y + (System.Math.Cos(angle2) * innercircleradius)
OffScrnDC.DrawLine(minortickpen, startpoint.X, startpoint.Y, endpoint.X, endpoint.Y)
angle2 = angle2 + (System.Math.PI / 6)
endpoint.X = origin.X + (System.Math.Sin(angle2) * outercircleradius * 0.95)
endpoint.Y = origin.Y + (System.Math.Cos(angle2) * outercircleradius * 0.95)
startpoint.X = origin.X + (System.Math.Sin(angle2) * innercircleradius)
startpoint.Y = origin.Y + (System.Math.Cos(angle2) * innercircleradius)
OffScrnDC.DrawLine(minortickpen, startpoint.X, startpoint.Y, endpoint.X, endpoint.Y)
Next angle1
'Now draw the hands
Dim MinHandPen As New Pen(Color.Black, 3)
Dim HourHandPen As New Pen(Color.Black, 4)
endpoint.X = origin.X + (System.Math.Sin(MinAngle) * mincircleradius)
endpoint.Y = origin.Y - (System.Math.Cos(MinAngle) * mincircleradius)
OffScrnDC.DrawLine(MinHandPen, origin.X, origin.Y, endpoint.X, endpoint.Y)
endpoint.X = origin.X + (System.Math.Sin(HourAngle) * hourcircleradius)
endpoint.Y = origin.Y - (System.Math.Cos(HourAngle) * hourcircleradius)
OffScrnDC.DrawLine(HourHandPen, origin.X, origin.Y, endpoint.X, endpoint.Y)
e.Graphics.DrawImage(OffScrnBmp, 0, 0)
If Not (mypen Is Nothing) Then mypen.Dispose()
If Not (minortickpen Is Nothing) Then minortickpen.Dispose()
'If Not (OffScrnBmp Is Nothing) Then OffScrnBmp.Dispose()
If Not (OffScrnDC Is Nothing) Then OffScrnDC.Dispose()
If Not (MinHandPen Is Nothing) Then MinHandPen.Dispose()
If Not (HourHandPen Is Nothing) Then HourHandPen.Dispose()
End Sub

Public Sub ShowTime(ByVal HourTime As Integer, ByVal MinTime As Integer)
'Here is the function we will call to draw the hands on the clock face
Dim tempangle As Double

tempangle = (HourTime / 6) * System.Math.PI
HourAngle = tempangle + (MinTime * System.Math.PI / 360)
MinAngle = (System.Math.PI / 30) * MinTime
Me.Invalidate()
End Sub

End Class
 
Back
Top