Menu Looks Like MS Office (Icon,Font,Colors) so cool

Hi every one,

The way to do it is easy:

- Create a new user control
- Copy the code and paste it in the control (Clear the control class declaration before pasting).
- Design you menu (Main Menu or Context Menu)
- Find and Replace (Ctrl+H) (System.Windows.Forms.MenuItem) with (IconMenuItem)

Enjoy :)



Imports System

Imports System.ComponentModel

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Drawing.Text

Imports System.Reflection

Imports System.Resources

Imports System.Windows.Forms

PublicClass IconMenuItem :Inherits MenuItem

Public m_IconAs Image

Private m_FontAs Font

' By default these are set to the SystemColors Highight and Control values.

' This allows the appropriate color to be displayed if the user changes

' themes or display settings.

' These can be overriden by calling the appropriate constructor for this

' class.

Private m_Gradient_Color1As Color = SystemColors.Highlight

Private m_Gradient_Color2As Color = SystemColors.Control

Private TCAs Color = SystemColors.HotTrack

Private TFAs Color = Color.FromArgb(95, SystemColors.Highlight)

Dim LeftBarAs Brush =New SolidBrush(Color.FromArgb(80, SystemColors.GrayText))

PublicSubNew()

MyClass.New("",Nothing,Nothing, System.Windows.Forms.Shortcut.None)

EndSub

PublicSubNew(ByVal textAsString,ByVal iconAs Image,ByVal onClickAs EventHandler,ByVal shortcutAs Shortcut)

MyBase.New(text, onClick, shortcut)

' Owner Draw Property allows you to modify the menu item by handling

' OnMeasureItem and OnDrawItem

OwnerDraw =True

m_Font =New Font("Tahoma", 8)

m_Icon = icon

EndSub

' Additional constructor allows the setting of custom colors for each part of the menu

' color gradient.

PublicSubNew(ByVal textAsString,ByVal GradientColor1As System.Drawing.Color,ByVal GradientColor2As System.Drawing.Color,ByVal iconAs Image,ByVal onClickAs EventHandler,ByVal shortcutAs Shortcut)

MyBase.New(text, onClick, shortcut)

' Key Property

OwnerDraw =True

m_Font =New Font("Times New Roman", 8)

m_Gradient_Color1 = GradientColor1

m_Gradient_Color2 = GradientColor2

m_Icon = icon

EndSub

PrivateFunction GetRealText()AsString

Dim sAsString = Text

' Append shortcut if one is set and it should be visible

If ShowShortcutAnd Shortcut <> Shortcut.NoneThen

' To get a string representation of a Shortcut value, cast

' it into a Keys value and use the KeysConverter class (via TypeDescriptor).

Dim kAs Keys =CType(Shortcut, Keys)

s = s & Convert.ToChar(9) & TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)

EndIf

Return s

EndFunction

ProtectedOverridesSub OnDrawItem(ByVal eAs DrawItemEventArgs)

' OnDrawItem perfoms the task of actually drawing the item after

' measurement is complete

MyBase.OnDrawItem(e)

Dim brAs Brush

Dim pnAs Pen

IfNot m_IconIsNothingThen

e.Graphics.DrawImage(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2)

EndIf

Dim rcBkAs Rectangle = e.Bounds

IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Then

rcBk.X += 0

Else

rcBk.X += 0

EndIf

' Draw a background to the menu item with a linear gradient.

' This will use system defaults unless colors and have been

' passed on menu item instantiation

IfCBool(e.StateAnd DrawItemState.Selected)Then

' Draw the main rectangle

IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Then

br =New SolidBrush(TF)

pn =New Pen(TC)

e.Graphics.DrawRectangle(pn,New Rectangle(rcBk.X + 1, rcBk.Y + 1, rcBk.Width - 2, rcBk.Height - 3))

e.Graphics.FillRectangle(br,New Rectangle(rcBk.X + 2, rcBk.Y + 2, rcBk.Width - 3, rcBk.Height - 4))

Else

br =New SolidBrush(TF)

pn =New Pen(TC)

e.Graphics.DrawRectangle(pn,New Rectangle(rcBk.X, rcBk.Y, rcBk.Width - 1, rcBk.Height - 1))

e.Graphics.FillRectangle(br, rcBk)

e.Graphics.FillRectangle(LeftBar,New Rectangle(e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height))

EndIf

'Draw Icon

IfNot m_IconIsNothingThen

e.Graphics.DrawImage(m_Icon, e.Bounds.Left + 1, e.Bounds.Top + 1)

EndIf

