Winforms ToolStripSplitButton 在其下方显示一条灰线,并且仅当鼠标悬停在其上时才会升起

发布于 2024-07-26 07:46:24 字数 632 浏览 5 评论 0原文

我正在尝试找出 ToolStripSplitButton。 目的是在按下 ToolStripSplitButton 时显示用户控制仪表。 但是,无论我尝试什么设置,按钮下方都会出现一条灰线。

ToolStrip 本身设置为 RenderMode.System,未停靠,并且 ToolStripSplitButton 是其中唯一的组件。

可以通过引入自定义 ToolStripRenderer 类来删除这条线,但这对于删除控件下这条令人讨厌的深灰色线来说似乎完全是矫枉过正。

我意识到这可能完全是在黑暗中进行的,因为我没有为这些组件提供大量的其余设置,但我希望有人能够深入了解为什么该控件的行为方式如此。


我的第二个问题是关于 ToolStripSplitButton 的行为。 有什么方法可以避免鼠标悬停在组件上之前显示的扁平且无边框的外观吗? 我试图让它与面板中的其余按钮一起具有统一的外观,并且 ToolStripSplitButton 仅在鼠标光标放置在组件上方时才会出现。

这是一个屏幕截图:

ToolStripSplitButton有缺陷

非常感谢任何帮助!

I'm trying to figure out the ToolStripSplitButton. The purpose is to display a usercontrol gauge when a ToolStripSplitButton is pressed. However, no matter what settings I try, there is a grey line visible below the button.

The ToolStrip itself is set to RenderMode.System, is not docked, and the ToolStripSplitButton is the only component in it.

I can remove the line by introducing a custom ToolStripRenderer class, but this seems like a total overkill for removing this single annoying dark grey line under the control.

I realize it might be a total shot in the dark since I don't provide the rest of the gazillion settings for these components, but I was hoping someone could provide insight into why this control is behaving the way it does.


My second question is regarding the behaviour of the ToolStripSplitButton. Is there any way to avoid the flat and borderless look that the component displays before the mouse hovers over it? I'm trying to give it a uniform look along with the rest of the buttons in the panel, and the ToolStripSplitButton only appears raised when the mouse cursor is placed above the component.

Here's a screenshot:

ToolStripSplitButton flawed

Any help is greatly appreaciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛刀软妹 2024-08-02 07:46:24
/// <summary>
/// This class provides custom rendering code for the ToolStrip and ToolStripDropDownButton because the standard windows
/// rendering gave it a very flat look.
/// </summary>
public class CustomToolStripRenderer : ToolStripRenderer {
    ToolStripDropDownButton toolStripDDButton;

    public CustomToolStripRenderer(ToolStripDropDownButton toolStripDropDownButton) : base() {
        toolStripDDButton = toolStripDropDownButton;
    }

    //protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs tsirea) {
    //    // Check if the item is selected or hovered over.
    //    if (tsirea.Item.Selected || tsirea.Item.Pressed) {
    //        LinearGradientBrush brush = new LinearGradientBrush(tsirea.Item.Bounds, Color.DarkBlue, Color.DarkGreen, 90);
    //        tsirea.Graphics.FillRectangle(brush, 0, 0, tsirea.Item.Width, tsirea.Item.Height);
    //    }
    //}

    protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs tsrea) {
        // This event occurs before the OnRenderDropDownButtonBackground event...

        if (toolStripDDButton.Pressed) {
            base.OnRenderToolStripBackground(tsrea);
        }
        else if (toolStripDDButton.Selected) {
            ControlPaint.DrawButton(tsrea.Graphics, tsrea.AffectedBounds, ButtonState.Normal);
        }
        else {
            ControlPaint.DrawButton(tsrea.Graphics, tsrea.AffectedBounds, ButtonState.Normal);
        }
    }

    //protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs tsirea) {
    //    // Happens every time the button is hovered over as well, and upon mouseleave

    //    //ControlPaint.DrawButton(tsirea.Graphics, tsirea.Item.ContentRectangle, ButtonState.Normal);
    //    base.OnRenderDropDownButtonBackground(tsirea);
    //}

    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs tsrea) {
        //This event occurs after the OnRenderDropDownButtonBackground event...
        //Thus it will paint over whatever is already painted in the OnRenderDropDownButtonBackground event.

        //Debug.Println("OnRenderToolStripBorder");
        if (toolStripDDButton.Pressed) {
            // Draw the top and left borders of the component so that it looks like a tab page:
            tsrea.Graphics.DrawLine(SystemPens.ControlDarkDark, tsrea.AffectedBounds.Left, tsrea.AffectedBounds.Top, tsrea.AffectedBounds.Left, tsrea.AffectedBounds.Bottom);
            tsrea.Graphics.DrawLine(SystemPens.ControlDarkDark, tsrea.AffectedBounds.Left, tsrea.AffectedBounds.Top, tsrea.AffectedBounds.Right, tsrea.AffectedBounds.Top);
        }
        base.OnRenderToolStripBorder(tsrea);
    }

}
/// <summary>
/// This class provides custom rendering code for the ToolStrip and ToolStripDropDownButton because the standard windows
/// rendering gave it a very flat look.
/// </summary>
public class CustomToolStripRenderer : ToolStripRenderer {
    ToolStripDropDownButton toolStripDDButton;

