[Q] OwnerDraw Menu - Large Font - MainMenuBar thickness

S

Stuart Norris

Dear Group,

I am new to c# and windows form designer - coming from a Motif
background.

I am attempting to develop an application for a touch screen and I
need to have a menu system with a larger font to allow ease of
selection.

I have used OwnerDraw menus implementing the DrawItemEventHandler and
MeasureItemEventHandler event handlers.

My attached sample works for the menus, however the MainMenubar
thickness is still the default thickness.

How do I change the Main menu bar thickness? So I can use larger
fonts for my application.

Thanks

Stuart

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

namespace bigmenu
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem MenuMain;
private System.Windows.Forms.MenuItem MenuItemOne;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu = new System.Windows.Forms.MainMenu();
this.MenuMain = new System.Windows.Forms.MenuItem();
this.MenuItemOne = new System.Windows.Forms.MenuItem();
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.MenuMain});
//
// MenuMain
//
this.MenuMain.DefaultItem = true;
this.MenuMain.Index = 0;
this.MenuMain.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.MenuItemOne});
this.MenuMain.OwnerDraw = true;
this.MenuMain.Text = "Menu One";
this.MenuMain.DrawItem += new
System.Windows.Forms.DrawItemEventHandler(this.MenuItemDrawItem);
this.MenuMain.MeasureItem += new
System.Windows.Forms.MeasureItemEventHandler(this.MenuItemMeasureItem);
//
// MenuItemOne
//
this.MenuItemOne.Index = 0;
this.MenuItemOne.OwnerDraw = true;
this.MenuItemOne.Text = "MenuItem One";
this.MenuItemOne.DrawItem += new
System.Windows.Forms.DrawItemEventHandler(this.MenuItemDrawItem);
this.MenuItemOne.MeasureItem += new
System.Windows.Forms.MeasureItemEventHandler(this.MenuItemMeasureItem);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Menu = this.mainMenu;
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void MenuItemMeasureItem (Object sender,
MeasureItemEventArgs e)
{
MenuItem menuItem = (MenuItem) sender;
Font font = new System.Drawing.Font("Microsoft Sans Serif", 18.0F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
// Determine the size menuItem in the new font.
SizeF stringSize = e.Graphics.MeasureString(menuItem.Text, font);
// Set the height and width of the item
e.ItemHeight = (int)stringSize.Height;
e.ItemWidth = (int)stringSize.Width;
}

private void MenuItemDrawItem( Object sender, DrawItemEventArgs e)
{
MenuItem menuItem = (MenuItem) sender;
System.Drawing.Brush brush = System.Drawing.Brushes.Black;
Font font = new System.Drawing.Font("Microsoft Sans Serif", 18.0F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
SizeF stringSize = e.Graphics.MeasureString(font.Name, font);
// Highlight the rectangle and the menu head if needed
if (( e.State & DrawItemState.Selected ) == DrawItemState.Selected)
e.Graphics.FillRectangle(SystemBrushes.Highlight,e.Bounds);
else
e.Graphics.FillRectangle(SystemBrushes.Menu,e.Bounds);
// Finally Draw the string on the screen.
e.Graphics.DrawString(menuItem.Text, font,brush,e.Bounds.X,
e.Bounds.Y);

}
}
}
 
S

Shakir Hussain

If you add your menu inside a ReBar control (like menus in internet explorer
or developer studio), then you can set the height of rebar control to
watever you want.

Shak.


Stuart Norris said:
Dear Group,

I am new to c# and windows form designer - coming from a Motif
background.

I am attempting to develop an application for a touch screen and I
need to have a menu system with a larger font to allow ease of
selection.

I have used OwnerDraw menus implementing the DrawItemEventHandler and
MeasureItemEventHandler event handlers.

My attached sample works for the menus, however the MainMenubar
thickness is still the default thickness.

How do I change the Main menu bar thickness? So I can use larger
fonts for my application.

Thanks

Stuart

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

namespace bigmenu
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem MenuMain;
private System.Windows.Forms.MenuItem MenuItemOne;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu = new System.Windows.Forms.MainMenu();
this.MenuMain = new System.Windows.Forms.MenuItem();
this.MenuItemOne = new System.Windows.Forms.MenuItem();
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.MenuMain});
//
// MenuMain
//
this.MenuMain.DefaultItem = true;
this.MenuMain.Index = 0;
this.MenuMain.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.MenuItemOne});
this.MenuMain.OwnerDraw = true;
this.MenuMain.Text = "Menu One";
this.MenuMain.DrawItem += new
System.Windows.Forms.DrawItemEventHandler(this.MenuItemDrawItem);
this.MenuMain.MeasureItem += new
System.Windows.Forms.MeasureItemEventHandler(this.MenuItemMeasureItem);
//
// MenuItemOne
//
this.MenuItemOne.Index = 0;
this.MenuItemOne.OwnerDraw = true;
this.MenuItemOne.Text = "MenuItem One";
this.MenuItemOne.DrawItem += new
System.Windows.Forms.DrawItemEventHandler(this.MenuItemDrawItem);
this.MenuItemOne.MeasureItem += new
System.Windows.Forms.MeasureItemEventHandler(this.MenuItemMeasureItem);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Menu = this.mainMenu;
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void MenuItemMeasureItem (Object sender,
MeasureItemEventArgs e)
{
MenuItem menuItem = (MenuItem) sender;
Font font = new System.Drawing.Font("Microsoft Sans Serif", 18.0F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
// Determine the size menuItem in the new font.
SizeF stringSize = e.Graphics.MeasureString(menuItem.Text, font);
// Set the height and width of the item
e.ItemHeight = (int)stringSize.Height;
e.ItemWidth = (int)stringSize.Width;
}

private void MenuItemDrawItem( Object sender, DrawItemEventArgs e)
{
MenuItem menuItem = (MenuItem) sender;
System.Drawing.Brush brush = System.Drawing.Brushes.Black;
Font font = new System.Drawing.Font("Microsoft Sans Serif", 18.0F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
SizeF stringSize = e.Graphics.MeasureString(font.Name, font);
// Highlight the rectangle and the menu head if needed
if (( e.State & DrawItemState.Selected ) == DrawItemState.Selected)
e.Graphics.FillRectangle(SystemBrushes.Highlight,e.Bounds);
else
e.Graphics.FillRectangle(SystemBrushes.Menu,e.Bounds);
// Finally Draw the string on the screen.
e.Graphics.DrawString(menuItem.Text, font,brush,e.Bounds.X,
e.Bounds.Y);

}
}
}
 
S

Stuart Norris

Hi All,

Just a quick followup on this.

Is the only way to change the height of the main menubar using a third
party package.

How do they do it? Do they draw the menus completely differently.

Thanks

Stuie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top