需要自定义菜单项的外观,有三步:
1)将需要自定义外观的MenuItem的OwnerDraw设为true
2)添加MenuItem.DrawItem事件的处理函数
3)添加MenuItem.MeasureItem事件的处理函数
4)that's ok!!
注意事项:
当OwnerDraw的属性为true时,必须同时添加上面讲到的两个事件处理函数,缺一不可!
下面是两个处理函数的例子,enjoy yourself:
private void menuItem7_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Icon icon = new Icon("house.ico");
//e.Graphics.DrawString("hahaha",e.Font,e.b
//this.textBox1.Text+=e.Bounds.Location;
//this.textBox1.Text+=sender.GetType();
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Ivory),e.Bounds.Location.X,e.Bounds.Location.Y,e.Bounds.Width,e.Bounds.Height);
e.Graphics.DrawIcon(icon,e.Bounds.Location.X,e.Bounds.Location.Y);
e.Graphics.DrawString("hello",new System.Drawing.Font("arial",12),new System.Drawing.SolidBrush(System.Drawing.Color.Chocolate),e.Bounds.Location.X+22,e.Bounds.Location.Y);
this.menuItem7.Visible=true;
} private void menuItem7_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
//this.textBox1.Text+=e.ItemHeight+"\r\n"; //this.textBox1.Text+=e.ItemWidth+"\r\n";
e.ItemHeight=21;
e.ItemWidth=140;
}
1)将需要自定义外观的MenuItem的OwnerDraw设为true
2)添加MenuItem.DrawItem事件的处理函数
3)添加MenuItem.MeasureItem事件的处理函数
4)that's ok!!
注意事项:
当OwnerDraw的属性为true时,必须同时添加上面讲到的两个事件处理函数,缺一不可!
下面是两个处理函数的例子,enjoy yourself:
private void menuItem7_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Icon icon = new Icon("house.ico");
//e.Graphics.DrawString("hahaha",e.Font,e.b
//this.textBox1.Text+=e.Bounds.Location;
//this.textBox1.Text+=sender.GetType();
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Ivory),e.Bounds.Location.X,e.Bounds.Location.Y,e.Bounds.Width,e.Bounds.Height);
e.Graphics.DrawIcon(icon,e.Bounds.Location.X,e.Bounds.Location.Y);
e.Graphics.DrawString("hello",new System.Drawing.Font("arial",12),new System.Drawing.SolidBrush(System.Drawing.Color.Chocolate),e.Bounds.Location.X+22,e.Bounds.Location.Y);
this.menuItem7.Visible=true;
} private void menuItem7_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
//this.textBox1.Text+=e.ItemHeight+"\r\n"; //this.textBox1.Text+=e.ItemWidth+"\r\n";
e.ItemHeight=21;
e.ItemWidth=140;
}
菜单是应该完全手绘吗?如果不是那我怎么在一个Form里面放两个MainMenu呢?如果是的话,那么数据结构应该怎么搞呢,我怎么才能知道那是兄弟菜单项,哪个又是子菜单项?盼知情者速速告之,小女感激不尽!!
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication
{
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.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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = 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.menuItem2});
this.menuItem1.Text = "自定义菜单";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.OwnerDraw = true;
this.menuItem2.Text = "";
this.menuItem2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem2_DrawItem);
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
this.menuItem2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem2_MeasureItem);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 270);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1"; }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void menuItem2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemWidth =70;
e.ItemHeight=70;
}
private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Pen mypen=new Pen(new SolidBrush(Color.Red ),3);
e.Graphics.DrawArc(mypen,new Rectangle(5,0,35,30),0,-180);
e.Graphics.DrawArc(mypen,new Rectangle(45,0,35,30),0,-180);
e.Graphics.DrawLine(mypen,new Point(43,30),new Point(43,45));
e.Graphics.DrawArc(mypen,new Rectangle(5,35,75,30),0,180);
} private void menuItem2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("你是MM吗?");
}
}
}
现在菜单项的确可以画得很漂亮,但是还有问题.一个问题是用户点击菜单项时应该让其输入菜单项的名字,这个怎么实现呢?另一个问题时,我做的是菜单编辑软件,在Form中已经有一个系统菜单了,我怎么再加入一个呀?(下面是我简单绘制的菜单,见笑见笑)
public class Form1 : System.Windows.Forms.Form
{
MainMenu mainMenu1;
MenuItem menuItem1;
MenuItem menuItem2;
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
AppStartMenu();
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
public void AppStartMenu()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2
});
mainMenu1.MenuItems .Add (menuItem1);
this.Menu =mainMenu1;
this.IsMdiContainer =true;
menuItem1.OwnerDraw =true;
menuItem2.OwnerDraw =true;
menuItem1.DrawItem +=new System.Windows .Forms.DrawItemEventHandler(this. menuItem_DrawItem);
menuItem1.MeasureItem +=new System.Windows .Forms.MeasureItemEventHandler(this.menuItem_MeasureItem);
menuItem2.DrawItem +=new System.Windows .Forms.DrawItemEventHandler(this. menuItem_DrawItem);
menuItem2.MeasureItem +=new System.Windows .Forms.MeasureItemEventHandler(this.menuItem_MeasureItem);
}
private void menuItem_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{ e.Graphics.DrawRectangle(new System.Drawing.Pen (System.Drawing.Color.DarkGray),e.Bounds.Location.X,e.Bounds.Location.Y,e.Bounds.Width-2,e.Bounds.Height-2);
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White ),e.Bounds.Location.X,e.Bounds.Location.Y,e.Bounds.Width-3,e.Bounds.Height-3);
//e.Graphics.DrawIcon(icon,e.Bounds.Location.X,e.Bounds.Location.Y);
e.Graphics.DrawString("请在此输入",new System.Drawing.Font("arial",9),new System.Drawing.SolidBrush(System.Drawing.Color.DarkGray ),e.Bounds.Location.X+6,e.Bounds.Location.Y);
this.menuItem1.Visible=true;
this.menuItem2.Visible =true;
} private void menuItem_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight=19;
e.ItemWidth=86;
}