    public CustomToolStripRenderer(ToolStripDropDownButton toolStripDropDownButton) : base() {
        toolStripDDButton = toolStripDropDownButton;
    }

    //protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs tsirea) {
    //    // Check if the item is selected or hovered over.
    //    if (tsirea.Item.Selected || tsirea.Item.Pressed) {
    //        LinearGradientBrush brush = new LinearGradientBrush(tsirea.Item.Bounds, Color.DarkBlue, Color.DarkGreen, 90);
    //        tsirea.Graphics.FillRectangle(brush, 0, 0, tsirea.Item.Width, tsirea.Item.Height);
    //    }
    //}

    protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs tsrea) {
        // This event occurs before the OnRenderDropDownButtonBackground event...

        if (toolStripDDButton.Pressed) {
            base.OnRenderToolStripBackground(tsrea);
        }
        else if (toolStripDDButton.Selected) {
            ControlPaint.DrawButton(tsrea.Graphics, tsrea.AffectedBounds, ButtonState.Normal);
        }
        else {
            ControlPaint.DrawButton(tsrea.Graphics, tsrea.AffectedBounds, ButtonState.Normal);
        }
    }

    //protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs tsirea) {
    //    // Happens every time the button is hovered over as well, and upon mouseleave

    //    //ControlPaint.DrawButton(tsirea.Graphics, tsirea.Item.ContentRectangle, ButtonState.Normal);
    //    base.OnRenderDropDownButtonBackground(tsirea);
    //}

    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs tsrea) {
        //This event occurs after the OnRenderDropDownButtonBackground event...
        //Thus it will paint over whatever is already painted in the OnRenderDropDownButtonBackground event.

        //Debug.Println("OnRenderToolStripBorder");
        if (toolStripDDButton.Pressed) {
            // Draw the top and left borders of the component so that it looks like a tab page:
            tsrea.Graphics.DrawLine(SystemPens.ControlDarkDark, tsrea.AffectedBounds.Left, tsrea.AffectedBounds.Top, tsrea.AffectedBounds.Left, tsrea.AffectedBounds.Bottom);
            tsrea.Graphics.DrawLine(SystemPens.ControlDarkDark, tsrea.AffectedBounds.Left, tsrea.AffectedBounds.Top, tsrea.AffectedBounds.Right, tsrea.AffectedBounds.Top);
        }
        base.OnRenderToolStripBorder(tsrea);
    }

}
云朵有点甜 2024-08-02 07:46:24

对于任何感兴趣的人,我最终创建了一个自定义 ToolStripRenderer 类。 在这里,我必须重写几个方法才能获得想要的结果,结果非常好。 为了绘制按钮轮廓,我只使用了 ControlPaint,对于类似下拉选项卡的外观,我使用 ControlDarkDark 系统颜色绘制了一些线条。 不深入细节,已经有几个教程对此进行了描述。 现在,要从 ToolStripDropDownButton 获得类似按钮的行为,必须自己绘制,这似乎很奇怪,但我不排除某个设置可能与另一个设置发生冲突。

如果有人感兴趣的话我可以发布代码。

For anyone interested, I ended up creating a custom ToolStripRenderer class after all. Here I had to override several methods to get the wanted result, and the result came out pretty nice. To draw the button outline I simply used ControlPaint, and for the dropped-down tab-like look I drew some lines with the ControlDarkDark system color. Not to get into the gory details, there are several tutorials out there describing this already. Now, it does seem weird that to get a button-like behaviour from a ToolStripDropDownButton one has to do the drawing oneself, but I'm not ruling out that a setting might have clashed with another somewhere.

I can post the code if anyone is interested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文