Label - Paint background problem...

AlexCode

Senior Contributor
Joined
Jul 7, 2003
Messages
931
Location
Portugal
Hi!
I have a set of inherited Windows.Forms controls that I have anhanced.
One of those enhancements is the possibility of display a gradient background.

No problem with the painting, the problem is that, for example, on the Label control, the background doesn't refresh if another window passes over it or even if the control is resized...

My painting code is:
Visual Basic:
    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(pevent)

        If (Not _BackColor2.IsEmpty) OrElse (Not FillType = Core.Enums.eFillType.OneColor_A) Then
            Dim gr1, gr2 As System.Drawing.Drawing2D.LinearGradientBrush

            Select Case FillType
                Case Core.Enums.eFillType.Vertical_AB
                    gr1 = New System.Drawing.Drawing2D.LinearGradientBrush( _
                        New Point(0, 0), New Point(0, pevent.ClipRectangle.Height), _
                        Me.BackColor, Me.BackColor2)

                    pevent.Graphics.FillRectangle(gr1, pevent.ClipRectangle)
                'Case Core.Enums.eFillType.Vertical_ABA
                '     ... all other gradient drawing is similar ...
            End Select

        Else
            MyBase.OnPaintBackground(pevent)
        End If

    End Sub

Note that:
1- The text is allways well drawn (so the Paint event is called properlly)
2- BackColor2 is a property representing the 2nd gradient color
3- Core.Enums.eFillType.Vertical_AB and all the similar are part of an Enumeration that represents the kind of gradient painting, ie. Vertical_AB represents a vertical gradient from color A to color B.

Thanks!

Alex
 
Is it redrawing the gradient but incorrectly? If so it is probably because you are using the clip rectangle to calculate the gradient as well as which part to redraw. Try changing the code like so

Visual Basic:
gr1 = New System.Drawing.Drawing2D.LinearGradientBrush( _
                        New Point(0, 0), New Point(0, pevent.ClipRectangle.Height), _
                        Me.BackColor, Me.BackColor2)
'change to
 gr1 = New System.Drawing.Drawing2D.LinearGradientBrush( _
            New Point(0, 0), New Point(Me.Height, Me.Width), _
                Me.BackColor, Me._BackColor2)
and see if that helps.
 
Damn stupid thing...
The clip rectangle only represents the invalidated area right?

Damn...

Thanks... it works perfectly now...

Alex :p
 
Back
Top