c# - ToolStrip.Background 仅更改按钮的背景 (C#/.NET/WinForms)

标签 c# winforms toolstrip

我希望我的 ToolStrip 背景在项目未保存时发生变化。

为了渲染我的工具条的背景,我使用了我自己的渲染器:

    class ToolStripRenderer : ToolStripProfessionalRenderer
    {
        private MenuBarForm parent;

        public ToolStripRenderer(MenuBarForm Parent)
        {
            parent = Parent;
        }

        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            if (parent.controlItems.Last().Unsaved)
                e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, Color.Red, 90, true), e.AffectedBounds);
            else
                e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, SystemColors.ControlDark, 90, true), e.AffectedBounds);
        }
    }

工具条第一次呈现时,它使用灰色到深灰色设计正确呈现:

CorrectToolstrip

但是当栏应该变成红色时,只有鼠标悬停在上面的按钮变成红色:

IncorrectToolstrip

我希望整个工具条立即变为红色。

我已经尝试将 e.AffectedBounds 更改为 e.ToolStrip.Bounds,但无济于事。

最佳答案

您可以创建自定义颜色表继承 ProfessionalColorTable并覆盖相关属性以更改背景颜色:

public class CustomColorTable : ProfessionalColorTable
{
    public override Color ToolStripGradientBegin
    {
        get { return Color.Red; }
    }
    public override Color ToolStripGradientMiddle
    {
        get { return Color.Red; }
    }
    public override Color ToolStripGradientEnd
    {
        get { return SystemColors.ControlLightLight; }
    }
}

要更改您的 ToolStrip 背景,分配一个新的 ToolStripProfessionalRenderer它使用您的自定义颜色表来 ToolStripManager.Renderer :

ToolStripManager.Renderer = new ToolStripProfessionalRenderer(new CustomColorTable());

设置原始专业渲染器:

ToolStripManager.Renderer = new ToolStripProfessionalRenderer();

关于c# - ToolStrip.Background 仅更改按钮的背景 (C#/.NET/WinForms),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586505/

相关文章:

c# - 如何将参数从 ajax 传递到 web 方法?

c# - 通过静态方法访问页面对象

c# - 在 Windows 窗体中只能显示为大写字母的 TextBox 中仅允许 Windows 文件系统中的有效字符?

c# - 移除 ToolStripMenuItem 左边框

c# - 在 ContextMenuStrip 中获取 ToolstripItem 的文本

c# - "value cannot be null parameter name: key"

c# - 事件发生时唤醒一个线程

c# - Try/Catch 没有捕捉到

C# DataGridView,大单元格 : Content never fully visible, 滚动跳过单元格

wpf - ToolStripButton 是否有 WPF 等效项?