Windows Develop Bookmark and Share   
 index > Windows Forms General > How to keep a form from redrawing when i use graphics
 

How to keep a form from redrawing when i use graphics

i have a form that keeps redrawing(its a clock). Is there anyway that i can keep it from clearing. CanI create an image from it--like the background image property of the form?
kevdog114  Wednesday, April 23, 2008 11:09 PM
All your drawing should be done in the Paint event of the form (which provides the Graphics object you need). If you need to create your own Graphics object, you are probably doing it wrong.
wjousts  Thursday, April 24, 2008 12:55 AM
hi, if you can post some code will be alot of help! it could be in code that your calling g.redraw() method?

and i thought that the graphics object is constantly redrawing?? blitting to the screen or any action is taken??

cheers
Anonymousaaaaaaaaaaaaaaaahhhh  Thursday, April 24, 2008 12:00 AM

Well, my first problem is that i would prefer that the background color changes in addition to the drawn shapes over them.

I use the form graphics to draw the shapes and the background color is set by a timer. But every time the timer ticks, the shapes that i drew(via the graphics) are erased.

The next problem that i am having is the actual clock. I have 3 line controls for the hour, minute, and second hands, But as i set their x and y endpoints, the background image(again drawn with the graphics) is erased

I tried the "drawToBitmap" but that didn't seem to work

note: the drawHour code is called after the form is maximized in the form_load event

Code Snippet

Public Sub DrawHour(ByVal t As DateTime, ByVal rect As Rectangle)

'cos = x

'sin = y

Me.DrawToBitmap(image2, Me.Bounds)

h = ((t.Hour * 30) - 90)

Dim radians As Double = h * Math.PI / 180

Dim cos As Double = Math.Cos(radians)

Dim radians1 As Double = h * Math.PI / 180

Dim sin As Double = Math.Sin(radians1)

cos = cos * (rect.Width / 2)

sin = sin * (rect.Width / 2)

Dim graph As Graphics = Me.CreateGraphics

LnHour.X1 = (Me.Width / 2)

LnHour.Y1 = (Me.Height / 2)

LnHour.X2 = cos + (Me.Width / 2)

LnHour.Y2 = sin + (Me.Height / 2)

Me.BackgroundImage = image2

End Sub

Public Sub DrawClock()

Dim lin As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), Color.DarkBlue, Color.MediumSpringGreen, 35)

Dim rect As New Rectangle

If Me.Width > Me.Height Then

rect.Width = Me.Height

rect.Height = Me.Height

Else : rect.Width = Me.Width

rect.Height = Me.Width

End If

Dim graph As Graphics = Me.CreateGraphics

rect.X = (Me.Width / 2) - (rect.Width / 2)

rect.Y = (Me.Height / 2) - (rect.Height / 2)

'Dim Graph1 As Graphics = pbClock.CreateGraphics

graph.FillRectangle(lin, New Rectangle(0, 0, Me.Width, Me.Height))

Dim lin2 As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.Red, Color.Goldenrod, 45)

graph.FillEllipse(lin2, rect)

Dim image01 As Image

image01 = Me.BackgroundImage

Me.DrawToBitmap(Me.BackgroundImage, Me.Bounds)

'every 30 degrees

'cos = x

'sin = y

Dim pt As New List(Of Point)

n = 30

Do While n <= 360

Dim radians As Double = n * Math.PI / 180

Dim cos As Double = Math.Cos(radians)

Dim radians1 As Double = n * Math.PI / 180

Dim sin As Double = Math.Sin(radians1)

x = cos * ((rect.Width / 2) - 25)

y = sin * ((rect.Width / 2) - 25)

x = x + (Me.Width / 2)

y = y + (Me.Height / 2)

n = n + 30

pt.Add(New Point(x, y))

Loop

Dim txt As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White, Color.Black, Drawing2D.LinearGradientMode.ForwardDiagonal)

Dim pt1 As New Point

Dim pt2 As New SizeF

Dim ct As Integer = 4

For i = 1 To pt.Count

pt1 = pt.Item(i - 1)

pt2 = (graph.MeasureString(ct, Me.Font))

pt1.X = pt1.X - (pt2.Width / 2)

pt1.Y = pt1.Y - (pt2.Height / 2)

graph.DrawString(ct, Me.Font, txt, pt1)

ct = ct + 1

If ct = 13 Then

ct = 1

End If

Next

'pbClock.BringToFront()

image2 = Me.BackgroundImage

Dim date1 As DateTime = Now

DrawHour(date1, rect)

End Sub

kevdog114  Thursday, April 24, 2008 12:51 AM
All your drawing should be done in the Paint event of the form (which provides the Graphics object you need). If you need to create your own Graphics object, you are probably doing it wrong.
wjousts  Thursday, April 24, 2008 12:55 AM

You can use google to search for other answers

Custom Search

More Threads

• LinkLabel and Locking Workstation
• Control Draw Boundaries
• Can't stop the beep when pressing enter
• XML resource is not copying to the "Resources" folder on deployment.
• Printing a big image in multiple pages
• Multithreaded application behaving abnormal.
• Custom controls not keeping state
• ComboBox DroppedDown
• How can i play all video filles ie wmv, avi, ect: from a specified folder?
• Programmatically Clicking a Treenode