请问菜单项的OwnerDraw = true后,如何为其添加Text标题?

yefeng238 2003-08-23 05:23:41
请问菜单项的OwnerDraw = true后,如何为其添加Text标题?

用this.menuItem_Clear.Text ="???";已不起作用了。
注:己通过Image.FromFile为菜单项添加了图标。
请指教,谢谢!
...全文
45 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2003-08-24
  • 打赏
  • 举报
回复
private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{

string myCaption = "Owner Draw Item1";

// Create a Brush and a Font with which to draw the item.
Brush myBrush = System.Drawing.Brushes.AliceBlue;
Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline, GraphicsUnit.Pixel);
SizeF mySizeF = e.Graphics.MeasureString(myCaption, myFont);

// Draw the item, and then draw a Rectangle around it.
e.Graphics.DrawString(myCaption, myFont, myBrush, e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(e.Bounds.X, e.Bounds.Y, Convert.ToInt32(mySizeF.Width), Convert.ToInt32(mySizeF.Height)));

}
yefeng238 2003-08-24
  • 打赏
  • 举报
回复
请问朋友,把OwnerDraw=true后,同时this.menuItem.shortcut和showshortcut也用不了了,应该如何设置它们的值?
谢谢!
8789 2003-08-23
  • 打赏
  • 举报
回复
這樣:
Rectangle rc = new Rectangle ( e.Bounds.X + 1 , e.Bounds.Y + 1 , e.Bounds.Width - 5 , e.Bounds.Height - 1 );
MenuItem mnuItem = (MenuItem) sender;
string sMenu = mnuItem.Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString ( sMenu , new Font ( "Ariel" , 9 ) , new SolidBrush ( Color.Black ) , rc ,sf );

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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