'Draw Check If Checked and Radio If Selected

Dim SymbolsFontAsNew Font("Marlett", 12)

IfMe.RadioCheckThen

e.Graphics.DrawString("h", SymbolsFont,New SolidBrush(Color.White), e.Bounds.Left + 1, e.Bounds.Top + 1)

ElseIfMe.CheckedThen

e.Graphics.DrawString("a", SymbolsFont,New SolidBrush(Color.White), e.Bounds.Left + 1, e.Bounds.Top + 1)

EndIf

ElseIfCBool(e.StateAnd DrawItemState.HotLight)Then

IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Then

br =New SolidBrush(Color.FromArgb(20, SystemColors.Highlight))

pn =New Pen(TC)

e.Graphics.DrawRectangle(pn,New Rectangle(rcBk.X + 1, rcBk.Y + 1, rcBk.Width - 2, rcBk.Height - 3))

e.Graphics.FillRectangle(br,New Rectangle(rcBk.X + 2, rcBk.Y + 2, rcBk.Width - 3, rcBk.Height - 4))

Else

br =New SolidBrush(TF)

pn =New Pen(TC)

e.Graphics.DrawRectangle(pn,New Rectangle(rcBk.X, rcBk.Y, rcBk.Width - 1, rcBk.Height - 1))

'e.Graphics.FillRectangle(br, rcBk)

e.Graphics.FillRectangle(LeftBar,New Rectangle(e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height))

EndIf

'Draw Icon

IfNot m_IconIsNothingThen

e.Graphics.DrawImage(m_Icon, e.Bounds.Left + 1, e.Bounds.Top + 1)

EndIf

Else

pn = SystemPens.Control

br = SystemBrushes.Control

' Draw the main rectangle

IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Then

e.Graphics.DrawRectangle(pn,New Rectangle(rcBk.X + 1, rcBk.Y + 1, rcBk.Width - 2, rcBk.Height - 3))

e.Graphics.FillRectangle(br,New Rectangle(rcBk.X + 2, rcBk.Y + 2, rcBk.Width - 3, rcBk.Height - 4))

Else

e.Graphics.FillRectangle(br, rcBk)

e.Graphics.FillRectangle(LeftBar,New Rectangle(e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height))

EndIf

'Draw Icon

IfNot m_IconIsNothingThen

e.Graphics.DrawImage(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2)

EndIf

'Draw Check If Checked and Radio If Selected

Dim SymbolsFontAsNew Font("Marlett", 12)

IfMe.RadioCheckThen

e.Graphics.DrawString("h", SymbolsFont,New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 1)

ElseIfMe.CheckedThen

e.Graphics.DrawString("a", SymbolsFont,New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 1)

EndIf

EndIf

' Leave room for accelerator key

Dim sfAs StringFormat =New StringFormat

sf.HotkeyPrefix = HotkeyPrefix.Show

' Draw the actual menu text

br =New SolidBrush(e.ForeColor)

pn =New Pen(SystemColors.ControlDarkDark, 1)

IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Then

e.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 4, e.Bounds.Top + 2, sf)

ElseIfMe.Text = "-"Then

e.Graphics.DrawLine(pn, e.Bounds.Left + 25, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Top + 2)

Else

e.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 25, e.Bounds.Top + 3, sf)

EndIf

EndSub

ProtectedOverridesSub OnMeasureItem(ByVal eAs MeasureItemEventArgs)

' The MeasureItem event along with the OnDrawItem event are the two key events

' that need to be handled in order to create owner drawn menus.

' Measure the string that makes up a given menu item and use it to set the

' size of the menu item being drawn.

Dim sfAsNew StringFormat

sf.HotkeyPrefix = HotkeyPrefix.Show

MyBase.OnMeasureItem(e)

e.ItemHeight = 18

e.ItemWidth =CInt(e.Graphics.MeasureString(GetRealText(), m_Font, 10000, sf).Width) + 10

IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Then

e.ItemWidth -= 12

e.ItemHeight += 6

ElseIfMe.IsParentThen

e.ItemWidth += 20

ElseIfMe.Text = "-"Then

e.ItemHeight = 5

Else

e.ItemWidth += 18

EndIf

EndSub

PublicProperty Icon()As Image

Get

Icon =Me.m_Icon

EndGet

Set(ByVal ValueAs Image)

m_Icon = Value

EndSet

EndProperty

EndClass




Feel free to modify Fonts and Colors.

For any technical questions about the message feel free to contact me
Abdullah Al-Rasheed

[27619 byte] By [TwistedHardware] at [2007-12-17]