OnPaint override causing graphics to leave artifacts when resized

B

Brian Henry

I am trying to draw a rectangle that sizes to the size of a user control...
which the user control is docked to the form... when i resize the form it
redraws but the old lines are still left on the screen makeing garbage
images and lines... how do you get the OnPaint event to draw correctly? this
is my code right now for it
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)



mybase.OnPaint(e)

e.Graphics.FillRectangle(New SolidBrush(Color.Red), Me.Top, Me.Left,
Me.Width, Me.Height)

e.Graphics.DrawRectangle(New Pen(Color.Gray), Me.Top, Me.Left, Me.Width -
10, 40)


End Sub



i tried moveing the base class call from the top to the bottom that made no
diffrence.. its a similar effect to drawing a line but not eraseing the
previous line while draging it... leaves the old lines in place.. thanks
 
B

Brian Henry

i added this
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)

Invalidate()



End Sub



and it works ok i think, is this correct? thanks
 
J

Jay B. Harlow [MVP - Outlook]

Brian,
That's one way, you can also use the following in the constructor:

' Stop the flicker
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()

The ControlStyles.ResizeRedraw specifically handles the resizing, while the
other three handle "flickering".

Hope this helps
Jay
 
B

Brian Henry

even better, thanks!


Jay B. Harlow said:
Brian,
That's one way, you can also use the following in the constructor:

' Stop the flicker
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()

The ControlStyles.ResizeRedraw specifically handles the resizing, while the
other three handle "flickering".

Hope this helps
Jay
 

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

Similar Threads

Custom Paint & disposing 5
Custom form border 8
Panel Bordercolor 2

Top