MenuItem.DrawItem : MenuItem « System.Windows.Forms « VB.Net by API






MenuItem.DrawItem

  
Imports System
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms
Imports System.IO


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
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem

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

    '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 Red As System.Windows.Forms.MenuItem
    Friend WithEvents Green As System.Windows.Forms.MenuItem
    Friend WithEvents Blue As System.Windows.Forms.MenuItem
    Friend WithEvents Yellow As System.Windows.Forms.MenuItem
    Friend WithEvents Black As System.Windows.Forms.MenuItem
    Friend WithEvents White As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents AlignLeft As System.Windows.Forms.MenuItem
    Friend WithEvents AlignCenter As System.Windows.Forms.MenuItem
    Friend WithEvents AlignRight As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem()
        Me.Red = New System.Windows.Forms.MenuItem()
        Me.Green = New System.Windows.Forms.MenuItem()
        Me.Blue = New System.Windows.Forms.MenuItem()
        Me.Yellow = New System.Windows.Forms.MenuItem()
        Me.Black = New System.Windows.Forms.MenuItem()
        Me.White = New System.Windows.Forms.MenuItem()
        Me.MenuItem2 = New System.Windows.Forms.MenuItem()
        Me.AlignLeft = New System.Windows.Forms.MenuItem()
        Me.AlignCenter = New System.Windows.Forms.MenuItem()
        Me.AlignRight = New System.Windows.Forms.MenuItem()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Red, Me.Green, Me.Blue, Me.Yellow, Me.Black, Me.White})
        Me.MenuItem1.Text = "Color"
        '
        'Red
        '
        Me.Red.Index = 0
        Me.Red.OwnerDraw = True
        Me.Red.Text = ""
        '
        'Green
        '
        Me.Green.Index = 1
        Me.Green.OwnerDraw = True
        Me.Green.Text = ""
        '
        'Blue
        '
        Me.Blue.Index = 2
        Me.Blue.OwnerDraw = True
        Me.Blue.Text = ""
        '
        'Yellow
        '
        Me.Yellow.Index = 3
        Me.Yellow.OwnerDraw = True
        Me.Yellow.Text = ""
        '
        'Black
        '
        Me.Black.Index = 4
        Me.Black.OwnerDraw = True
        Me.Black.Text = ""
        '
        'White
        '
        Me.White.Index = 5
        Me.White.OwnerDraw = True
        Me.White.Text = ""
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 1
        Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.AlignLeft, Me.AlignCenter, Me.AlignRight})
        Me.MenuItem2.Text = "Alignment"
        '
        'AlignLeft
        '
        Me.AlignLeft.Index = 0
        Me.AlignLeft.OwnerDraw = True
        Me.AlignLeft.Text = "Left"
        '
        'AlignCenter
        '
        Me.AlignCenter.Index = 1
        Me.AlignCenter.OwnerDraw = True
        Me.AlignCenter.Text = "Center"
        '
        'AlignRight
        '
        Me.AlignRight.Index = 2
        Me.AlignRight.OwnerDraw = True
        Me.AlignRight.Text = "Right"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 129)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Owner-Drawn Menu"

    End Sub

#End Region

    Dim currentFont As Font

    Private Sub Red_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Red.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        e.Graphics.FillRectangle(Brushes.Red, R)
    End Sub

    Private Sub Red_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Red.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(80, 18)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub Green_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Red.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(80, 18)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub Green_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Green.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        e.Graphics.FillRectangle(Brushes.Green, R)
    End Sub

    Private Sub Blue_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Blue.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(80, 18)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub Blue_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Blue.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        e.Graphics.FillRectangle(Brushes.Blue, R)
    End Sub

    Private Sub Yellow_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Yellow.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(80, 18)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub Yellow_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Yellow.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        e.Graphics.FillRectangle(Brushes.Yellow, R)
    End Sub

    Private Sub White_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles White.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(80, 18)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub White_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles White.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        e.Graphics.FillRectangle(Brushes.White, R)
    End Sub

    Private Sub Black_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Black.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(80, 18)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub Black_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Black.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        e.Graphics.FillRectangle(Brushes.Black, R)
    End Sub

    Private Sub AlignLeft_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignLeft.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(40, 14)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub AlignLeft_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignLeft.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        Dim strfmt As New StringFormat()
        strfmt.Alignment = StringAlignment.Near
        e.Graphics.DrawString("Left", Me.Font, Brushes.Black, R, strfmt)
    End Sub

    Private Sub AlignRight_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignRight.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(40, 14)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub AlignRight_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignRight.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        Dim strfmt As New StringFormat()
        strfmt.Alignment = StringAlignment.Far
        e.Graphics.DrawString("Right", Me.Font, Brushes.Black, R, strfmt)
    End Sub

    Private Sub AlignCenter_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignCenter.MeasureItem
        Dim itemSize As SizeF
        itemSize = New SizeF(40, 14)
        e.ItemHeight = itemSize.Height
        e.ItemWidth = itemSize.Width
    End Sub

    Private Sub AlignCenter_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignCenter.DrawItem
        Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        Dim strfmt As New StringFormat()
        strfmt.Alignment = StringAlignment.Center
        e.Graphics.DrawString("Center", Me.Font, Brushes.Black, R, strfmt)
    End Sub


    Private Sub Alignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlignCenter.Click, AlignLeft.Click, AlignRight.Click,Red.Click,Green.Click,Blue.Click,Yellow.Click,Black.Click,White.Click
        MessageBox.Show(sender.Text)
    End Sub
End Class

   
    
  








Related examples in the same category

1.MenuItem.Checked
2.MenuItem.Enabled
3.MenuItem.MeasureItem
4.MenuItem.MenuItems.Add
5.MenuItem.RadioCheck
6.MenuItem.Visible
7.Menuitem.Click