From: kpg on
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
From: kpg on

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.

From: Armin Zingler on
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.

> 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
From: Armin Zingler on
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.

> 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
From: Armin Zingler on
Am 08.04.2010 22:14, schrieb Armin Zingler:
> [...]

Sry, accidently sent too early.

--
Armin