MenuStrip Control With Microsot Office

This article provides an enhanced MenuStrip with a Microsoft Office 2007 Visual Style as a User Control.

 

Introduction

Why another MenuStrip? The standard MenuStrip 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 onto the form and use it like the normal MenuStrip.

Background

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

Code

The concept for this MenuStrip came from the Microsoft Office 2007 context menu. I organized methods of MBMenuStrip into layers like this.

The following methods are responsible for rendering a simple MenuStrip like Microsoft Office.

The following method renders the background of a MBMenuStrip 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(2, 1, e.Item.Size.Width - 2, _  
  8.             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 an image of a MBMenuStrip 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 = ((Convert.ToDouble(imageheight) / _  
  7.                 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 
The following method handles the painting of the MBMenuStrip:
  1. Public Sub DrawArc(ByVal re As Rectangle, ByVal pa As GraphicsPath)  
  2.         Dim _radiusX0Y0 As Int32 = _radius, _radiusXFY0 As Int32 = _radius, _  
  3.             _radiusX0YF As Int32 = _radius, _radiusXFYF As Int32 = _radius  
  4.         pa.AddArc(re.X, re.Y, _radiusX0Y0, _radiusX0Y0, 180, 90)  
  5.         pa.AddArc(re.Width - _radiusXFY0, re.Y, _radiusXFY0, _radiusXFY0, 270, 90)  
  6.         pa.AddArc(re.Width - _radiusXFYF, _  
  7.     re.Height - _radiusXFYF, _radiusXFYF, _radiusXFYF, 0, 90)  
  8.         pa.AddArc(re.X, re.Height - _radiusX0YF, _radiusX0YF, _radiusX0YF, 90, 90)  
  9.         pa.CloseFigure()  
  10. End Sub 

Using the Code

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

History

  • MBMenuStrip Version 1.0