温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Trying to color a MenuStrip, a white border appears on the left side
.net-core-3.1 c# menustrip winforms

c# - 尝试为MenuStrip着色,左侧出现白色边框

发布于 2021-04-18 16:58:27

我正在创建一个表单,我希望菜单栏具有不同的颜色。关于这方面有很多帖子,我设法更改了所有颜色,除了菜单左侧的白色方块/线条之外。
我正在使用.Net Core 3.1 Windows窗体应用程序。

在此处输入图片说明

ExitToolstripMenuItem后面的白色块:使用分隔符时,它将变宽。

在此处输入图片说明

以上菜单上的白细线。

我正在使用专业渲染器来覆盖颜色。

public class DxColorTable : ToolStripProfessionalRenderer
{
    public DxColorTable(dynamic theme) : base(new DxCols(theme)) { }
}

public class DxCols : ProfessionalColorTable
{
    private readonly dynamic theme = DefaultTheme.Default;
    public DxCols(dynamic theme)
    {
        this.theme = theme;
    }

    public override Color MenuBorder
    {
        get { return this.theme.MenuSelectedColor; }
    }
    public override Color MenuItemBorder
    {
        get { return this.theme.MenuSelectedColor; }
    }
    public override Color MenuItemPressedGradientBegin
    {
        get { return this.theme.MenuSelectedColor; }
    }

    public override Color MenuItemPressedGradientEnd
    {
        get { return this.theme.MenuSelectedColor; }
    }
    public override Color MenuItemSelected
    {              
        get { return this.theme.MenuSelectedColor; }
    }
    public override Color MenuItemSelectedGradientBegin
    {            
        get { return this.theme.MenuSelectedColor; }
    }
    public override Color MenuItemSelectedGradientEnd
    {
        get { return this.theme.MenuSelectedColor; }
    }

    public override Color ToolStripBorder
    {
        get { return this.theme.MenuBackgroundColor; }
    }
    
    public override Color ToolStripDropDownBackground
    {
        get { return this.theme.MenuBackgroundColor; }
    }
    public override Color ToolStripGradientBegin
    {
        get { return this.theme.MenuBackgroundColor; }
    }
    public override Color ToolStripGradientEnd
    {
        get { return this.theme.MenuBackgroundColor; }
    }
    public override Color ToolStripGradientMiddle
    {
        get { return this.theme.MenuBackgroundColor; }
    }

    public override Color ToolStripContentPanelGradientBegin
    {
        get
        {
            return this.theme.MenuBackgroundColor; 
        }
    }

    public override Color ToolStripContentPanelGradientEnd
    {
        get
        {
            return this.theme.MenuBackgroundColor;
        }
    }
}

查看更多

提问者
Shaun Roach
被浏览
11
Jimi 2020-07-04 02:03

你忘记了覆盖定义图像边距区域的三个属性。
你需要为ImageMarginGradient零件指定一个Color值

当你添加ToolStripComboBox或ToolStripSeparator时,它特别明显。请注意,即使在设计器中已经设置了背景颜色,它也不会影响标准的ToolStripMenuItems,即使它们显示图像也是如此。

public override Color ImageMarginGradientBegin => this.theme.MenuBackgroundColor;
public override Color ImageMarginGradientMiddle => this.theme.MenuBackgroundColor;
public override Color ImageMarginGradientEnd => this.theme.MenuBackgroundColor; 

如果不需要显示图像,则可以隐藏图像边距区域:

([Your ToolStripMenuItem].DropDown as ToolStripDropDownMenu).ShowImageMargin = false;