Click here to Skip to main content
Click here to Skip to main content

Bouncing Hover Button

, 3 Nov 2005
Rate this:
Please Sign up or sign in to vote.
The hover button for WinForms in VB.NET with pictures bouncing effect.

Sample Image

Introduction

This multifunctional button supports two different styles: Standard (standard VB.NET button) and BouncingHover. In the BouncingHover mode, the user can set normal and hover colors for the button's border, backcolor and forecolor as well as location of the text and the image. In this mode, the button's image (if it exists) and text are bouncing when the mouse is entering/leaving or clicking the control, while the disabled button's image and text get a colorless view. This control also supports three different types: Normal, Check and Alternative, that make the button work respectively as a normal Button, CheckBox and RadioButton. The user can also switch on beep and manage its duration and frequency.

Capabilities

  • One of two different styles can be selected (FlatStyle property).
  • One of three different types can be selected (ButtonType property). When ButtonType is equal to Alternative define a group of buttons (which work as RadioButtons) by setting the AlternativeGroup string value.
  • One of four different GradientMode can be selected (GradientMode property).
  • The start and the end color of the button (gradient colors) for normal state (when the mouse is not over the control) can be defined (BackColor1 and BackColor2 properties if button is not selected and SelectedColor1 and SelectedColor2 otherwise).
  • The start and the end color of the button (gradient colors) for hover state (when the mouse is over the control) can be defined (HoverColor1 and HoverColor2 properties if button is not selected and SelectedHoverColor1 and SelectedHoverColor2 otherwise).
  • The start and the end color of the button (gradient colors) when the mouse is clicked on the control can be defined (ClickColor1 and ClickColor2 properties).
  • Can be defined whether to view the border or not (DrawBorder property).
  • Can be defined whether to hover the border or not (BorderHover property).
  • The border's normal color and hover color can be defined (BorderNormalColor and BorderHoverColor properties).
  • Color of the text when the mouse is over the control or the button is selected can be defined (respectively HoverForeColor and SelectedForeColor properties).
  • Another image and text can be defined when the control is selected (SelectedImage and SelectedText properties).
  • Any position of text and image is possible using ContentAlignment enumerations.
  • The beep's duration and frequency can be defined when BeepOn is equal to True.

Usage

  • Add the control to the toolbox (BHBut.dll).
  • Drop the BouncingHoverButton on a WinForm.
  • Set the corresponding properties.
  • Go!

Points of Interest

The BouncingHoverButton is written in VB.NET and inherits from Button. Colors, text and images are drawn with FillRectangle, DrawString, DrawImage and ControlPaint.DrawImageDisabled methods.

