C#:OnRenderSplitButtonBackground 覆盖后消失的箭头

C#: Disappearing arrow after OnRenderSplitButtonBackground override

我试图使用覆盖更改 ToolStripSplitButton 的背景,但这样做后,当鼠标悬停在按钮上时,下拉菜单上的箭头消失了。

代码:

protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
{
    if (!e.Item.Selected)
    {
        base.OnRenderSplitButtonBackground(e);
    }
    else
    {
        Rectangle button = new Rectangle(0, 0, e.Item.Size.Width - 12, e.Item.Size.Height - 1);
        e.Graphics.FillRectangle(Brushes.White, button);
        e.Graphics.DrawRectangle(Pens.Olive, button);

        Rectangle dropdown = new Rectangle(e.ToolStrip.Items[0].Size.Width - 1, 0, e.Item.Size.Width - 32, e.Item.Size.Height - 1);
        e.Graphics.FillRectangle(Brushes.White, dropdown);
        e.Graphics.DrawRectangle(Pens.Olive, dropdown);
    }
}

结果如下所示:

我知道 OnRenderArrow 存在,我也试图覆盖它,但看起来它被 OnRenderSplitButtonBackground 中的 dropdown 矩形“覆盖”了。

我应该如何解决这个问题?提前致谢!

如果将 e.Item 属性 returns(ToolStripItem)的基础 class 转换为 ToolStripSplitButton class,您将获取所有相关属性来绘制拆分按钮的不同部分。

边界属性,例如 ButtonBounds, DropDownButtonBounds, SplitterBounds. State properties; ButtonPressed, ButtonSelected, DropDownButtonPressed、...等

至于箭头部分。您需要调用 OnRenderArrow 方法。

例子

protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
{
    if (!e.Item.Selected || e.Item.Pressed)
        base.OnRenderSplitButtonBackground(e);
    else
    {
        var sb = e.Item as ToolStripSplitButton;
        var button = sb.ButtonBounds;
        var dropdown = sb.DropDownButtonBounds;

        button.Width--;
        button.Height--;
        dropdown.Width--;
        dropdown.Height--;

        var br = sb.ButtonPressed ? Brushes.Gainsboro : Brushes.White;

        e.Graphics.FillRectangle(br, button);
        e.Graphics.DrawRectangle(Pens.Olive, button);
        e.Graphics.FillRectangle(Brushes.White, dropdown);
        e.Graphics.DrawRectangle(Pens.Olive, dropdown);

        OnRenderArrow(new ToolStripArrowRenderEventArgs(
            e.Graphics, e.Item, sb.DropDownButtonBounds, e.Item.ForeColor,
            ArrowDirection.Down));
    }
}