Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake OwnerDraw menu items in VB .NET
KeywordsOwnerDraw, menu, custom menu
CategoriesControls, VB.NET
 

At design time, set the menu item's OwnerDraw property to True. Then at run time, you must respond to two events for the menu: MeasureItem and DrawItem.

The MeasureItem event handler receives as a parameter an object of type MeasureItemEventArgs. The code must set this object's ItemHeight and ItemWidth properties to tell Visual Basic how much room the menu needs. This example determines how big the string it will draw is going to be and requests enough room for the string.

The DrawItem event handler receives as a parameter an object of type DrawItemEventArgs that has properties and methods that tell where the menu should draw. This example checks the object's State parameter to see if the mouse is over the menu item. If it is, the program draws a gradient background and the menu's text in AliceBlue. If the mouse is not over the menu item, the program draws a simple light gray background with black text.

 
Private Const FONT_NAME As String = "Times New Roman"
Private Const FONT_SIZE As Single = 12
Private Const FONT_STYLE As FontStyle = FontStyle.Bold
Private Const MENU_CAPTION As String = "Say Hi"

' Tell Windows how big to make the menu item.
Private Sub mnuFileSayHi_MeasureItem(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.MeasureItemEventArgs) Handles _
    mnuFileSayHi.MeasureItem
    ' Create the font we will use to draw the text.
    Dim menu_font As New Font( _
        FONT_NAME, FONT_SIZE, FONT_STYLE)

    ' See how big the text will be.
    Dim text_size As SizeF = _
        e.Graphics.MeasureString(MENU_CAPTION, menu_font)

    ' Set the necessary size.
    e.ItemHeight = text_size.Height
    e.ItemWidth = text_size.Width
End Sub

' Draw the menu item.
Private Sub mnuFileSayHi_DrawItem(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.DrawItemEventArgs) Handles _
    mnuFileSayHi.DrawItem
    ' Create the font we will use to draw the text.
    Dim menu_font As New Font( _
        FONT_NAME, FONT_SIZE, FONT_STYLE)

    ' See if the mouse is over the menu item.
    If e.State And DrawItemState.Selected Then
        ' The mouse is over the item.
        ' Draw a shaded background.
        Dim clr As Double
        Dim dclr As Double
        Dim Y As Long

        clr = 255
        dclr = 128 / e.Bounds.Height
        For Y = 0 To e.Bounds.Height - 1
            e.Graphics.DrawLine( _
                New Pen(Color.FromArgb(clr, 0, 0)), _
                e.Bounds.X, Y, e.Bounds.X + e.Bounds.Width, _
                    Y)
            clr -= dclr
        Next Y

        ' Draw the text.
        e.Graphics.DrawString(MENU_CAPTION, menu_font, _
            System.Drawing.Brushes.AliceBlue, _
            e.Bounds.X, e.Bounds.Y)
    Else
        ' The mouse is not over the item.
        ' Erase the background.
        e.Graphics.FillRectangle( _
            System.Drawing.Brushes.LightGray, _
            e.Bounds.X, e.Bounds.Y, _
            e.Bounds.Width, e.Bounds.Height)

        ' Draw the text.
        e.Graphics.DrawString(MENU_CAPTION, menu_font, _
            System.Drawing.Brushes.Black, _
            e.Bounds.X, e.Bounds.Y)
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated