Windows Forms 2.0-Draw Beautiful Gradient Backdrops

17 Nov

Tired with the usual gray in windows forms? Yeah, me too! With .Net you can easily create visually beautiful color gradients on your windows forms, and with very minimum code.

Here’s a piece of code that does just that for you. This method takes the top color, bottom color and the object that called the method, it then creates a brush and an area to paint and then draws a gradient on your object which could be a form, a panel etc with a mix of the colors you selected.

Public Sub DrawGradient(ByVal UpperColor As Color, ByVal LowerColor As Color, _
                        ByVal Caller As Object)

        Dim cur As Control = CType(Caller, Control)

        Dim objBrush As New Drawing2D.LinearGradientBrush _
          (cur.DisplayRectangle, UpperColor, LowerColor, Drawing2D.LinearGradientMode.Vertical)
        Dim objGraphics As Graphics = cur.CreateGraphics()
        objGraphics.FillRectangle(objBrush, cur.DisplayRectangle)
        objBrush.Dispose()
        objGraphics.Dispose()
    End Sub

The gradient will be vertical, i.e top to bottom, but you can change the LinearGradientMode option to horizontal or diagonal depending on your choice of style.

To call this method, you use the Paint event of a form or a control. As you can see on the code above, I used the DisplayRectangle method of a control to make the code more generic, otherwise, you could use a form, or another control depending on your needs.

I used this code Dim cur As Control = CType(Caller, Control). This way you can call it from a form, a panel control, a button etc. without changing the code.
But if you are going to call it from just forms, then you could change it to this
Dim cur As Form = CType(Caller, Form).

Implementation:

Its easy. Call the method from the object’s Paint event, providing the colors you want to use. I created a simple form called GradientsSample. Here is my implementation on the form’s Paint event.

Private Sub GradientsSample_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        DrawGradient(Color.CornflowerBlue, Color.White, sender)
    End Sub

I used the XP style mix of CornflowerBlue and White. See how the form looks like.

xp-style-gradient

See, just beautiful.

I then added a panel to the left side of the form, say you want to use it as your navigation menu. This is how the forms looks like now.

xpstyle_panel

So you see the panel on the left now looks gray and ugly. The form paint event only paints the form and not the controls on the form. Which means we have to use our method to paint the panel control. I added this code to the panel’s Paint event.

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        DrawGradient(Color.DarkSeaGreen, Color.White, sender)
    End Sub

For the panel I used the DarkSeaGreen and White colors. The output is just beautiful, as you can see below.
panel_gradient

It is really easy to create gradients and make your forms and controls beautiful, instead of using the usual gray that is common and boring.
Till next time, yours truly.

About these ads

5 Responses to “Windows Forms 2.0-Draw Beautiful Gradient Backdrops”

  1. Gary Greenacre March 4, 2009 at 4:53 pm #

    I implemented your example in a test project. Have you noticed what happens if you drag the form beyond the edge of the display? Drag it back into full view and the part of the form that was off the screen will have its original background color. I wonder why that happens. I will probably look around for an event to monitor and draw the gradient again when moving the form happens.

    I’m also interested in dealing with background colors of controls such as radio buttons. Their default ugly “control” color really stands out in a bad way against the form. But drawing that same gradient (at least the colors I picked) is not a good solution. Perhaps you have some input about that.

    Thanks for the article.

  2. http://www.youtube.com/watch?v=caSEY6ykNKw April 10, 2013 at 3:41 am #

    You need to be a part of a contest for one of the finest sites on the net.

    I’m going to highly recommend this blog!

  3. what smells attract women June 12, 2013 at 7:01 am #

    I had the early morning sickness however it by no means arrived
    up. Even though I keep in mind sitting over the bathroom wishing
    it might just come up cuz it got so bad but Thessaly only lasted possibly two weeks.

    And my bf produced me consider six exams. All good.
    But I have not been towards the medical professional given that February so I’m way previous on account of go. I’m now 15 weeks and
    Tuesday I will be 16 so I’m a little anxious cuz I have not got to do all of the blood work or anything that was due last thirty day period.

  4. Josefa July 27, 2013 at 2:40 am #

    Let us get this entering into Hickory, NC

  5. The only thing you have to do is to utilize this laser leveler tool which
    will project a visible laser-cross on the walls by simultaneously, displaying accurate horizontal and vertical lines within seconds.
    Many hand tools are automatic and are generally powered by electricity.
    These craftsmen have the knowledge, tools and time to ensure your home.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: