Color Gradients on Buttons

Hello.

I am wondering if there is any way to put two different colors on a single button control Similar to the "fill effects" in the Microsoft Office Suite - I would like to put one color on the top of the control and one on the bottom and have it form a gradient from one color to the next. There is somewhat of a gradient on the button when you put it on, but the gradient is not apparent when "running" the program.

Also, is there any way to change the gradient slopes Once again - similar to the Office Suite, I would like to be able to have the gradient be horizontal, vertical, diagonal, etc.

Thank you very much.




Answer this question

Color Gradients on Buttons

  • woodpecker0127

    hi,

    i don't know about office suite but i was searching for a way to do that as well all what i found it was this article. you have to build a new user control and to inherit button class and to change the colors to gradient colors by useing brush something like this


    Public Class ColoredButton
    Inherits System.Windows.Forms.Button
    Private m_color1 As Color = Color.LightGreen
    ' first color

    Private m_color2 As Color = Color.DarkBlue
    ' second color

    Private m_color1Transparent As Integer = 64
    ' transparency degree

    ' (applies to the 1st color)

    Private m_color2Transparent As Integer = 64
    Public Sub New()
    MyBase.New()
    InitializeComponent()
    End Sub

    ' transparency degree

    ' (applies to the 2nd color)

    Public Property cuteColor1() As Color
    Get

    Return m_color1
    End Get

    Set(ByVal value As Color)
    m_color1 = value
    Invalidate()
    End Set

    End Property

    Public Property cuteColor2() As Color
    Get

    Return m_color2
    End Get

    Set(ByVal value As Color)
    m_color2 = value
    Invalidate()
    End Set

    End Property

    Public Property cuteTransparent1() As Integer

    Get

    Return m_color1Transparent
    End Get

    Set(ByVal value As Integer)
    m_color1Transparent = value
    Invalidate()
    End Set

    End Property

    Public Property cuteTransparent2() As Integer

    Get

    Return m_color2Transparent
    End Get

    Set(ByVal value As Integer)
    m_color2Transparent = value
    Invalidate()
    End Set

    End Property

    Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
    ' Calling the base class OnPaint

    MyBase.OnPaint(pe)
    ' Create two semi-transparent colors

    Dim c1 As Color = Color.FromArgb(m_color1Transparent, m_color1)
    Dim c2 As Color = Color.FromArgb(m_color2Transparent, m_color2)
    Dim b As Brush = New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10)
    pe.Graphics.FillRectangle(b, ClientRectangle)
    b.Dispose()
    End Sub

    End Class


    it will require you to change the inherited class from the designer.vb and to remove AutoScaleMode from there. the way you want to draw the button depend on the brush that you will use

    Hope this Helps



  • asilter

    Another option would be to create a bitmap image with your gradient and assign it to the Image property of the button.

    Hope this helps,

    Steve Hoag

    Visual Basic Express



  • Simon Mourier

    Thank you very much.

    I will go ahead and play around with this code and see what I can get it to do. I'll browse through all of the code and see how they make the color changes.

    Also, I never thought about creating a bitmap image. I'll try that as well.

    Much appreciated guys.

    Thank you very much.



  • Color Gradients on Buttons