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

Tagged as

Go to top

Glass Style Button With Dropdown MenuList using VB.NET 2008

, 29 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manoj K Bhoir
Software Developer
India India
It always seems good to hear about me, but the thing I do is I code. I'm Interested in Designing Windows Based Application, Web Applications and building Mobile Applications. Currently restricting it to Android 4.0 applications, building Internet Based Applications using ASP.NET and contributing to bring the student community to a position which will help technology to reach the greatest heights ever. A very Big fan of Microsoft & Android..!!
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 3 PinmemberMember 381729910-Jul-14 20:38 
GeneralRe: My vote of 3 PinprofessionalManoj K Bhoir11-Jul-14 19:05 
GeneralGlass Style Button With Dropdown MenuList using VB.NET 2008 PinmemberScryaga11-Mar-13 1:39 
GeneralRe: Glass Style Button With Dropdown MenuList using VB.NET 2008 PinmemberManoj K Bhoir11-Mar-13 19:54 
GeneralMy vote of 5 PinmemberPolinia30-Jan-12 23:10 
GeneralRe: My vote of 5 PinmemberManoj K Bhoir14-Feb-12 20:41 
QuestionButtons aren't tracking the MB_DOWN/in region state Pinmemberjeffb4211-Dec-11 7:37 
SuggestionRe: Buttons aren't tracking the MB_DOWN/in region state PinmemberManoj K Bhoir11-Dec-11 17:34 
QuestionButtons on left side of demo don't display the assigned ContextMenu Pinmemberjeffb4211-Dec-11 7:33 
AnswerRe: Buttons on left side of demo don't display the assigned ContextMenu PinmemberManoj K Bhoir11-Dec-11 17:22 
QuestionException when application is closed Pinmemberjeffb4211-Dec-11 7:28 
AnswerRe: Exception when application is closed PinmemberManoj K Bhoir11-Dec-11 17:17 
Thanks Jeff for your feedback and suggetions.I will implement this in MBGlassButton as soon as possible.
 
Thanks again. Smile | :)
QuestionLook like Office 2007 PinmemberĐỗ Hồng Ngọc8-Dec-11 14:52 
AnswerRe: Look like Office 2007 PinmemberManoj K Bhoir8-Dec-11 18:52 
Question.. Pinmemberfreakyit8-Dec-11 10:09 
AnswerRe: .. PinmemberManoj K Bhoir8-Dec-11 18:49 
AnswerRe: .. PinmemberManoj K Bhoir8-Dec-11 19:07 
Questionyeah Pinmemberfreakyit8-Dec-11 10:05 
AnswerRe: yeah PinmemberManoj K Bhoir8-Dec-11 18:52 

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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 29 Dec 2011
Article Copyright 2011 by Manoj K Bhoir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid