0

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

1 回答 1

0

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


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

SOQ28492071A

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

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

SOQ28492071B

有关此行为的更多详细信息,请参阅Winforms ToolStrip.BackColor 返回错误颜色


在你的情况下,显然这个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

于 2021-07-17T00:19:54.063 回答