/// <summary>
/// 绘制菜单选项
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件</param>
private void MenuDrawItem( object sender , System.Windows.Forms.DrawItemEventArgs e )
{
//此项不被选择和鼠标不在此项时的背景色
Brush MouseOutBackColor = new SolidBrush( SystemColors.Control );
//鼠标经过和选择此项时的背景色
Brush MouseOverBackColor = new SolidBrush( Color.LightSteelBlue );
//字体和字体大小
Font font = new Font( "Ariel", 9 );
//字体和边框颜色
Brush MouseOverColor = new SolidBrush( System.Drawing.Color.Black );
//此项不被选择和鼠标不在此项时的边框色
Brush MouseOutColor = new SolidBrush( SystemColors.Control );
//绘制图型
Pen penMouseOver = new Pen( MouseOverColor );
Pen penMouseOut = new Pen( MouseOutColor ,1 );
//绘制边框大小
Rectangle rc = new Rectangle( e.Bounds.X+1 , e.Bounds.Y+1, e.Bounds.Width-2, e.Bounds.Height-2 );
//填充和格式化文本
e.Graphics.FillRectangle( MouseOutBackColor , rc );
MenuItem mOrigin = ( MenuItem )sender ;
string mItemText = mOrigin.Text ;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center ;
e.Graphics.DrawString( mItemText , font, MouseOverColor , rc , sf );
//判断是否为选择和鼠标在此项上
if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.Selected ) || e.State == ( DrawItemState.NoAccelerator | DrawItemState.HotLight ) )
{
e.Graphics.FillRectangle( MouseOverBackColor, rc );
e.Graphics.DrawString( mItemText , font ,MouseOverColor, rc ,sf );
e.Graphics.DrawRectangle( penMouseOver, rc );
}
else
{
e.Graphics.FillRectangle( MouseOutBackColor , rc );
e.Graphics.DrawString( mItemText , font ,MouseOverColor, rc ,sf );
e.Graphics.DrawRectangle( penMouseOut, rc );
}
e.DrawFocusRectangle();
}
/// <summary>
/// 设置菜单选项的高宽
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件</param>
private void MenuMeasureItem( object sender, System.Windows.Forms.MeasureItemEventArgs e )
{
//高度
e.ItemHeight = 20;
//宽度
e.ItemWidth = 100;
}
/// 绘制菜单选项
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件</param>
private void MenuDrawItem( object sender , System.Windows.Forms.DrawItemEventArgs e )
{
//此项不被选择和鼠标不在此项时的背景色
Brush MouseOutBackColor = new SolidBrush( SystemColors.Control );
//鼠标经过和选择此项时的背景色
Brush MouseOverBackColor = new SolidBrush( Color.LightSteelBlue );
//字体和字体大小
Font font = new Font( "Ariel", 9 );
//字体和边框颜色
Brush MouseOverColor = new SolidBrush( System.Drawing.Color.Black );
//此项不被选择和鼠标不在此项时的边框色
Brush MouseOutColor = new SolidBrush( SystemColors.Control );
//绘制图型
Pen penMouseOver = new Pen( MouseOverColor );
Pen penMouseOut = new Pen( MouseOutColor ,1 );
//绘制边框大小
Rectangle rc = new Rectangle( e.Bounds.X+1 , e.Bounds.Y+1, e.Bounds.Width-2, e.Bounds.Height-2 );
//填充和格式化文本
e.Graphics.FillRectangle( MouseOutBackColor , rc );
MenuItem mOrigin = ( MenuItem )sender ;
string mItemText = mOrigin.Text ;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center ;
e.Graphics.DrawString( mItemText , font, MouseOverColor , rc , sf );
//判断是否为选择和鼠标在此项上
if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.Selected ) || e.State == ( DrawItemState.NoAccelerator | DrawItemState.HotLight ) )
{
e.Graphics.FillRectangle( MouseOverBackColor, rc );
e.Graphics.DrawString( mItemText , font ,MouseOverColor, rc ,sf );
e.Graphics.DrawRectangle( penMouseOver, rc );
}
else
{
e.Graphics.FillRectangle( MouseOutBackColor , rc );
e.Graphics.DrawString( mItemText , font ,MouseOverColor, rc ,sf );
e.Graphics.DrawRectangle( penMouseOut, rc );
}
e.DrawFocusRectangle();
}
/// <summary>
/// 设置菜单选项的高宽
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件</param>
private void MenuMeasureItem( object sender, System.Windows.Forms.MeasureItemEventArgs e )
{
//高度
e.ItemHeight = 20;
//宽度
e.ItemWidth = 100;
}
Menu菜单的OwnerDraw属性要设为true,然后在每个选项的:DrawItem和MeasureItem事件加入:
this.menuItem1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.MenuDrawItem);
this.menuItem1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.MenuMeasureItem);
其中menuItem1为Menu的Name。
效果如图: