Winforms MenuStrip - 如何更改下拉项左边缘的白线?

Winforms MenuStrip - How to change white line on left edge of dropdown items?

如何更改 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, 0);
    public static Color Blue = Color.FromArgb(0, 0, 255);
    public static Color Green = Color.FromArgb(0, 255, 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;
}

您可以通过创建自己的 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());
}

根据 Hans Passant 的评论:

该线是图像边距的背景色。所以我能够通过将这两行添加到我的 ProfessionalColorTable 来修复它:

public override Color ImageMarginGradientBegin => Clr.Grey64;
public override Color ImageMarginGradientEnd => Clr.Grey64;

谢谢你,汉斯·帕桑特

另一种方法,使用自定义 ToolStripProfessionalRenderer, to override OnRenderItemText 并消除不考虑子项的初始循环 (foreach (var topLevelItem in toolStrip.Items))。

此外,ImageMarginGradientMiddle也应该设置,否则添加子项时会出现奇怪的结果。您也应该覆盖其他中间部分。

您可以向自定义呈现器添加 public 属性,以便在需要时更改菜单文本 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;
}