作者:初夏mx | 来源:互联网 | 2023-10-12 18:00
如何更改MenuStrip上此白线的颜色?我不知道要使用什么属性。
到目前为止,我正在使用以下代码设置所有其他部分的样式:
internal static class Clr
{
public static Color White = Color.FromArgb(255,255,255);
public static Color Grey64 = Color.FromArgb(64,64,64);
public static Color Grey86 = Color.FromArgb(86,86,86);
public static Color Grey127 = Color.FromArgb(127,127,127);
public static Color Red = Color.FromArgb(255,0);
public static Color Blue = Color.FromArgb(0,255);
public static Color Green = Color.FromArgb(0,0);
}
public class Grey64Menu
{
public void ConfigureMenu(ToolStrip toolStrip)
{
toolStrip.Renderer = new ToolStripProfessionalRenderer(new Grey64ClrTable());
foreach (var topLevelItem in toolStrip.Items)
{
ToolStripMenuItem mainItem = (ToolStripMenuItem)topLevelItem;
mainItem.ForeColor = Clr.White;
mainItem.BackColor = Clr.Grey64;
foreach (var itm in mainItem.DropDownItems)
{
ToolStripMenuItem m = (ToolStripMenuItem)itm;
m.ForeColor = Clr.White;
m.BackColor = Clr.Grey64;
}
}
}
}
public class Grey64ClrTable : ProfessionalColorTable
{
public override Color MenuBorder => Clr.Grey86;
public override Color MenuItemBorder => Clr.Grey127;
public override Color MenuStripGradientBegin => Clr.Red;
public override Color MenuStripGradientEnd => Clr.Red;
public override Color ToolStripGradientBegin => Clr.Red;
public override Color ToolStripGradientEnd => Clr.Red;
public override Color ToolStripBorder => Clr.Blue;
//Dropdown Border Color
public override Color ToolStripDropDownBackground => Clr.Grey64;
public override Color MenuItemSelected => Clr.Grey86;
public override Color MenuItemSelectedGradientBegin => Clr.Grey86;
public override Color MenuItemSelectedGradientEnd => Clr.Grey86;
public override Color MenuItempressedGradientBegin => Clr.Grey86;
public override Color MenuItempressedGradientEnd => Clr.Grey86;
}
使用自定义ToolStripProfessionalRenderer的另一种方法可以覆盖OnRenderItemText并消除不考虑子项的初始循环(foreach (var topLevelItem in toolStrip.Items)
)。
还应该设置ImageMarginGradientMiddle
,否则在添加子项时会有奇怪的结果。您也应该覆盖其他中间部分。
您可以将公共属性添加到自定义渲染器,以在需要时更改菜单文本ForeColor。
public class Grey64Menu
{
public Grey64Menu() : this(null) { }
public Grey64Menu(ToolStrip menu) {
if (menu != null) {
ConfigureMenu(menu);
}
}
public void ConfigureMenu(ToolStrip toolStrip)
{
toolStrip.Renderer = new MyMenuRenderer();
}
}
public class MyMenuRenderer : ToolStripProfessionalRenderer
{
public MyMenuRenderer() : this(new Grey64ClrTable()) { }
public MyMenuRenderer(ProfessionalColorTable colorTable) : base(colorTable) { }
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
e.Item.ForeColor = Clr.White;
base.OnRenderItemText(e);
}
}
public class Grey64ClrTable : ProfessionalColorTable
{
// (...)
// Fill the Image area: ImageMarginGradientMiddle is required for sub-items
public override Color ImageMarginGradientMiddle => Clr.Grey64;
public override Color ImageMarginGradientBegin => Clr.Grey64;
public override Color ImageMarginGradientEnd => Clr.Grey64;
}
,
Per Hans Passant的评论:
该行是图像边距的背景色。因此,我可以通过将以下两行添加到我的ProfessionalColorTable中来解决此问题:
public override Color ImageMarginGradientBegin => Clr.Grey64;
public override Color ImageMarginGradientEnd => Clr.Grey64;
谢谢你,汉斯·帕桑特
,
您可以通过创建自己的ColorTable并覆盖您希望更改其颜色的属性来实现:
public class TestColorTable : ProfessionalColorTable
{
public override Color MenuItemSelected
{
get { return Color.Red; }
}
public override Color MenuBorder
{
get { return Color.Green; }
}
}
您将像这样使用它:
private void Form1_Load(object sender,EventArgs e)
{
menuStrip1.Renderer = new ToolStripProfessionalRenderer(new TestColorTable());
}