1、需要你的菜单是完整的,就是说,在ide的designer中,要有定义好的菜单(当然,你程序中自动生成除外)
2、你需要ownerdraw的菜单,它的ownerdraw属性设置为true了吗?
3、measuerItem这个事件设置了吗?如:
MenuItem item = (MenuItem)sender;
string shortcut = GetShortcutName(item);
if (item.Text == "-")
{
e.ItemHeight = 4;
}
else
{
e.ItemHeight = SystemInformation.MenuHeight+4;
}
2、你需要ownerdraw的菜单,它的ownerdraw属性设置为true了吗?
3、measuerItem这个事件设置了吗?如:
MenuItem item = (MenuItem)sender;
string shortcut = GetShortcutName(item);
if (item.Text == "-")
{
e.ItemHeight = 4;
}
else
{
e.ItemHeight = SystemInformation.MenuHeight+4;
}
可不可以又完整的代码啊!?
我需要完整的菜单,只是全部都是自画的!
先设置菜单的那个ownerdraw(是不是这个?忘记)为真。private void menuItem_Start_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
// 设置菜单栏的高度和宽度
e.ItemHeight=20;
e.ItemWidth=80;
} private void menuItem_Start_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g=e.Graphics;
// 如果已经启动
if (bStart==false)
{
// 如果鼠标经过菜单栏,要把菜单画为蓝色,否则不变
if ((e.State & DrawItemState.Selected)==DrawItemState.Selected)
{
g.FillRectangle(new SolidBrush(Color.DarkBlue),e.Bounds.Left,e.Bounds.Top,e.Bounds.Width,e.Bounds.Height);
g.DrawString("启动 CTRL+S",this.Font,new SolidBrush(Color.White),20,2);
}
else
{
g.FillRectangle(new SolidBrush(Color.White),e.Bounds.Left,e.Bounds.Top,e.Bounds.Width,e.Bounds.Height);
g.DrawString("启动 CTRL+S",this.Font,new SolidBrush(Color.Black),20,2);
}
}
// 如果未启动,就把菜单画成灰色
else
{
g.FillRectangle(new SolidBrush(Color.White),e.Bounds.Left,e.Bounds.Top,e.Bounds.Width,e.Bounds.Height);
g.DrawString("启动 CTRL+S",this.Font,new SolidBrush(Color.Gray),20,2);
}
// 在菜单上画出图标和文字并释放资源
System.Drawing.Icon menuico=new Icon("start.ico");
g.DrawIcon(menuico,new Rectangle(0,0,16,16));
menuico.Dispose();
}
if (bStart==false)
这个是什么啊?!它报错,bStart不是知道是什么!?我做了个菜单但只是一个:下面弹出得菜单在选择OwnerDraw=true时,不知道怎么会事没有出来得!默认得就行!
代码:
private void menuItem_File_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g=e.Graphics;
if(e.State==DrawItemState.NoAccelerator) //刚开始时
{
g.FillRectangle(new SolidBrush(System.Drawing.SystemColors.Control),4,23,e.Bounds.Width,e.Bounds.Height);
//g.FillRectangle(new SolidBrush(System.Drawing.Color.LightSteelBlue),5,24,e.Bounds.Width-2,e.Bounds.Height-2);
g.DrawString("文件(F)",this.Font,new SolidBrush(Color.Black),6,26);
}
else
if((e.State & DrawItemState.Selected)==DrawItemState.Selected) //鼠标按下后
{
g.FillRectangle(new SolidBrush(Color.Black),4,23,e.Bounds.Width,e.Bounds.Height);
g.FillRectangle(new SolidBrush(System.Drawing.SystemColors.Control),5,24,e.Bounds.Width-2,e.Bounds.Height-1);
g.DrawString("文件(F)",this.Font,new SolidBrush(Color.Black),6,26);
}
else //鼠标放在上面
{
g.FillRectangle(new SolidBrush(Color.Black),4,23,e.Bounds.Width,e.Bounds.Height);
g.FillRectangle(new SolidBrush(System.Drawing.Color.LightSteelBlue),5,24,e.Bounds.Width-2,e.Bounds.Height-2);
g.DrawString("文件(F)",this.Font,new SolidBrush(Color.Black),6,26);
}
} private void menuItem_File_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight=16;
e.ItemWidth=38;
}