Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Glass Style Button With Dropdown MenuList using VB.NET 2008

0.00/5 (No votes)
29 Dec 2011 1  
A user control with lots of properties and versatility
MBGlassButton_Demo.JPG

Introduction

Why another Button? The standard Button is too limited in functionality and I couldn't find a custom control written that did all that I wanted. This is a User Control with lots of properties and versatility. It is simple to use, just drop it on the form, adjust the design time properties, and use it like the normal Button.

Background

MBGlassButton is a Button which inherits all the properties of a simple Button control. I added some extra functionalities in MBGlassButton like Glow, Split, Highlight, etc. The language used is VB.NET.

Control Properties

Here is the list of properties available in MBGlassButton:

  • Arrow: This property is used to set the arrow on MBGlassButton.
  • BaseColor: This property is used to set the Base Color of MBGlassButton.
  • BaseStrokeColor: This property is used to set the Base Stroke color of MBGlassButton.
  • OnColor: This property is used to set the on color of MBGlassButton.
  • OnStrokeColor: This property is used to set the On Stroke color of MBGlassButton.
  • PressColor: This property is used to set the Press Color of MBGlassButton.
  • PressStrokeColor: This property is used to set the Press Stroke Color of MBGlassButton.
  • ShowBase: This property is used to Show the Glow on MBGlassButton.
  • Radius: This property is used to set the corner Radius of MBGlassButton.
  • SplitButton: This property is used to Split MBGlassButton into two parts.
  • SplitDistance: This property is used to set the Split Distance of MBGlassButton.
  • SplitLocation: This property is used to Split MBGlassButton at a specific location.

Code

MBGlassButtonControl.jpg

The concept for this Button came from the Vista ‘Button’ or Windows 7 “Button”. I organized my paint event into layers like this:

Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = pevent.Graphics
        g.SmoothingMode = SmoothingMode.HighQuality
        g.InterpolationMode = InterpolationMode.High
        Dim r As Rectangle = New Rectangle(New Point(-1, -1), _
	New Size(Me.Width + _radius, Me.Height + _radius))
        Dim path As GraphicsPath = New GraphicsPath
        Dim rp As Rectangle = New Rectangle(New Point(0, 0), _
	New Size(Me.Width - 1, Me.Height - 1))
        DrawArc(rp, path)
        FillGradients(g, path)
        DrawImage(g)
        DrawText(g)
        DrawArrow(g)
End Sub

Following are some methods which provide a transparent look to MBGlassButton. This method draws the background for MBGlassButton.

Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        A0 = BaseColor.A
        R0 = BaseColor.R
        G0 = BaseColor.G
        B0 = BaseColor.B
        _colorStroke = _baseStroke
        Dim r As Rectangle = New Rectangle(New Point(-1, -1), _
		New Size(Me.Width + _radius, Me.Height + _radius))
        If Me.Size <> Nothing Then
            Dim pathregion As GraphicsPath = New GraphicsPath
            DrawArc(r, pathregion)
            Me.Region = New Region(pathregion)
        End If
End Sub

This method draws the background shadow for MBGlassButton:

Public Sub DrawShadow(ByVal re As Rectangle, ByVal pa As GraphicsPath)
        Dim _radiusX0Y0 As Integer = _radius, _radiusXFY0 _
	As Integer = _radius, _radiusX0YF _
        	As Integer = _radius, _radiusXFYF As Integer = _radius
        Select Case _grouppos
            Case MB_GroupPos.Left
                _radiusXFY0 = 1
                _radiusXFYF = 1
            Case MB_GroupPos.Center
                _radiusX0Y0 = 1
                _radiusX0YF = 1
                _radiusXFY0 = 1
                _radiusXFYF = 1
            Case MB_GroupPos.Right
                _radiusX0Y0 = 1
                _radiusX0YF = 1
            Case MB_GroupPos.Top
                _radiusX0YF = 1
                _radiusXFYF = 1
            Case MB_GroupPos.Bottom
                _radiusX0Y0 = 1
                _radiusXFY0 = 1
        End Select
        pa.AddArc(re.X, re.Y, _radiusX0Y0, _radiusX0Y0, 180, 90)
        pa.AddArc(re.Width - _radiusXFY0, re.Y, _radiusXFY0, _radiusXFY0, 270, 90)
        pa.AddArc(re.Width - _radiusXFYF, _
		re.Height - _radiusXFYF, _radiusXFYF, _radiusXFYF, 0, 90)
        pa.AddArc(re.X, re.Height - _radiusX0YF, _radiusX0YF, _radiusX0YF, 90, 90)
        pa.CloseFigure()
End Sub

This method handles the Menu List Rendering for MBGlassButton:

Protected Overrides Sub OnRenderMenuItemBackground_
(ByVal e As System.Windows.Forms.ToolStripItemRenderEventArgs)
        If e.Item.Selected Then
            Dim g As Graphics = e.Graphics
            g.SmoothingMode = SmoothingMode.HighQuality
            Dim pa As GraphicsPath = New GraphicsPath()
            Dim rect As Rectangle = New Rectangle_
		(2, 1, e.Item.Size.Width - 2, e.Item.Size.Height - 1)
            DrawArc(rect, pa)
            Dim lgbrush As LinearGradientBrush = New LinearGradientBrush_
            (rect, Color.White, Color.White, LinearGradientMode.Vertical)
            Dim pos As Single() = New Single(3) {0.0F, 0.4F, 0.45F, 1.0F}
            Dim colors As Color() = New Color(3) {GetRendererColor(0, 50, 100), _
            GetRendererColor(0, 0, 30), Color.FromArgb(R0, G0, B0), _
		GetRendererColor(0, 50, 100)}
            Dim mix As ColorBlend = New ColorBlend()
            mix.Colors = colors
            mix.Positions = pos
            lgbrush.InterpolationColors = mix
            g.FillPath(lgbrush, pa)
            g.DrawPath(New Pen(StrokeColor), pa)
            lgbrush.Dispose()
        Else
            MyBase.OnRenderMenuItemBackground(e)
        End If
    End Sub

Using the Code

MBGlassButtonMenuLists.jpg

Just add new context Menu Strip in your application and set it to MBGlassButton Context Menu Strip Property.

MBGlassButtonInToolBox.jpg

It is very easy to use the MBGlassButton in your application. Simply add the reference of the provided DLL to your application and just drag and drop.

History

  • 12/8/2011: MBGlassButton Version 1.0
  • 12/26/2011: Updated demo and source code

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