StatusStrip 的水平分隔线

Horizontal divider of the StatusStrip

Windows.Forms 应用程序中,我想更改 StatusStrip 的水平分隔线的颜色,或者使这条线不可见。有什么想法吗?

这是我指的:

文件:Program.cs

using System;
using System.Data;
using System.Drawing;
using System.Data.Linq;
using System.Windows.Forms;

namespace test {
    class Program {

        [STAThread]

        static void Main() {
            Application.Run(new FormMain());
        }
    }
}

文件:FormMain.cs

using System;
using System.Data;
using System.Drawing;
using System.Data.Linq;
using System.Windows.Forms;

namespace test {
    class Vars {
        public class Colors {
            public static Color BackRed = Color.FromArgb(040, 000, 000);
            public static Color ForeRed = Color.FromArgb(240, 120, 120);
            public static Color BackGrn = Color.FromArgb(000, 040, 000);
            public static Color ForeGrn = Color.FromArgb(120, 240, 120);
            public static Color BackBlu = Color.FromArgb(000, 000, 040);
            public static Color ForeBlu = Color.FromArgb(120, 120, 240);
        }
    }

    class FormMain : Form {
        MenuStrip menuStrip = new MenuStrip();
        StatusStrip statusStrip = new StatusStrip();

        public FormMain() {
            this.FormMain_Setup();
        }

        private void FormMain_Setup() {
            this.Top = 20;
            this.Left = 20;
            this.Width = 1200;
            this.Height = 675;
            this.BackColor = Vars.Colors.BackBlu;
            this.ForeColor = Vars.Colors.ForeBlu;
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.Manual;
            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            this.KeyDown += FormMain_KeyDown;
            this.FormMain_MenuStrip_Setup();
            this.FormMain_StatusStrip_Setup();
        }

        private void FormMain_StatusStrip_Setup() {
            this.statusStrip.Height = 30;
            this.statusStrip.AutoSize = false;
            this.statusStrip.BackColor = Vars.Colors.BackRed;
            this.statusStrip.ForeColor = Vars.Colors.ForeRed;
            this.statusStrip.SizingGrip = false;
            this.Controls.Add(statusStrip);
        }

        private void FormMain_MenuStrip_Setup() {
            this.menuStrip.Height = 30;
            this.menuStrip.AutoSize = false;
            this.menuStrip.BackColor = Vars.Colors.ForeGrn;
            this.menuStrip.ForeColor = Vars.Colors.BackGrn;
            this.Controls.Add(menuStrip);
        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e) {
            this.FormMain_Exit();
        }

        private void FormMain_Exit() {
            this.Close();
        }
    }
}

我在谷歌搜索时发现了这个 6 年多以前的问题。我不认为这仍然是 OP 的问题。只为未来的读者。


当您通过设计器或代码添加 StatusStrip 实例时,您会在控件顶部看到一条细水平线。

您可以通过将 StatusStrip.BackColor 属性 显式设置为任何颜色来摆脱这条线。在设计器中,将颜色更改为任何颜色并将其设置回继承的颜色(表单的),它就会消失。或者,在代码中,将 属性 设置为自身:

private void FormMain_StatusStrip_Setup()
{
    this.statusStrip.BackColor = this.statusStrip.BackColor;
    //...
}

有关此行为的更多详细信息,请参阅


在你的情况下,显然 BackColor 技巧没有效果,线条仍然如我们在你的图片中看到的那样。这可能是自定义 ToolStripRenderer if you have one assigned to the StatusStrip.Renderer 属性 的结果,它使用默认值和方式来呈现包括边框在内的条带。

考虑这个例子:

public class FormMain
{
    public FormMain() : base()
    {
        this.BackColor = Color.Black;
        this.statusStrip.Renderer = new MyCustomRenderer();
    }
}

public class MyCustomRenderer : ToolStripProfessionalRenderer
{
    public MyCustomRenderer() : base(new MyColorTable()) { }
}

public class MyColorTable : ProfessionalColorTable
{
    public override Color StatusStripGradientBegin => Color.Black;
    public override Color StatusStripGradientEnd => Color.Black;
    // ...
}

这里需要重写renderer的OnRenderToolStripBorder方法来防止绘制StatusStrip的边框。

public class MyCustomRenderer : ToolStripProfessionalRenderer
{
    public MyCustomRenderer() : base(new MyColorTable()) { }

    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        if (!(e.ToolStrip is StatusStrip)) base.OnRenderToolStripBorder(e);
    }
}

或者用您选择的颜色画线:

protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
    if (e.ToolStrip is StatusStrip)
        e.Graphics.DrawLine(Pens.Red, 0, 0, e.ToolStrip.Width, 0);
    else
        base.OnRenderToolStripBorder(e);
}

当我将 Application.EnableVisualStyles(); 添加到 Main() 时,问题行消失了。

namespace test {
    class Program {

        [STAThread]

        static void Main() {
            Application.EnableVisualStyles(); // this line added
            Application.Run(new FormMain());
        }
    }
}