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

ContextMenuStrip Control with Microsoft Office like Visual Style using VB.NET

0.00/5 (No votes)
11 Dec 2011 1  
ContextMenuStrip Control with Microsoft Office like Visual Style using VB.NET
MBContextMenuStripDemo.gif

Introduction

Why another ContextMenuStrip? The standard ContextMenuStrip 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 Microsoft Office 2007 Visual Style. It is simple to use, just drop it on the form, and use it like the normal ContextMenuStrip.

Background

MBContextMenuStrip is a ContextMenuStrip which inherits all the properties of simple ContextMenuStrip control. I added Microsoft Office 2007 like Visuals in MBContextMenuStrip. The language used is VB.NET. There are so many classes which provide the same functionality, but for that we have to write minimum two lines of code to add that renderer class in our application. MBContextMenuStrip is the ContextMenuStrip which already contains MBRenderer Class You Just add the reference of MBContextMenuStrip.dll and use it by dragging and dropping.

Code

The concept for this ContextMenuStrip came from the Microsoft Office 2007 Right Click Menu. I organized methods of MBContextMenuStrip into layers like this.

The following methods are responsible for rendering simple ContextMenuStrip like Microsoft office.

This method Render Background of MBContextMenuStrip item:

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

This method Render Image of MBContextMenuStrip Item:

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
This method Handles painting of MBContextMenuStrip:
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

Using the Code

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

MBContextMenuStripControl.gif

History

  • MBContextMenuStrip Version 1.0

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