How to Add a Gradient Background to a Win Form with VB.NET & VB2005
description Article and source code which demonstrate how to add a gradient background to a Windows Form with VB.NET and Visual Basic 2005.
get resource -> How to Add a Gradient Background to a Win Form VB.NET
How to Add a Gradient Background to a Win Form with VB2005
environment Visual Studio 2002, Visual Studio 2003, Visual Studio 2005
language VB.NET, Visual Basic 2005
namespace System.Drawing.Drawing2D
tags gradient, paint, optimize, windows, form, visual basic, vb, visual studio

This article and two downloadable Visual Studio Visual Basic solutions provide code that overrides the Form OnPaint event to create beautiful gradient backgrounds for a Windows Form. Also included is code that optimizes form drawing.

Form With Image and Gradient BackGround

First, the code imports:

Imports System.Drawing.Drawing2D

Next, painting is optimized in the form's load handler:

 

Private Sub DemoOneForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles MyBase.Load

 

    ' Make this form a MDI child of MainForm.

    Me.MdiParent = My.Forms.MainForm

 

    ' Optimize Painting.

    SetStyle(ControlStyles.AllPaintingInWmPaint Or _

                ControlStyles.DoubleBuffer Or _

                ControlStyles.ResizeRedraw Or _

                ControlStyles.UserPaint, _

                True)

End Sub

Finally, the Form's OnPaint method is overridden to paint the gradient background.

' Override the OnPaint event for this form.

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

    ' Declare a variable of type Graphics named formGraphics.

    ' Assign the address (reference) of this forms Graphics object

    ' to the formGraphics variable.

    Dim formGraphics As Graphics = e.Graphics

    ' Declare a variable of type LinearGradientBrush named gradientBrush.

    ' Use a LinearGradientBrush constructor to create a new LinearGradientBrush object.

    ' Assign the address (reference) of the new object

    ' to the gradientBrush variable.

    Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), Color.White, Color.DarkMagenta)

 

    ' Here are two more examples that create different gradients.

    ' Comment the Dim statement immediately above and uncomment one of these

    ' Dim statements to see how varying the two colors changes the gradient result.

    ' Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), Color.Chartreuse, Color.SteelBlue)

    ' Dim gradientBrush As New LinearGradientBrush(New Point(0, 0), New Point(Width, 0), Color.White, Color.SteelBlue)

 

    formGraphics.FillRectangle(gradientBrush, ClientRectangle)

End Sub

mike mcintyre http://www.getdotnetcode.com

 

 

 

 

 

 

 

 

 

 

 



Copyright © 2001-2007 aZ Software Developers. All rights reserved.

 

.