Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)aaa
published in(发表于) 2013/12/15 8:54:45
讲解:自己做出VS.NET风格的右键菜单_.net资料_编程技术

讲解:自己做出VS.NET风格的右键菜单_.net资料_编程技术-你的首页-uuhomepage.com

  自己做出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;
}
}



If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759