StatusStrip的水平分隔符

时间:2015-02-13 03:37:52

标签: c# winforms statusstrip

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

这是我指的是:

file: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());
        }
    }
}

file: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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在谷歌搜索时发现了这个 6 岁以上的问题。我不认为这对 OP 来说仍然是一个问题。只为未来的读者。


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

SOQ28492071A

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

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

SOQ28492071B

有关此行为的更多详细信息,请参阅 Winforms ToolStrip.BackColor returns wrong color


在您的情况下,显然 BackColor 技巧无效,我们在您的图像中可以看到这条线。这可能是自定义 ToolStripRenderer 的结果,如果您有一个分配给 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;
    // ...
}

SOQ28492071C

这里需要重写渲染器的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);
    }
}

SOQ28492071D

或者用您选择的颜色绘制线条:

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);
}

SOQ28492071E