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

发布时间:2007年04月24日      浏览次数:1043 次
自己做出VS.NET风格的右键菜单(简单,实用)
  此主题相关图片如下:class MyMenuItem : System.Windows.Forms.MenuItem
{
public MyMenuItem()
{
//这里很重要,必须把OwerDraw设为true,这样可以自己画菜单,否则便是让操作系统画菜单了,默认的是false
this.OwnerDraw=true;
}
protected override void OnDrawItem(SysDrawItemEventArgs e)
{
//要重画菜单,是没有OnPaint方法重载的,只有重载OnDrawItem方法!
Graphics g=e.Graphics;
g.SmoothingMode=SmoothingMode.AntiAlias;//抗锯齿
Font f = new Font(FontFamily.GenericSerif, 12, FontStyle.Regular, GraphicsUnit.Pixel);//设定菜单的字体
Pen p=new Pen(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,否则你的菜单什么也看不到
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight=20;
e.ItemWidth=100;
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!