all groups > dotnet windows forms > january 2006 >
You're in the

dotnet windows forms

group:

Form Gradient Background


Form Gradient Background Jay Pondy
1/31/2006 2:00:55 PM
dotnet windows forms:
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.
RE: Form Gradient Background Vark
1/31/2006 10:36:27 PM
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.

[quoted text, click to view]
Re: Form Gradient Background Bob Powell [MVP]
2/1/2006 7:12:28 PM
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.



[quoted text, click to view]

Re: Form Gradient Background Jay Pondy
2/2/2006 6:56:57 AM
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.



On Wed, 1 Feb 2006 19:12:28 +0100, "Bob Powell [MVP]"
[quoted text, click to view]
Re: Form Gradient Background Bob Powell [MVP]
2/2/2006 7:37:45 PM
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.



[quoted text, click to view]

Re: Form Gradient Background Jay Pondy
2/3/2006 6:51:23 AM
Thanks for your help Bob.


On Thu, 2 Feb 2006 19:37:45 +0100, "Bob Powell [MVP]"
[quoted text, click to view]
AddThis Social Bookmark Button