C# MenuStrip面板视觉异常

C# MenuStrip Panel visual anomalies

我正在为菜单栏着色以匹配我的应用程序主题。但是,每次尝试时,我都会在面板分隔符(白色矩形)的左侧看到这种奇怪的视觉异常。复选框的位置也有些奇怪(可能相关?)。

这是什么原因造成的,我该如何删除它?

谢谢

示例代码:

private Color MainColor = Color.FromArgb(39, 40, 34);

public class MenuStripRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
    {
        Rectangle rect = new Rectangle(Point.Empty, e.Item.Size);

        if (e.Item.Selected)
        {
            Color color = Color.FromArgb(24, 25, 19);
            using (SolidBrush brush = new SolidBrush(color))
                e.Graphics.FillRectangle(brush, rect);
        }
        else
        {
            using (SolidBrush brush = new SolidBrush(MainColor))
                e.Graphics.FillRectangle(brush, rect);
        }
    }

    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
    {
        e.Item.ForeColor = Color.White;
        base.OnRenderItemText(e);
    }

    protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
    {
        base.OnRenderSeparator(e);
    }

    public MenuStripRenderer() : base(new MenuStripColors()) { }
}

public class MenuStripColors : ProfessionalColorTable
{
    public override Color ToolStripDropDownBackground
    {
        get { return MainColor; }
    }

    public override Color ToolStripContentPanelGradientBegin
    {
        get { return MainColor; }
    }

    public override Color ToolStripContentPanelGradientEnd
    {
        get { return MainColor; }
    }

    public override Color MenuItemSelected
    {
        get { return MainColor; }
    }

    public override Color MenuItemBorder
    {
        get { return MainColor; }
    }

    public override Color MenuItemSelectedGradientBegin
    {
        get { return MainColor; }
    }

    public override Color MenuItemSelectedGradientEnd
    {
        get { return MainColor; }
    }

    public override Color MenuBorder
    {
        get { return MainColor; }
    }
}

// In the forms Load()
this.menuStrip1.Renderer = new MenuStripRenderer();
this.menuStrip1.BackColor = MainColor;
this.menuStrip1.ForeColor = Color.White;

我相信您也必须覆盖 ImageMargin 绘图:

protected override void OnRenderImageMargin(ToolStripRenderEventArgs e) {
  //base.OnRenderImageMargin(e);
  using (SolidBrush brush = new SolidBrush(MainColor)) {
    e.Graphics.FillRectangle(brush, e.AffectedBounds);
  }
}

我不知道是什么导致了您的复选标记问题,因为我无法在我的机器上复制该问题。我猜这可能是您 windows.

中的图形 DPI 设置