Form Gradient Background

  • Thread starter Thread starter Jay Pondy
  • Start date Start date
J

Jay Pondy

Is the code below the proper way to achieve a LinearGradient
Background on a form?


Private Sub MyForm_Paint(ByVal sender As Object, ByVal e As
PaintEventArgs) Handles Me.Paint

Dim oBrush As New LinearGradientBrush(Me.ClientRectangle,
Color.AliceBlue, Color.CornflowerBlue, LinearGradientMode.Vertical)

e.Graphics.FillRectangle(oBrush, Me.ClientRectangle)
oBrush.Dispose()

end sub


The reason I ask is because it looks like the Paint event is fired one
time for each control on the form.
 
Instead of a Paint event handler, I'd do:
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
MyBase.OnPaintBackground(e)
' Insert your code here
End Sub

It doesn't seem to get called as often as OnPaint does. However there are a
few problems you'll run into with this method and your code below: first and
foremost, you'll get an exception if you minimize then restore your form.
You'll need additional code to protect against that. Also, if the form gets
resized, the gradient doesn't get re-drawn unless you also override OnResize
(or OnResizeEnd if you want fewer screen re-draws) with a call to Me.Refresh
or something similar.
 
It's possible the parent PaintBackground will be invoked for each control if
you've made the controls transparent. Transparent controls actually use a
transform to offset the graphics to correspond with the window rectangle of
the parent form and then call the parents draw routines directly...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Thanks - that explains why the event is occurring so often - my
controls DO have a transparent background.

How can I tell the difference between when the Forms background paint
is called and the controls? I've tried using "if type of sender is
form" but it is always the Form.

What is happening is that the transparent controls backgrounds are
also being drawn with their own gradient background so I'm curious as
to how to tell them apart.
 
There is no effective way to tell them apart. The transparency mechanism is
a bit of a pig in that respect.

The sender will be the form because the control just manipulates the
graphics object and tells the form to paint it's background...ergo it's
always the form that calls the method.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top