Owner Drawn Menu : MenuItem « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » MenuItemScreenshots 
Owner Drawn Menu
Owner Drawn Menu

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace OwnerDrawnMenu
{
  /// <summary>
  /// Summary description for OwnerDrawnMenu.
  /// </summary>
  public class OwnerDrawnMenu : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.ImageList imgMenu;
    internal System.Windows.Forms.MainMenu mainMenu1;
    internal System.Windows.Forms.MenuItem mnuFile;
    internal System.Windows.Forms.MenuItem mnuNew;
    internal System.Windows.Forms.MenuItem mnuOpen;
    internal System.Windows.Forms.MenuItem mnuSave;
    private System.ComponentModel.IContainer components;

    public OwnerDrawnMenu()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(OwnerDrawnMenu));
      this.imgMenu = new System.Windows.Forms.ImageList(this.components);
      this.mainMenu1 = new System.Windows.Forms.MainMenu();
      this.mnuFile = new System.Windows.Forms.MenuItem();
      this.mnuNew = new System.Windows.Forms.MenuItem();
      this.mnuOpen = new System.Windows.Forms.MenuItem();
      this.mnuSave = new System.Windows.Forms.MenuItem();
      // 
      // imgMenu
      // 
      this.imgMenu.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imgMenu.ImageSize = new System.Drawing.Size(1616);
      this.imgMenu.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgMenu.ImageStream")));
      this.imgMenu.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // mainMenu1
      // 
      this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuFile});
      // 
      // mnuFile
      // 
      this.mnuFile.Index = 0;
      this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                          this.mnuNew,
                                          this.mnuOpen,
                                          this.mnuSave});
      this.mnuFile.Text = "File";
      // 
      // mnuNew
      // 
      this.mnuNew.Index = 0;
      this.mnuNew.OwnerDraw = true;
      this.mnuNew.Text = "New";
      this.mnuNew.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.mnu_DrawItem);
      this.mnuNew.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.mnu_MeasureItem);
      // 
      // mnuOpen
      // 
      this.mnuOpen.Index = 1;
      this.mnuOpen.OwnerDraw = true;
      this.mnuOpen.Text = "Open";
      this.mnuOpen.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.mnu_DrawItem);
      this.mnuOpen.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.mnu_MeasureItem);
      // 
      // mnuSave
      // 
      this.mnuSave.Index = 2;
      this.mnuSave.OwnerDraw = true;
      this.mnuSave.Text = "Save";
      this.mnuSave.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.mnu_DrawItem);
      this.mnuSave.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.mnu_MeasureItem);
      // 
      // OwnerDrawnMenu
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292266);
      this.Menu = this.mainMenu1;
      this.Name = "OwnerDrawnMenu";
      this.Text = "Owner-Drawn Menu";

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new OwnerDrawnMenu());
    }

    private void mnu_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    {
      // Retrieve current item.
      MenuItem mnuItem = (MenuItem)sender;

      Font menuFont = new Font("Tahoma"8);

      // Measure size needed to display text.
      // We add 30 pixels to the width to allow a generous spacing for the image.
      e.ItemHeight = (int)e.Graphics.MeasureString(mnuItem.Text, menuFont).Height + 5;
            e.ItemWidth = (int)e.Graphics.MeasureString(mnuItem.Text, menuFont).Width + 30;
    }

    private void mnu_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
      // Retrieve current item.
      MenuItem mnuItem = (MenuItem)sender;
        
      // This defaults to the highlighted background if selected.
      e.DrawBackground();
            
      // Retrieve the image from an ImageList control.
      Image menuImage = imgMenu.Images[mnuItem.Index];
      
      // Draw the image.
      e.Graphics.DrawImage(menuImage, e.Bounds.Left + 3, e.Bounds.Top + 2);
      
      // Draw the text with the supplied colors and in the set region.
      e.Graphics.DrawString(mnuItem.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + 25, e.Bounds.Top + 3);
    }
  }
}


           
       
OwnerDrawnMenu.zip( 28 k)
Related examples in the same category
1.new MenuItem
2.Creating a menu on the fly.
3.Set MenuItem ShortCut
4.Font Menu
5.Subclass MenuItem
6.RadioCheck MenuItem
7.Hatch Brush Menu
8.Owner Drawn Menu ControlOwner Drawn Menu Control
9.Add Help text for MenuItem
10.Help Menu
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.