首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StatusStrip的水平分频器

StatusStrip的水平分频器
EN

Stack Overflow用户
提问于 2015-02-13 03:37:51
回答 2查看 1.2K关注 0票数 0

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

我指的是:

文件: Program.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-14 04:03:25

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

代码语言:javascript
复制
namespace test {
    class Program {

        [STAThread]

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

Stack Overflow用户

发布于 2021-07-17 00:19:54

当我在谷歌上搜索时,我发现了这个6+的老问题。我不认为这对行动来说是个问题。只是为了将来的读者。

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

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

代码语言:javascript
复制
private void FormMain_StatusStrip_Setup()
{
    this.statusStrip.BackColor = this.statusStrip.BackColor;
    //...
}

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

在您的例子中,显然BackColor技巧没有任何效果,这条线仍然是我们可以在您的图像中看到的。这可能是自定义ToolStripRenderer的结果,如果您有一个分配给StatusStrip.Renderer属性的属性,该属性使用默认值和呈现条带的方法,包括边框。

考虑一下这个例子:

代码语言:javascript
复制
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的边框。

代码语言:javascript
复制
public class MyCustomRenderer : ToolStripProfessionalRenderer
{
    public MyCustomRenderer() : base(new MyColorTable()) { }

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

或者用你所选择的颜色来划定界限:

代码语言:javascript
复制
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);
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28492071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档