讲解:自己做出VB.NET风格的右键菜单 |
|
www.nanhushi.com 佚名 不详 |
自己做出VB.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; } } |
说明:这里我没有画按钮按下时的样子(懒:),主要是以后进一步改进),当然也没有画图标,也是为了以后改进,这只是一个初步的形态,大家看看有什么更高的方法?!
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: 建立、改变及重构Access数据库 下一篇文章: 利用EM_LINESCROLL信息控制TextBox的卷动 |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|