Want to add a little more visual impact to your application? How about adding an appealing gradient backdrop to your forms and in just a few lines of code?
That's exactly what this next snippet of code does for you. It accepts top and bottom gradient colors as arguments, then defines the brush and area to paint and displays the effect. Here's the code you'll need to put behind your form:
Private Sub DrawFormGradient( _ ByVal TopColor As Color, _ ByVal BottomColor As Color) ' Draws a gradient using the specified colors ' on the entire page Dim objBrush As New Drawing2D.LinearGradientBrush _ (Me.DisplayRectangle, _ TopColor, _ BottomColor, _ Drawing2D.LinearGradientMode.Vertical) Dim objGraphics As Graphics = _ Me.CreateGraphics() objGraphics.FillRectangle(objBrush, _ Me.DisplayRectangle) objBrush.Dispose() objGraphics.Dispose() End Sub
Next, you need to call this method—typically in response to the Paint event of your Form, like this:
Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles MyBase.Paint DrawFormGradient(Color.Blue, Color.AliceBlue) End Sub
Here, we're running our code each time that our form is "painted" (that is, "drawn" on the computer screen). It then paints our gradient: a rich blue mixing into a lighter blue. Of course, if that's too bold, you may opt for the more subtle White blending into PapayaWhip. Or the mysterious Black merging into DarkOrchid. Or the XP-styled White into CornflowerBlue. But the coloring is, of course, up to you.
Two quick tips: firstly, with a little editing, you can change the brush from painting Vertical to Horizontal, ForwardDiagonal, or BackwardDiagonal. And secondly, in the interest of the public health, don't overuse gradients. It could be scary.
Figure: My blue to alice blue form!
About the Author
Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.
# # #