Y'herd thisun? 

“Want to hear the last-ever space shuttle landing sonic booms echoing across the Los Angeles basin? It's neat!”

from this av page by smith

Easy gradient forms

TaggedCoding, CSharp, VB

Companies know that an app can have all the best functionality but if it doesn't look spiffy it won't sell. Offer users an upgrade that has no difference but pop-up flat buttons and they'll pay the price.

Time was when you wanted to get a gradient background on a form you had to do a lot of API work, and in VB the redraws were never quite quick enough. Plus the complexity of dealing with areas that needed to be repainted vs areas that didn't and setting a forms' controls to match the form gradient of their location led most of us to go for custom controls.

Times have changed.

Create a new form, add a label (name it "lblTest" and give it a Text value), add a button (name it "butClickMe" and give it a Text value). Doubleclick on the form to bring up code view. Now, navigate to the Paint event and drop in this code:

Dim GradientMode As LinearGradientMode = _
Dim r As New Rectangle(0, 0, Me.Width, Me.Height)
Dim br As New LinearGradientBrush(r,  Color.WhiteSmoke, _
Color.DarkGray, GradientMode)
e.Graphics.FillRectangle(br, 0, 0, Me.Width, Me.Height)
lblTest.BackColor = Color.Transparent
butClickMe.BackColor = Color.Transparent

Now run the project. Not bad, eh? Only took us 15 years to easily imitate the NeXt look <g>

Experiment with the different styles in the LinearGradientMode enumeration and with different starting and ending colors. Of course, for controls that you don't want transparent (such as input text boxes), just don't set the backcolor in this routine.

You will find issues with transparency on some anchored controls on resizable forms, and note that child controls of panels and groupboxes won't go transparent, For the latter issue there is a visual workaround: draw your "children" on top of a Label control. It looks like a group box as you can see in the example below (the Tables and Columns areas are just labels and the other controls are z-ordered above them). You lose the "grouping" because the other controls are not really children of the label, but if you're just wanting a visual grouping then its' ok.

Hope it helps!

Robert Smith
Kirkland, WA

home     who is smith    contact smith     rss feed π
Since 1997 a place for my stuff, and it if helps you too then all the better