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 SystemImports System.ComponentModelImports System.DrawingImports System.Drawing.Drawing2DImports System.Drawing.TextImports System.ReflectionImports System.ResourcesImports System.Windows.FormsPublic Class IconMenuItem :Inherits MenuItemPublic m_IconAs ImagePrivate 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.HighlightPrivate m_Gradient_Color2As Color = SystemColors.ControlPrivate TCAs Color = SystemColors.HotTrackPrivate 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)EndSubPublicSubNew(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 OnDrawItemOwnerDraw = Truem_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 PropertyOwnerDraw = Truem_Font = New Font("Times New Roman", 8)m_Gradient_Color1 = GradientColor1 m_Gradient_Color2 = GradientColor2 m_Icon = icon EndSubPrivateFunction GetRealText()AsStringDim sAsString = Text' Append shortcut if one is set and it should be visibleIf 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)EndIfReturn sEndFunctionProtectedOverridesSub OnDrawItem(ByVal eAs DrawItemEventArgs)' OnDrawItem perfoms the task of actually drawing the item after' measurement is completeMyBase.OnDrawItem(e)Dim brAs BrushDim pnAs PenIfNot m_IconIsNothingThene.Graphics.DrawImage(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2) EndIfDim rcBkAs Rectangle = e.BoundsIfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"ThenrcBk.X += 0 ElsercBk.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 instantiationIfCBool(e.StateAnd DrawItemState.Selected)Then' Draw the main rectangleIfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Thenbr = 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))Elsebr = 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 IconIfNot m_IconIsNothingThene.Graphics.DrawImage(m_Icon, e.Bounds.Left + 1, e.Bounds.Top + 1) EndIf'Draw Check If Checked and Radio If SelectedDim SymbolsFontAsNew Font("Marlett", 12)IfMe.RadioCheckThene.Graphics.DrawString("h", SymbolsFont, New SolidBrush(Color.White), e.Bounds.Left + 1, e.Bounds.Top + 1)ElseIfMe.CheckedThene.Graphics.DrawString("a", SymbolsFont, New SolidBrush(Color.White), e.Bounds.Left + 1, e.Bounds.Top + 1)EndIfElseIfCBool(e.StateAnd DrawItemState.HotLight)ThenIfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Thenbr = 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))Elsebr = 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 IconIfNot m_IconIsNothingThene.Graphics.DrawImage(m_Icon, e.Bounds.Left + 1, e.Bounds.Top + 1) EndIfElsepn = SystemPens.Control br = SystemBrushes.Control ' Draw the main rectangleIfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Thene.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))Elsee.Graphics.FillRectangle(br, rcBk) e.Graphics.FillRectangle(LeftBar, New Rectangle(e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height))EndIf'Draw IconIfNot m_IconIsNothingThene.Graphics.DrawImage(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2) EndIf'Draw Check If Checked and Radio If SelectedDim SymbolsFontAsNew Font("Marlett", 12)IfMe.RadioCheckThene.Graphics.DrawString("h", SymbolsFont, New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 1)ElseIfMe.CheckedThene.Graphics.DrawString("a", SymbolsFont, New SolidBrush(Color.Black), e.Bounds.Left + 1, e.Bounds.Top + 1)EndIfEndIf' Leave room for accelerator keyDim sfAs StringFormat =New StringFormatsf.HotkeyPrefix = HotkeyPrefix.Show ' Draw the actual menu textbr = New SolidBrush(e.ForeColor)pn = New Pen(SystemColors.ControlDarkDark, 1)IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Thene.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 4, e.Bounds.Top + 2, sf) ElseIfMe.Text = "-"Thene.Graphics.DrawLine(pn, e.Bounds.Left + 25, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Top + 2) Elsee.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 25, e.Bounds.Top + 3, sf) EndIfEndSubProtectedOverridesSub 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 StringFormatsf.HotkeyPrefix = HotkeyPrefix.Show MyBase.OnMeasureItem(e)e.ItemHeight = 18 e.ItemWidth = CInt(e.Graphics.MeasureString(GetRealText(), m_Font, 10000, sf).Width) + 10IfMe.Parent.GetType().ToString = "System.Windows.Forms.MainMenu"Thene.ItemWidth -= 12 e.ItemHeight += 6 ElseIfMe.IsParentThene.ItemWidth += 20 ElseIfMe.Text = "-"Thene.ItemHeight = 5 Elsee.ItemWidth += 18 EndIfEndSubPublicProperty Icon()As ImageGetIcon = Me.m_IconEndGetSet(ByVal ValueAs Image)m_Icon = Value EndSetEndPropertyEnd Class
|
Feel free to modify Fonts and Colors.For any technical questions about the message feel free to contact me
Abdullah Al-Rasheed