Owner Drawn form background problems

K

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
 
K

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.
 
A

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.
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
 
A

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.
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(VS.85).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
 
K

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
 

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