GDI Question

J

Joel Whitehouse

Hey guys,

I have a quick question:

I am trying to draw a border around a control with GDI to highlight it.
Here is my simple test code added onto a new Usercontrol to make it's
border turn off and on with a user's click:

Public Class UserControl1
Inherits System.Windows.Forms.UserControl

Private _draw as Boolean
Private _pen as System.Drawing.Pen

Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Instantiate pen
_pen = New System.Drawing.Pen(System.Drawing.Color.Red, 4)
End Sub

Private Sub UserControl1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Click

'Invert the draw flag
_draw = Not _draw

'Redraw form
MyBase.Refresh()
End Sub

Protected Overreides Sub OnPaint(ByVal e as PaintEventArgs)

Dim g As Graphics = e.Graphics

If (_draw) Then
g.Clear(Me.BackColor)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
g.DrawRectangle(_pen, MyBase.DisplayRectangle)
g.Fluch(Drawing2d.FlushIntention.Sync)
End If

End Sub
End Class

The problem? The toggling only works if I click the control and then
drag it off the viewable area of the screen, and then drag it back onto
the viewable area again. How do I force the rectangle to auto-redraw?

Thanks!

-Joel
 
J

Joel Whitehouse

Ken Tucker [MVP] wrote:

Invalidate the control in the click event to get it to redraw.

This helps the regularity of the transition a lot, but I have one more
question: the rectangle that gets rendered is often choppy and
irregular. The lines are of inconsistent width, almost as if they were
splintered... What can I do to make sure that the whole rectangle gets
drawn?

Thanks again!

-Joel
 
P

Peter Proost

Hi,

if I leave out the smoothingmode and draw the border like this, then the
border isn't choppy or irregular

If (_draw) Then
g.Clear(Me.BackColor)
g.DrawRectangle(_pen, 0, 0, Me.Width, Me.Height)
g.Flush(Drawing2D.FlushIntention.Sync)
End If

Hth Greetz Peter
 
J

Joel Whitehouse

Peter said:
if I leave out the smoothingmode and draw the border like this, then the
border isn't choppy or irregular

If (_draw) Then
g.Clear(Me.BackColor)
g.DrawRectangle(_pen, 0, 0, Me.Width, Me.Height)
g.Flush(Drawing2D.FlushIntention.Sync)
End If

Hth Greetz Peter

Thanks Ken and Peter! When I took out the smoothingmode line from the
drawing code, used an inalidation in the flag changing code, and removed
the MyBase.Refresh() line, everything worked as I expected.

-Joel

P.S. I understand why the control invalidaiton is necessary, but why
would the smoothingmode line cause so much trouble?
 

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