The main part of the code that provides all effects is the OnPaint event for the BouncingHoverButton:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  If Me.FlatStyle = FltStyle.BouncingHover Then
    Dim pntMouse As Point = PointToClient(MousePosition)
    Dim R As New Rectangle(0, 0, Me.Width, Me.Height)
    Dim g As Graphics = e.Graphics
    Dim blnHighlight As Boolean = Me.Enabled AndAlso R.Contains(pntMouse)
    Dim blnPressed As Boolean = MouseButtons = MouseButtons.Left
    Dim P As New Point
    Dim c1 As Color
    Dim c2 As Color

    If blnClicked Then
      c1 = mClickColor1
      c2 = mClickColor2
    Else
      If mMouseHover Then
        If mSelected Then
          c1 = mSelectedHoverColor1
          c2 = mSelectedHoverColor2
        Else
          c1 = mHoverColor1
          c2 = mHoverColor2
        End If
      Else
        If mSelected Then
          c1 = mSelectedColor1
          c2 = mSelectedColor2
        Else
          c1 = mBackColor1
          c2 = mBackColor2
        End If
      End If
    End If

    Dim LGBrush As New System.Drawing.Drawing2D.LinearGradientBrush(R, _
                                                c1, c2, mGradientMode)
    g.FillRectangle(LGBrush, New Rectangle(0, 0, Me.Width, Me.Height))
    If blnClicked Then blnClicked = False
      Dim ChosenImage As Image
      ' Draw the border
      If blnDrawBorder Then
        Dim BorderColor As Color
        If Me.FlatStyle = FltStyle.BouncingHover Then
          BorderColor = mBorderNormalColor
        End If
        If mMouseHover Then
          If blnBorderHover Then
            BorderColor = mBorderHoverColor
          End If
        End If
        Dim Pn As New Pen(BorderColor)
        g.DrawRectangle(Pn, 0, 0, Me.Width - 1, Me.Height - 1)
      End If
      'Choose the Image
      If Not Me.Image Is Nothing AndAlso Me.SelectedImage Is Nothing Then
        ChosenImage = Me.Image
      ElseIf Not Me.Image Is Nothing AndAlso _
             Not Me.SelectedImage Is Nothing Then
        If Not mSelected Then
          ChosenImage = Me.Image
        Else
          ChosenImage = Me.SelectedImage
        End If
      ElseIf Me.Image Is Nothing AndAlso Not Me.SelectedImage Is Nothing Then
        If mSelected Then
          ChosenImage = Me.SelectedImage
      End If
    End If

    If Not Me.Image Is Nothing Or Me.Image Is Nothing AndAlso _
           Not Me.SelectedImage Is Nothing AndAlso mSelected Then
      If Not ChosenImage.Width = 0 Then
        ' Get the position of the image
        Select Case ImageAlign
          Case ContentAlignment.BottomCenter
            P = New Point(Convert.ToInt32((Me.Width / 2) - _
                (ChosenImage.Width / 2)), _
                (Me.Height - ChosenImage.Height) - 3)
          Case ContentAlignment.BottomLeft
            P = New Point(5, (Me.Height - ChosenImage.Height) - 3)
          Case ContentAlignment.BottomRight
            P = New Point((Me.Width - ChosenImage.Width) - 5, _
                (Me.Height - ChosenImage.Height) - 3)
          Case ContentAlignment.MiddleCenter
            P = New Point(Convert.ToInt32((Me.Width / 2) - _
                (ChosenImage.Width / 2)), _
                Convert.ToInt32((Me.Height / 2) - _
                (ChosenImage.Height / 2)))
          Case ContentAlignment.MiddleLeft
            P = New Point(5, Convert.ToInt32((Me.Height / 2) _
                - (ChosenImage.Height / 2)))
          Case ContentAlignment.MiddleRight
            P = New Point((Me.Width - ChosenImage.Width) - 5, _
                Convert.ToInt32((Me.Height / 2) - _
                (ChosenImage.Height / 2)))
          Case ContentAlignment.TopCenter
            P = New Point(Convert.ToInt32((Me.Width / 2) - _
                (ChosenImage.Width / 2)), 5)
          Case ContentAlignment.TopLeft
            P = New Point(5, 5)
          Case ContentAlignment.TopRight
            P = New Point((Me.Width - ChosenImage.Width) - 5, 5)
        End Select
      End If

      If Not Me.Enabled Then
        ControlPaint.DrawImageDisabled(g, ChosenImage, _
                          P.X, P.Y, Color.Transparent)
      Else
        ControlPaint.DrawImageDisabled(g, ChosenImage, _
                     P.X, P.Y, Color.Transparent)
        If blnHighlight AndAlso Not blnPressed Then
          e.Graphics.DrawImage(ChosenImage, P.X - 1, _
               P.Y - 2, ChosenImage.Width - 1, ChosenImage.Height - 1)
        ElseIf blnHighlight AndAlso blnPressed Then
          g.DrawImage(ChosenImage, P.X, P.Y)
        ElseIf blnHighlight AndAlso blnClicked = False Then
          e.Graphics.DrawImage(ChosenImage, P.X - 1, P.Y - 2, _
                     ChosenImage.Width - 1, ChosenImage.Height - 1)
        ElseIf blnHighlight = False Then
          g.DrawImage(ChosenImage, P.X, P.Y)
        End If
      End If
    End If ''''''

    ' Get the dimensions of the text.
    Dim ptSize As SizeF = g.MeasureString(Text, Font)
    Dim ptF As New PointF
    ' Get the location to set the Text.
    Select Case Me.TextAlign
      Case ContentAlignment.BottomCenter
        ptF.Y = Me.Height
        ptF.Y -= ptSize.Height
        ptF.X = Convert.ToInt32(Me.Width / 2)
        ptF.X -= ptSize.Width / 2
      Case ContentAlignment.BottomLeft
        ptF.Y = Me.Height
        ptF.Y -= ptSize.Height
        ptF.X = 2
      Case ContentAlignment.BottomRight
        ptF.Y = Me.Height
        ptF.Y -= ptSize.Height
        ptF.X = Me.Width
        ptF.X -= ptSize.Width
      Case ContentAlignment.MiddleCenter
        ptF.Y = Convert.ToInt32(Me.Height / 2)
        ptF.Y -= ptSize.Height / 2
        ptF.X = Convert.ToInt32(Me.Width / 2)
        ptF.X -= ptSize.Width / 2
      Case ContentAlignment.MiddleLeft
        ptF.Y = Convert.ToInt32(Me.Height / 2)
        ptF.Y -= ptSize.Height / 2
        ptF.X = 0
      Case ContentAlignment.MiddleRight
        ptF.Y = Convert.ToInt32(Me.Height / 2)
        ptF.Y -= ptSize.Height / 2
        ptF.X = Me.Width
        ptF.X -= ptSize.Width
      Case ContentAlignment.TopCenter
        ptF.Y = 0
        ptF.X = Convert.ToInt32(Me.Width / 2)
        ptF.X -= ptSize.Width / 2
      Case ContentAlignment.TopLeft
        ptF.Y = 0
        ptF.X = 0
      Case ContentAlignment.TopRight
        ptF.Y = 0
        ptF.X = Me.Width
        ptF.X -= ptSize.Width
    End Select

    Dim ChosenText As String
    If Not Me.Text Is Nothing AndAlso _
           Me.SelectedText Is Nothing Then
      ChosenText = Me.Text
    ElseIf Not Me.Text Is Nothing AndAlso _
               Not Me.SelectedText Is Nothing Then
      If Not mSelected Then
        ChosenText = Me.Text
      Else
        ChosenText = Me.SelectedText
      End If
    ElseIf Me.Text Is Nothing AndAlso Not _
                   Me.SelectedText Is Nothing Then
      If mSelected Then
        ChosenText = Me.SelectedText
      End If
    End If

    Dim ChosenForeColor As Color
    If Not Me.ForeColor.Equals(Color.Empty) AndAlso _
              Me.SelectedForeColor.Equals(Color.Empty) Then
      ChosenForeColor = Me.ForeColor
    ElseIf Not Me.ForeColor.Equals(Color.Empty) AndAlso _
               Not Me.SelectedForeColor.Equals(Color.Empty) Then
      If Not mSelected Then
        ChosenForeColor = Me.ForeColor
      Else
        ChosenForeColor = Me.SelectedForeColor
      End If
    ElseIf Me.ForeColor.Equals(Color.Empty) AndAlso _
              Not Me.SelectedForeColor.Equals(Color.Empty) Then
      If mSelected Then
        ChosenForeColor = Me.SelectedForeColor
      End If
    End If

    Dim textRectangleF As New RectangleF(ptF.X, ptF.Y, _
                             ptSize.Width, ptSize.Height)
    If Not Me.Enabled Then
      ControlPaint.DrawStringDisabled(g, ChosenText, _
             Me.Font, Color.FromKnownColor(KnownColor.Control), _
             textRectangleF, _
             System.Drawing.StringFormat.GenericTypographic)
    Else
      If mMouseHover Then
        If Not Me.HoverForeColor.Equals(Color.Empty) Then
          If blnHighlight AndAlso Not blnPressed Then
            e.Graphics.DrawString(ChosenText, Me.Font, _
                New SolidBrush(Me.HoverForeColor), ptF.X - 1, ptF.Y - 3)
          ElseIf blnHighlight AndAlso blnPressed Then
            g.DrawString(ChosenText, Me.Font, _
                   New SolidBrush(Me.HoverForeColor), ptF)
          ElseIf blnHighlight AndAlso blnClicked = False Then
            e.Graphics.DrawString(ChosenText, Me.Font, _
                New SolidBrush(Me.HoverForeColor), ptF.X - 1, ptF.Y - 3)
          ElseIf blnHighlight = False Then
            g.DrawString(ChosenText, Me.Font, _
                New SolidBrush(Me.HoverForeColor), ptF)
          End If
        Else
          If blnHighlight AndAlso Not blnPressed Then
            e.Graphics.DrawString(ChosenText, Me.Font, _
                New SolidBrush(ChosenForeColor), ptF.X - 1, ptF.Y - 3)
          ElseIf blnHighlight AndAlso blnPressed Then
            g.DrawString(ChosenText, Me.Font, _
                  New SolidBrush(ChosenForeColor), ptF)
          ElseIf blnHighlight AndAlso blnClicked = False Then
            e.Graphics.DrawString(ChosenText, Me.Font, _
                New SolidBrush(ChosenForeColor), ptF.X - 1, ptF.Y - 3)
          ElseIf blnHighlight = False Then
            g.DrawString(ChosenText, Me.Font, _
                New SolidBrush(ChosenForeColor), ptF)
          End If
        End If
      Else
        g.DrawString(ChosenText, Me.Font, _
                    New SolidBrush(ChosenForeColor), ptF)
      End If
    End If
  Else
      MyBase.OnPaint(e)
  End If
End Sub

Notes

I tested this project under VS.NET 2003 and Windows XP SP2.

Contact me

My name is Levan Midodashvili and you can contact me by email: levmid@hotmail.com or levmid@yahoo.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Levan Midodashvili
Web Developer
Georgia Georgia
Lecturer in Gori University (Georgia)

Comments and Discussions

 
GeneralA Clone! Pinmembercarloqueirolo3-Nov-05 9:12 
GeneralRe: A Clone! PinmemberjohnMCCA3-Nov-05 23:29 
GeneralNice PinmemberGiorgi Moniava12-Oct-05 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 3 Nov 2005
Article Copyright 2005 by Levan Midodashvili
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid