自己做出VB.NET风格的右键菜单


自己做出VB.NET风格的右键菜单(简单,实用)

  此主题相关图片如下:class MyMenuItem : System.Windows.Forms.MenuItem

以下是引用片段:
{ 
publicMyMenuItem() 
{ 
//这里很重要,必须把OwerDraw设为true,这样可以自己画菜单,否则便是让操作系统画菜单了,默认的是false 
this.OwnerDraw=true; 
} 
protectedoverridevoidOnDrawItem(SysDrawItemEventArgse) 
{ 
//要重画菜单,是没有OnPaint方法重载的,只有重载OnDrawItem方法! 
Graphicsg=e.Graphics; 
g.SmoothingMode=SmoothingMode.AntiAlias;//抗锯齿 
Fontf=newFont(FontFamily.GenericSerif,12,FontStyle.Regular,GraphicsUnit.Pixel);//设定菜单的字体 
Penp=newPen(Color.Navy,1);//这是画边框的字体 
if(e.State==DrawItemState.NoAccelerator)//一开始右键单击出现菜单,但是鼠标并没有移上去 
{//用白色的底色 
g.FillRectangle(Brushes.WhiteSmoke,e.Bounds.X-2,e.Bounds.Y-2,121,23); 
} 
//鼠标移上去,但是并没有单击 
if((e.State&DrawItemState.Selected)==DrawItemState.Selected) 
{ 
//花边框和底色 
g.FillRectangle(Brushes.LightSteelBlue,e.Bounds.X,e.Bounds.Y,109,20); 
g.DrawLine(p,e.Bounds.X,e.Bounds.Y,e.Bounds.X,e.Bounds.Y+19); 
g.DrawLine(p,e.Bounds.X,e.Bounds.Y+19,e.Bounds.X+109,e.Bounds.Y+19); 
g.DrawLine(p,e.Bounds.X+109,e.Bounds.Y+19,e.Bounds.X+109,e.Bounds.Y); 
g.DrawLine(p,e.Bounds.X+109,e.Bounds.Y,e.Bounds.X,e.Bounds.Y); 
} 
//显示文字 
g.DrawString(this.Text,f,Brushes.Black,e.Bounds.X,e.Bounds.Y); 
g.Dispose(); 
} 
//这是很重要的,这给你的菜单定义了大小,高20,宽100,否则你的菜单什么也看不到 
protectedoverridevoidOnMeasureItem(MeasureItemEventArgse) 
{ 
e.ItemHeight=20; 
e.ItemWidth=100; 
} 
}
  说明:这里我没有画按钮按下时的样子(懒:),主要是以后进一步改进),当然也没有画图标,也是为了以后改进,这只是一个初步的形态,大家看看有什么更高的方法?!

 

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3