What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake menus display pictures in VB .NET
KeywordsOwnerDraw, menu, custom menu, picture
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 sets these values to 16 and 60 respectively.

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. In this example, the event handler calls subroutine DrawMenuBackground to draw the menu item's background. It then draws the appropriate PictureBox's image into the menu item.

Subroutine DrawMenuBackground checks the object's State parameter to see if the mouse is over the menu item. If it is, the program calls the object's DrawBackground method to draw a standard "mouse over" background and then draws the menu's text in white. If the mouse is not over the menu item, the routine makes the background a light gray and then draws the text in black.

Private Const MENU_WID As Long = 60
Private Const MENU_HGT As Long = 16
Private Const FONT_NAME As String = "MS Sans Serif"
Private Const FONT_SIZE As Single = 8.25
Private Const FONT_STYLE As FontStyle = FontStyle.Regular

Private Sub mnuFileExit_MeasureItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
    Handles mnuFileExit.MeasureItem
    e.ItemHeight = MENU_HGT
    e.ItemWidth = MENU_WID
End Sub

' Exit.
Private Sub mnuFileExit_DrawItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DrawItemEventArgs) _
    Handles mnuFileExit.DrawItem
    ' Draw the background.
    DrawMenuBackground("Exit", e)

    ' Draw the picture.
    e.Graphics.DrawImage(picExit.Image, e.Bounds.X, _
End Sub

' Draw the menu item.
Private Sub DrawMenuBackground(ByVal menu_text As String, _
    ByVal e As System.Windows.Forms.DrawItemEventArgs)
    ' Create the font we will use to draw the text.
    Dim menu_font As New Font( _

    ' See if the mouse is over the menu item.
    If e.State And DrawItemState.Selected Then
        ' The mouse is over the item.
        ' Use a appropriate dark background.

        ' Draw the text.
        e.Graphics.DrawString(menu_text, menu_font, _
            System.Drawing.Brushes.White, _
            e.Bounds.X + 18, e.Bounds.Y)
        ' The mouse is not over the item.
        ' Use a light background.
        e.Graphics.FillRectangle( _
            Brushes.LightGray, _
            e.Bounds.X, e.Bounds.Y, _
            e.Bounds.Width, e.Bounds.Height)

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