WinformMenuStrip更改选择背景⾊
⼀、⾃定义MenuStrip render
项⽬时,觉得⾃带菜单项选中颜⾊与项⽬不匹配,想要更改,多⽅查询总结出⽅法。
初始化时 将der=new mylfrender();设置为⾃⼰重写的render类。
public class CustomProfessionalRenderer:ToolStripProfessionalRenderer
{
Font textFont = new System.Drawing.Font("微软雅⿊", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
private Color menuItemSelectedColor = Color.Gray;
private Color menuItemBorderColor = Color.Black;
/// <summary>
/// Initialize a new instance of the Visual Studio MenuBarRenderer class.
/// </summary>
public CustomProfessionalRenderer()
: ba(new MenuBarColor())
{
}
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
e.TextFont = textFont;
ba.OnRenderItemText(e);
}
#region Backgrounds
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
if (e.Item.IsOnDropDown)
{
if (e.Item.Selected == true && e.Item.Enabled)
{
DrawMenuDropDownItemHighlight(e);
}
}
el
{
ba.OnRenderMenuItemBackground(e);
}
}
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
ToolStrip toolStrip = e.ToolStrip;
双排脚手架图片Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿
Rectangle bounds = e.AffectedBounds;
LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(0, toolStrip.Height), Color.FromArgb(200, Color.FromArgb(48,61,69) if (toolStrip is MenuStrip)
{
//由menuStrip的Paint⽅法定义这⾥不做操作
}
el if (toolStrip is ToolStripDropDown)
{
微笑着面对生活
胆囊炎的原因int diameter = 10;//直径
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(Point.Empty, toolStrip.Size);
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
path.AddLine(0, 0, 10, 0);
// 右上⾓
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下⾓
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
// 左下⾓
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloFigure();
toolStrip.Region = new Region(path);
g.FillPath(lgbrush, path);
}
el
{
ba.OnRenderToolStripBackground(e);
}
}
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
// ba.OnRenderImageMargin(e);
}
碗粿
#endregion
#region DrawMenuDropDownItemHighlight
private void DrawMenuDropDownItemHighlight(ToolStripItemRenderEventArgs e)
{
Rectangle rect = new Rectangle();
rect = new Rectangle(2, 0, (int)e.Graphics.VisibleClipBounds.Width - 4, (int)e.Graphics.VisibleClipBounds.Height - 1);
using (SolidBrush brush = new SolidBrush(menuItemSelectedColor))
{
e.Graphics.FillRectangle(brush, rect);
}
using (Pen pen = new Pen(menuItemBorderColor))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
#endregion
}
public class MenuBarColor : ProfessionalColorTable
{
Color ManuBarCommonColor = Color.LightSteelBlue;
Color SelectedHighlightColor = Color.FromArgb(253, 244, 191);
Color MenuBorderColor = Color.White;
Color MenuItemBorderColor = Color.FromArgb(48,61,69);
Color MenuStripbg = Color.Transparent;
/// <summary>
/// Initialize a new instance of the Visual Studio MenuBarColor class.
/// </summary>
public MenuBarColor()
{
}
#region
/ <summary>
/ 获取选中除顶级 System.Windows.Forms.ToolStripMenuItem 之外的 System.Windows.Forms.ToolStripMenuItem 时,要使⽤的纯⾊。 / </summary>
//public override Color MenuItemSelected
//{
// get
// {
// return SelectedHighlightColor;
// }
//}
/// <summary>
/// 被按下时使⽤的渐变的开始颜⾊
/// </summary>
public override Color MenuItemPresdGradientBegin {
qq通讯录同步助手get
{
return MenuStripbg;
}
}
/// <summary>
/// 被按下时使⽤的渐变的结束颜⾊
/// </summary>
public override Color MenuItemPresdGradientEnd {
get
{
return MenuStripbg;
}
}
/// <summary>
/// 被按下时使⽤的渐变的中间颜⾊
/// </summary>
public override Color MenuItemPresdGradientMiddle {
get
{
return MenuStripbg;
}
}
/// <summary>
/// 获取按钮被选定时使⽤的纯⾊
/// </summary>
public override Color ButtonSelectedHighlight
{
get
{
return ManuBarCommonColor;
}
}
/
// <summary>
/// 被选定时使⽤的渐变的开始颜⾊
/// </summary>
public override Color MenuItemSelectedGradientBegin {
get
{
return ManuBarCommonColor;
}
}
/// <summary>
/// 被选定时使⽤的渐变的结束颜⾊
/
// </summary>
public override Color MenuItemSelectedGradientEnd {
get
{
return ManuBarCommonColor;
}
}
/// <summary>
/// MenuStrip 上使⽤的边框颜⾊
/// </summary>
/// </summary>
public override Color MenuBorder
{
区域游戏
get
{
return MenuBorderColor;
食安法}
}
/// <summary>
/// ToolStripMenuItem 的边框颜⾊
/// </summary>
public override Color MenuItemBorder {
get
{
return MenuItemBorderColor;
一类生字
}
}
#endregion
}