Home All Groups Group Topic Archive Search About

Owner Drawn form background problems

Author
8 Apr 2010 7:49 PM
kpg
vs 2005 vb.net windows form app.

Doing a simple gradient background on a form:

    Protected Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(e)

        Dim r As Rectangle = ClientRectangle
        Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush
(r, Color.Gray, Color.Black,
System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(b, ClientRectangle)

    End Sub


Problem:

On form resize the background is not painted properly.  When increasing the 
width the gradient is not re-drawn, it is just extended (the 2nd color is
painted in the new area exposed).  When increasing height the newly exposed
area is drwan properly but the old ard is not re-drawn.

The client rectangle used in the paint event covers the entire form, so why
is it not re-drawing the form?

Thanks,
kpg

Author
8 Apr 2010 8:05 PM
kpg
Private Sub frmMain_Resize(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Resize
        Me.Invalidate()
End Sub


If I had a nickle for everytime I answered my own questions, well,
I'd have a lot of nickles.
Author
8 Apr 2010 8:14 PM
Armin Zingler
Am 08.04.2010 21:49, schrieb kpg:
> vs 2005 vb.net windows form app.
>
> Doing a simple gradient background on a form:
>
>     Protected Overrides Sub OnPaintBackground(ByVal e As
> System.Windows.Forms.PaintEventArgs)
>
>         MyBase.OnPaint(e)

Why do you call OnPaint in OnPaintbackground? I'd remove the line.

Show quoteHide quote
>         Dim r As Rectangle = ClientRectangle
>         Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush
> (r, Color.Gray, Color.Black,
> System.Drawing.Drawing2D.LinearGradientMode.Vertical)
>         e.Graphics.FillRectangle(b, ClientRectangle)
>
>     End Sub
>
>
> Problem:
>
> On form resize the background is not painted properly.  When increasing the 
> width the gradient is not re-drawn, it is just extended (the 2nd color is
> painted in the new area exposed).  When increasing height the newly exposed
> area is drwan properly but the old ard is not re-drawn.
>
> The client rectangle used in the paint event covers the entire form, so why
> is it not re-drawing the form?

Because the old visible area is not part of the invalidated area of the
Form. Windows knows which area is invalid and drawing is clipped to
the invalid region only. To overcome this...:

   Sub New()

      ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
      InitializeComponent()

      ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
      SetStyle(ControlStyles.ResizeRedraw, True)
      UpdateStyles()


   End Sub

--
Armin
Author
8 Apr 2010 8:23 PM
Armin Zingler
Am 08.04.2010 22:14, schrieb Armin Zingler:
> [...]

Sry, accidently sent too early.

--
Armin
Author
8 Apr 2010 8:21 PM
Armin Zingler
Am 08.04.2010 21:49, schrieb kpg:
> vs 2005 vb.net windows form app.
>
> Doing a simple gradient background on a form:
>
>     Protected Overrides Sub OnPaintBackground(ByVal e As
> System.Windows.Forms.PaintEventArgs)
>
>         MyBase.OnPaint(e)

Why do you call OnPaint in OnPaintbackground? I'd remove the line.

Show quoteHide quote
>         Dim r As Rectangle = ClientRectangle
>         Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush
> (r, Color.Gray, Color.Black,
> System.Drawing.Drawing2D.LinearGradientMode.Vertical)
>         e.Graphics.FillRectangle(b, ClientRectangle)
>
>     End Sub
>
>
> Problem:
>
> On form resize the background is not painted properly.  When increasing the 
> width the gradient is not re-drawn, it is just extended (the 2nd color is
> painted in the new area exposed).  When increasing height the newly exposed
> area is drwan properly but the old ard is not re-drawn.
>
> The client rectangle used in the paint event covers the entire form, so why
> is it not re-drawing the form?

Because the old visible area is not part of the invalidated area of the
Form. Only the new part has to be redrawn. Windows knows which area is
invalid and drawing is clipped to the invalid region only.

http://msdn.microsoft.com/en-us/library/dd145135%28VS.85%29.aspx


To overcome this...:

   Sub New()

      ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
      InitializeComponent()

      ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
      SetStyle(ControlStyles.ResizeRedraw, True)
      UpdateStyles()


   End Sub

--
Armin
Author
8 Apr 2010 8:55 PM
kpg
>>         MyBase.OnPaint(e)
>
> Why do you call OnPaint in OnPaintbackground? I'd remove the line.

Only becuase the example I had did it.  I know it seems to work fine
without it, I thought it was there 'just in case'.

I'll remove it, however.

Thanks,
kpg