如何做一个象VS.net界面菜单的控件?

abcynic 2002-08-05 10:47:38
也就是在非选中状态时菜单的底色是白色的,选中时MenuItem被一个蓝色矩形框包围的那种效果。我想用一个从MainMenu继承的类来实现,但后来发现MainMenu没有OnPaint可以重载。
高手给个实现的思路吧,如果有源代码更好!
...全文
47 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
The_Gathering 2002-09-16
  • 打赏
  • 举报
回复
www.codeproject.com有已经写好的控件
lonk 2002-08-05
  • 打赏
  • 举报
回复
怎么没有,不是onpain,是drawitem事件.你响应这个就可以了.
设置该菜单项,如menuItem1菜单项的OwnerDraw属性为true
响应measureitem事件,设置菜单项的高度和宽度,然后就可以
在drawitem事件里画了.

以前一篇不知道谁的文章里的示例代码,
用来实现加图标exit.bmp的菜单项menuItemExit,可供您参考:

private void menuItemExit_MeasureIte(objectsender,System.Windows.Forms.MeasureItemEventArgs e)

{ e.ItemHeight = 25; e.ItemWidth = 75; }

private void menuItemExit_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgs e)

{

Rectangle rc = new Rectangle(e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);

e.Graphics.FillRectangle(new SolidBrush(Color.White),rc);

MenuItem s = (MenuItem)sender;

string strItem = s.Text;

StringFormat sf = new StringFormat();

sf.Alignment = StringAlignment.Far;

sf.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(strItem,new Font("Veranda",10), new SolidBrush(Color.Blue),rc,sf);

e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.White)),rc);

if(e.State ==(DrawItemState.NoAccelerator | DrawItemState.Selected))

{

e.Graphics.FillRectangle(new SolidBrush(Color.CornflowerBlue),rc); e.Graphics.DrawString(strItem,new Font("Veranda",10,FontStyle.Bold|FontStyle.Underline),new SolidBrush(Color.Yellow),rc,sf);

e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Blue)),rc);

//Draws a focus rectangle within the bounds specified in theDrawItemEventArgsconstructor.

e.DrawFocusRectangle();

}

Image imgExit = new Bitmap("exit.bmp"); SizeF sz = imgExit.PhysicalDimension ; e.Graphics.DrawImage(imgExit,e.Bounds.X + 5,(e.Bounds.Bottom + e.Bounds.Top)/2 - sz.Height/2);

}

cometsky 2002-08-05
  • 打赏
  • 举报
回复
我以前从某个网站上下载了这么一个菜单控件,具体是哪了我忘了,有源代码(我没分析过),你要的话,通过短信息告诉我你的信箱。我发给你。

604

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 控件与界面
社区管理员
  • 控件与界面社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