在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();
}
}
}发布于 2022-04-14 04:03:25
当我将Application.EnableVisualStyles();添加到Main()中时,问题线就消失了。
namespace test {
class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles(); // this line added
Application.Run(new FormMain());
}
}
}发布于 2021-07-17 00:19:54
当我在谷歌上搜索时,我发现了这个6+的老问题。我不认为这对行动来说是个问题。只是为了将来的读者。
当您通过设计器或代码添加StatusStrip实例时,您将在控件顶部看到一条细水平线。

您可以通过显式地将StatusStrip.BackColor属性设置为任何颜色来摆脱这一行。在设计器中,将颜色更改为任意颜色,并将其设置为继承的颜色(窗体),它将消失。或者,在代码中,将属性设置为自身:
private void FormMain_StatusStrip_Setup()
{
this.statusStrip.BackColor = this.statusStrip.BackColor;
//...
}

有关此行为的更多详细信息,请参见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;
// ...
}

在这里,您需要重写呈现器的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);
}

https://stackoverflow.com/questions/28492071
复制相似问题