Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / Visual Basic

MenuStrip Control with MSOffice Visual Styles using VB.NET

Rate me:
Please Sign up or sign in to vote.
4.15/5 (9 votes)
14 Dec 2011CPOL1 min read 30.7K   2.4K   13  
A User Control with Microsoft Office 2007 Visual Style
#Region "   About Auhor"
'
'   UserControl Name    :   MBMenuStrip Control
'   Created             :   12 Dec 2011
'   Purpose             :   Extends style capabilities of a MenuStrip Control
'   Vision              :   1.0.4362.39367
'   IDE                 :   Visual Basic .Net 2008
'   Author              :   Manoj K Bhoir
'
'   You can not:
'   Sell or redistribute this code or the binary for profit.
'   Use this in spyware, malware, or any generally acknowledged form of malicious software.
'   Remove or alter the above author accreditation, or this disclaimer.
'
'   You can:
'   Use this code in your applications in any way you like.
'   Use this in a published program, (a credit to MBUserControls would be nice)
'
'   I will not:
'   Except any responsibility for this code whatsoever. 
'   There is no guarantee of fitness, nor should you have any expectation of support. 
'   
'                                                                   Manoj K Bhoir
'                                                                   manojbhoir28@gmail.com    
#End Region

#Region "   Imports"

Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.ComponentModel

#End Region

#Region "   MBMenuStrip"

''' <summary>
''' MBMenuStrip Class © Manoj K Bhoir
''' </summary>
''' <remarks>Version 1.0</remarks>
<ToolboxItem(True), ToolboxBitmap(GetType(MBMenuStrip), "MBMenuStrip.MBMenuStrip.bmp"), ToolboxItemFilter("System.Windows.Forms"), Description("Display the MenuStrip.")> _
Public Class MBMenuStrip
    Inherits MenuStrip
    ''' <summary>
    ''' Constructor of MBMenuStrip
    ''' </summary>
    Public Sub New()
        InitializeComponent()
    End Sub
    ''' <summary>
    ''' Initialize MBMenuStrip and MBMenuRenderClass
    ''' </summary>
    Public Sub InitializeComponent()
        Me.Name = "MBMenuStrip"
        Me.Renderer = New MBMenuRendere()
    End Sub

End Class
''' <summary>
''' MBMenuRenderer Class for MBMenuStrip
''' </summary>
''' <remarks>Version 1.0</remarks>
Public Class MBMenuRendere
    Inherits ToolStripProfessionalRenderer
    Dim R0 As Int32 = 255, G0 As Int32 = 214, B0 As Int32 = 78
    Public StrokeColor As Color = Color.FromArgb(196, 177, 118)
    ''' <summary>
    ''' Draw MenuItem Background of MBMenuStrip
    ''' </summary>
    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) {GetColor(0, 50, 100), GetColor(0, 0, 30), Color.FromArgb(R0, G0, B0), GetColor(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

    Dim offsetx As Int32 = 3, offsety As Int32 = 2, imageheight As Int32 = 0, imagewidth As Int32 = 0
    ''' <summary>
    ''' Draw ItemImage for MBMenuStrip
    ''' </summary>
    Protected Overrides Sub OnRenderItemImage(ByVal e As System.Windows.Forms.ToolStripItemImageRenderEventArgs)
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality
        If Not (e.Image Is Nothing) Then
            imageheight = e.Item.Height - offsety * 2
            imagewidth = ((Convert.ToDouble(imageheight) / e.Image.Height) * e.Image.Width)
        End If
        e.Graphics.DrawImage(e.Image, New Rectangle(offsetx, offsety, imagewidth, imageheight))
    End Sub

#Region "Paint Method"

    Private _radius As Int32 = 6
    ''' <summary>
    ''' Draw Arc around MenuItem of MBMenuStrip
    ''' </summary>
    ''' <param name="re">Rectangle</param>
    ''' <param name="pa">Graphics Pth</param>
    Public Sub DrawArc(ByVal re As Rectangle, ByVal pa As GraphicsPath)
        Dim _radiusX0Y0 As Int32 = _radius, _radiusXFY0 As Int32 = _radius, _radiusX0YF As Int32 = _radius, _radiusXFYF As Int32 = _radius
        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
    ''' <summary>
    ''' Color used for Rendering MBMenuStrip
    ''' </summary>
    ''' <param name="R">Red Value</param>
    ''' <param name="G">Green Value</param>
    ''' <param name="B">Blue Value</param>
    ''' <returns></returns>
    ''' <remarks>New Color</remarks>
    Public Function GetColor(ByVal R As Int32, ByVal G As Int32, ByVal B As Int32) As Color
        If (R + R0 > 255) Then R = 255 Else R = R + R0
        If (G + G0 > 255) Then G = 255 Else G = G + G0
        If (B + B0 > 255) Then B = 255 Else B = B + B0
        Return Color.FromArgb(R, G, B)
    End Function

#End Region

End Class

#End Region






By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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..!!

Comments and Discussions