ContextMenuStrip Control With Microsoft Office



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 a Microsoft Office 2007 Visual Style. It is simple to use, just drop it onto the form and use it like the normal ContextMenuStrip.


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


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

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

The following method renders the background of a MBContextMenuStrip item:

  1. Protected Overrides Sub OnRenderMenuItemBackground_  
  2. (ByVal e As System.Windows.Forms.ToolStripItemRenderEventArgs)  
  3.         If e.Item.Selected Then  
  4.             Dim g As Graphics = e.Graphics  
  5.             g.SmoothingMode = SmoothingMode.HighQuality  
  6.             Dim pa As GraphicsPath = New GraphicsPath()  
  7.             Dim rect As Rectangle = New Rectangle_  
  8.             (2, 1, e.Item.Size.Width - 2, e.Item.Size.Height - 1)  
  9.             DrawArc(rect, pa)  
  10.             Dim lgbrush As LinearGradientBrush = New LinearGradientBrush_  
  11.             (rect, Color.White, Color.White, LinearGradientMode.Vertical)  
  12.             Dim pos As Single() = New Single(3) {0.0F, 0.4F, 0.45F, 1.0F}  
  13.             Dim colors As Color() = New Color(3) {GetColor(0, 50, 100), _  
  14.             GetColor(0, 0, 30), Color.FromArgb(R0, G0, B0), GetColor(0, 50, 100)}  
  15.             Dim mix As ColorBlend = New ColorBlend()  
  16.             mix.Colors = colors  
  17.             mix.Positions = pos  
  18.             lgbrush.InterpolationColors = mix  
  19.             g.FillPath(lgbrush, pa)  
  20.             g.DrawPath(New Pen(StrokeColor), pa)  
  21.             lgbrush.Dispose()  
  22.         Else  
  23.             MyBase.OnRenderMenuItemBackground(e)  
  24.         End If  
  25.     End Sub

The following method renders the image of a MBContextMenuStrip item:

  1. Protected Overrides Sub OnRenderItemImage_  
  2. (ByVal e As System.Windows.Forms.ToolStripItemImageRenderEventArgs)  
  3.         e.Graphics.SmoothingMode = SmoothingMode.HighQuality  
  4.         If Not (e.Image Is NothingThen  
  5.             imageheight = e.Item.Height - offsety * 2  
  6.             imagewidth = _  
  7.         ((Convert.ToDouble(imageheight) / e.Image.Height) * e.Image.Width)  
  8.         End If  
  9.         e.Graphics.DrawImage(e.Image, New Rectangle_  
  10.         (offsetx, offsety, imagewidth, imageheight))  
  11.     End Sub  
  12. This method Handles painting of MBContextMenuStrip:  
  13. Public Sub DrawArc(ByVal re As Rectangle, ByVal pa As GraphicsPath)  
  14.         Dim _radiusX0Y0 As Int32 = _radius, _radiusXFY0 As Int32 = _  
  15.         _radius, _radiusX0YF As Int32 = _radius, _radiusXFYF As Int32 = _radius  
  16.         pa.AddArc(re.X, re.Y, _radiusX0Y0, _radiusX0Y0, 180, 90)  
  17.         pa.AddArc(re.Width - _radiusXFY0, re.Y, _radiusXFY0, _radiusXFY0, 270, 90)  
  18.         pa.AddArc(re.Width - _radiusXFYF, _  
  19.     re.Height - _radiusXFYF, _radiusXFYF, _radiusXFYF, 0, 90)  
  20.         pa.AddArc(re.X, re.Height - _radiusX0YF, _radiusX0YF, _radiusX0YF, 90, 90)  
  21.         pa.CloseFigure()  
  22.     End Sub 

Using the Code

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


  •  MBContextMenuStrip Version 1.0

Up Next
    Ebook Download
    View all
    View all