Home All Groups Group Topic Archive Search About
Author
12 Jan 2006 9:36 PM
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

Author
12 Jan 2006 10:19 PM
Ken Tucker [MVP]
Hi,

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

Ken
--------------------
Show quoteHide quote
"Joel Whitehouse" <joelwhiteho***@hotmail.com> wrote in message
news:OIid4C8FGHA.1028@TK2MSFTNGP11.phx.gbl...
> 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
Author
12 Jan 2006 11:03 PM
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
Author
13 Jan 2006 10:02 AM
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Show quoteHide quote
"Joel Whitehouse" <joelwhiteho***@hotmail.com> schreef in bericht
news:Oktunz8FGHA.2704@TK2MSFTNGP15.phx.gbl...
> 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
Author
13 Jan 2006 3:57 PM
Joel Whitehouse
Peter Proost wrote:
> 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?