Export (0) Print
Expand All

MenuItem.OwnerDraw Property

Gets or sets a value indicating whether the code that you provide draws the menu item or Windows draws the menu item.

[Visual Basic]
Public Property OwnerDraw As Boolean
[C#]
public bool OwnerDraw {get; set;}
[C++]
public: __property bool get_OwnerDraw();
public: __property void set_OwnerDraw(bool);
[JScript]
public function get OwnerDraw() : Boolean;
public function set OwnerDraw(Boolean);

Property Value

true if the menu item is to be drawn using code; false if the menu item is to be drawn by Windows. The default is false.

Remarks

When the OwnerDraw property is set to true, you need to handle all drawing of the menu item. You can use this capability to create your own special menu displays.

Example

The following code example demonstrates a menu with an owner-drawn menu item. This example uses the AddHandler statement and the AddressOf operator to designate a delegate to handle the DrawItem event.To run the example place paste it in a form that imports the System, System.Windows.Forms, and System.Drawing namespaces.

[SampleID='System.Windows.Forms.MenuItemOwnerDraw' SnippetID='1']
--------- Languages displayed= cs, vb ---------
--------- cs ---------
--------- Snippet 1 ---------
    // Declare the MainMenu control.
    internal System.Windows.Forms.MainMenu MainMenu1;
    // Declare MenuItem2 as With-Events because it will be user drawn.
    internal System.Windows.Forms.MenuItem MenuItem2;
    private void InitializeMenu()
    {
        // Create MenuItem1, which will be drawn by the operating system.
        MenuItem MenuItem1 = new MenuItem("Regular Menu Item");
        // Create MenuItem2.
        MenuItem2 = new MenuItem("Custom Menu Item");
        // Set OwnerDraw property to true. This requires handling the
        // DrawItem event for this menu item.
        MenuItem2.OwnerDraw = true;
        //Add the event-handler delegate to handle the DrawItem event.
        MenuItem2.DrawItem += new DrawItemEventHandler(DrawCustomMenuItem);
        
      // Add the items to the menu.
        MainMenu1 = new MainMenu(new MenuItem[]{MenuItem1, MenuItem2});                                                                                                                      
        // Add the menu to the form.
        this.Menu = this.MainMenu1;
    }
    // Draw the custom menu item.
    private void DrawCustomMenuItem(object sender, 
        DrawItemEventArgs e)
    {
        // Cast the sender to MenuItem so you can access text property.
        MenuItem customItem = (MenuItem) sender;
        // Create a Brush and a Font to draw the MenuItem.
        System.Drawing.Brush aBrush = System.Drawing.Brushes.DarkMagenta;
        Font aFont = new Font("Garamond", 10, 
            FontStyle.Italic, GraphicsUnit.Point);
        // Get the size of the text to use later to draw an ellipse
        // around the item.
        SizeF stringSize = e.Graphics.MeasureString(
            customItem.Text, aFont);
        // Draw the item and then draw the ellipse.
        e.Graphics.DrawString(customItem.Text, aFont, 
            aBrush, e.Bounds.X, e.Bounds.Y);
        e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Black, 2),
            new Rectangle(e.Bounds.X, e.Bounds.Y, 
            (System.Int32)stringSize.Width,
            (System.Int32)stringSize.Height));
    }
--------- vb ---------
--------- Snippet 1 ---------
    ' Declare the MainMenu control.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    ' Declare MenuItem2 as With-Events because it will be user drawn.
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Private Sub InitializeMenu()
        ' Create MenuItem1, which will be drawn by the operating system.
        Dim MenuItem1 As New MenuItem("Regular Menu Item")
        ' Create MenuItem2.
        MenuItem2 = New MenuItem("Custom Menu Item")
        ' Set OwnerDraw property to true. This requires handling the
        ' DrawItem event for this menu item.
        MenuItem2.OwnerDraw = True
        'Add the event-handler delegate to handle the DrawItem event.
        AddHandler MenuItem2.DrawItem, New DrawItemEventHandler(AddressOf DrawCustomMenuItem)
        ' Add the items to the menu.
        MainMenu1 = New MainMenu(New MenuItem() {MenuItem1, MenuItem2})
        ' Add the menu to the form.
        Me.Menu = Me.MainMenu1
    End Sub
    ' Draw the custom menu item.
    Private Sub DrawCustomMenuItem(ByVal sender As Object, ByVal e As _
            System.Windows.Forms.DrawItemEventArgs)
        ' Cast the sender to MenuItem so you can access text property.
        Dim customItem As MenuItem = CType(sender, MenuItem)
        ' Create a Brush and a Font to draw the MenuItem.
        Dim aBrush As System.Drawing.Brush = System.Drawing.Brushes.DarkMagenta
        Dim aFont As New Font("Garamond", 10, FontStyle.Italic, _
            GraphicsUnit.Point)
        ' Get the size of the text to use later to draw an ellipse
        ' around the item.
        Dim stringSize As SizeF = e.Graphics.MeasureString( _
            customItem.Text, aFont)
        ' Draw the item and then draw the ellipse.
        e.Graphics.DrawString(customItem.Text, aFont, _
            aBrush, e.Bounds.X, e.Bounds.Y)
        e.Graphics.DrawEllipse(New Pen(System.Drawing.Color.Black, 2), _
            New Rectangle(e.Bounds.X, e.Bounds.Y, stringSize.Width, _
            stringSize.Height))
      End Sub

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

MenuItem Class | MenuItem Members | System.Windows.Forms Namespace | Graphics | Menu

Show:
© 2015 Microsoft