本文整理汇总了C#中System.Windows.Forms.MenuItem.OwnerDraw属性的典型用法代码示例。如果您正苦于以下问题:C# MenuItem.OwnerDraw属性的具体用法?C# MenuItem.OwnerDraw怎么用?C# MenuItem.OwnerDraw使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.MenuItem
的用法示例。
在下文中一共展示了MenuItem.OwnerDraw属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitializeMenu
// Declare the MainMenu control.
internal System.Windows.Forms.MainMenu MainMenu1;
// Declare MenuItem2 as With-Events because it will be user drawn.
internal System.Windows.Forms.MenuItem MenuItem2;
private void InitializeMenu()
{
// Create MenuItem1, which will be drawn by the operating system.
MenuItem MenuItem1 = new MenuItem("Regular Menu Item");
// Create MenuItem2.
MenuItem2 = new MenuItem("Custom Menu Item");
// Set OwnerDraw property to true. This requires handling the
// DrawItem event for this menu item.
MenuItem2.OwnerDraw = true;
//Add the event-handler delegate to handle the DrawItem event.
MenuItem2.DrawItem += new DrawItemEventHandler(DrawCustomMenuItem);
// Add the items to the menu.
MainMenu1 = new MainMenu(new MenuItem[]{MenuItem1, MenuItem2});
// Add the menu to the form.
this.Menu = this.MainMenu1;
}
// Draw the custom menu item.
private void DrawCustomMenuItem(object sender,
DrawItemEventArgs e)
{
// Cast the sender to MenuItem so you can access text property.
MenuItem customItem = (MenuItem) sender;
// Create a Brush and a Font to draw the MenuItem.
System.Drawing.Brush aBrush = System.Drawing.Brushes.DarkMagenta;
Font aFont = new Font("Garamond", 10,
FontStyle.Italic, GraphicsUnit.Point);
// Get the size of the text to use later to draw an ellipse
// around the item.
SizeF stringSize = e.Graphics.MeasureString(
customItem.Text, aFont);
// Draw the item and then draw the ellipse.
e.Graphics.DrawString(customItem.Text, aFont,
aBrush, e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Black, 2),
new Rectangle(e.Bounds.X, e.Bounds.Y,
(System.Int32)stringSize.Width,
(System.Int32)stringSize.Height));
}