Owner draw Menu with selection highlight : Menu « GUI « VB.Net






Owner draw Menu with selection highlight

Owner draw Menu with selection highlight
Imports System
Imports System.Drawing
Imports System.Windows.Forms


Public Class MainClass
    
    Shared Sub Main()
        Dim form1 As Form = New Form1

        Application.Run(form1)


    End Sub
End Class

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuNew As System.Windows.Forms.MenuItem
    Friend WithEvents mnuOpen As System.Windows.Forms.MenuItem
    Friend WithEvents mnuSave As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.mnuFile = New System.Windows.Forms.MenuItem()
        Me.mnuNew = New System.Windows.Forms.MenuItem()
        Me.mnuOpen = New System.Windows.Forms.MenuItem()
        Me.mnuSave = New System.Windows.Forms.MenuItem()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
        '
        'mnuFile
        '
        Me.mnuFile.Index = 0
        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuNew, Me.mnuOpen, Me.mnuSave})
        Me.mnuFile.Text = "File"
        '
        'mnuNew
        '
        Me.mnuNew.Index = 0
        Me.mnuNew.OwnerDraw = True
        Me.mnuNew.Text = "New"
        '
        'mnuOpen
        '
        Me.mnuOpen.Index = 1
        Me.mnuOpen.OwnerDraw = True
        Me.mnuOpen.Text = "Open"
        '
        'mnuSave
        '
        Me.mnuSave.Index = 2
        Me.mnuSave.OwnerDraw = True
        Me.mnuSave.Text = "Save"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Owner-Drawn Menu"

    End Sub

#End Region

    Private Sub mnu_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles mnuNew.MeasureItem, mnuOpen.MeasureItem, mnuSave.MeasureItem
        Dim mnuItem As MenuItem = CType(sender, MenuItem)
        Dim MenuFont As New Font("Tahoma", 8)
        e.ItemHeight = e.Graphics.MeasureString(mnuItem.Text, MenuFont).Height + 5
        e.ItemWidth = e.Graphics.MeasureString(mnuItem.Text, MenuFont).Width + 30
    End Sub

    Dim MenuImage As Image = New Bitmap("figure2.bmp")

    Private Sub mnu_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles mnuNew.DrawItem, mnuOpen.DrawItem, mnuSave.DrawItem
        Dim mnuItem As MenuItem = CType(sender, MenuItem)

        If e.State And DrawItemState.Selected = DrawItemState.Selected Then
            e.DrawBackground()
        End If
        e.Graphics.DrawImage(MenuImage, e.Bounds.Left + 3, e.Bounds.Top + 2)
        e.Graphics.DrawString(mnuItem.Text, e.Font, New SolidBrush(e.ForeColor), e.Bounds.Left + 25, e.Bounds.Top + 3)
    End Sub
End Class

           
       








Related examples in the same category

1.Create Menu at run timeCreate Menu at run time
2.Get Menu StructureGet Menu Structure
3.Owner Draw MenuItemOwner Draw MenuItem
4.Add Menu, Menu Item and bind action at run timeAdd Menu, Menu Item and bind action at run time
5.Menu action DemoMenu action Demo
6.Menu Item and Menu ActionMenu Item and Menu Action
7.Using menus to change font colors and stylesUsing menus to change font colors and styles
8.Add menu to a frame and implement the menu actionAdd menu to a frame and implement the menu action
9.Menu action: Close a frame fromMenu action: Close a frame from
10.Add Menu to Parent MDI Frame when opening a childred FrameAdd Menu to Parent MDI Frame when opening a childred Frame