How to draw custom fonts in a menu.

Menu fonts normally draw from the font provided by the system. You can get the font and discover its properties by getting the SystemInformation.MenuFont property.

When you don't want to draw a menu item with this font, there is no Font property that enables you to change the appearance of just one MenuItem object. This is only possible using a custom drawn menu.

To do this you need to provide handlers for the MenuItem.MeasureItem and MenuItem.DrawItem events raised by the specific menu item that you are customizing.

The application listing below shows how to draw custom font MenuItems. The handlers menuItem2_MeasureItem and menuItem2_DrawItem are the routines that do the work.

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

 

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

    {

    private System.Windows.Forms.MainMenu mainMenu1;

    private System.Windows.Forms.MenuItem menuItem1;

    private System.Windows.Forms.MenuItem menuItem2;

    private System.Windows.Forms.MenuItem menuItem3;

    private System.Windows.Forms.MenuItem menuItem4;

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

    

    public Form1()

    {

        //

        // 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 Dispose( bool disposing )

    {

        if( disposing )

        {

            if(components != null)

            {

                components.Dispose();

             }

        }

        base.Dispose( disposing );

    }

    

    #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.mainMenu1 = new System.Windows.Forms.MainMenu();

        this.menuItem1 = new System.Windows.Forms.MenuItem();

        this.menuItem4 = new System.Windows.Forms.MenuItem();

        this.menuItem2 = new System.Windows.Forms.MenuItem();

        this.menuItem3 = new System.Windows.Forms.MenuItem();

        // 

        // mainMenu1

        // 

        this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

        this.menuItem1});

        // 

        // menuItem1

        // 

        this.menuItem1.Index = 0;

        this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

        this.menuItem4,

        this.menuItem2,

        this.menuItem3});

        this.menuItem1.Text = "Menu";

        // 

        // menuItem4

        // 

        this.menuItem4.Index = 0;

        this.menuItem4.Text = "Ordinary item";

        // 

        // menuItem2

        // 

        this.menuItem2.Index = 1;

        this.menuItem2.OwnerDraw = true;

        this.menuItem2.Text = "Custom Font MenuItem";

        this.menuItem2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem2_DrawItem);

        this.menuItem2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem2_MeasureItem);

        // 

        // menuItem3

        // 

        this.menuItem3.Index = 2;

        this.menuItem3.Text = "Ordinary item";

        // 

        // Form1

        // 

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

        this.ClientSize = new System.Drawing.Size(292, 273);

        this.Menu = this.mainMenu1;

        this.Name = "Form1";

        this.Text = "Form1";

        this.Load += new System.EventHandler(this.Form1_Load);

        

    }

    #endregion

    

    [STAThread]

    public static void Main()

    {

        Application.Run(new Form1());

    }

    

    FontFamily ff; 

    Font TheFont; 

    

    private void Form1_Load(object sender, System.EventArgs e)

   

        ff = new FontFamily("BauHaus 93"); 

        TheFont = new Font(ff,14,FontStyle.Bold | FontStyle.Italic);

    }

    

    private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

    {

        SolidBrush b = new SolidBrush(e.ForeColor);

        e.DrawBackground(); //Draw the menu item background

         e.Graphics.DrawString(((MenuItem)sender).Text,TheFont,b,e.Bounds.Left+16,e.Bounds.Top+3,StringFormat.GenericTypographic);

        b.Dispose();

    }

 

    private void menuItem2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)

    {

        System.Drawing.SizeF s = e.Graphics.MeasureString(((MenuItem)sender).Text,TheFont,1024,StringFormat.GenericTypographic);

        s.Width+=SystemInformation.MenuCheckSize.Width; // for the checkmark if any

        s.Width+=30; // for the menu glyph if any

        e.ItemHeight=(int)s.Height;

        e.ItemWidth=(int)s.Width;

    }

}

 

     

When run, the  application menu items look like this...

 

Back to the Tips and Tricks page

Copyright Robert W. Powell 2003. All Rights Reserved.