如何修复有界的ToolStripStatusLabel

时间:2015-07-27 14:15:09

标签: c# winforms statusstrip toolstripstatuslabel

ToolStripStatusLabel设置为BorderSides并且我将背景颜色设置为与拥有All背景颜色不同时,我遇到StatusStrip问题:背景颜色: ToolStripStatusLabels Backgroundcolor在边界外流血 - 看起来很丑陋。我尝试将BorderStyle属性设置为除Flat之外的其他设置,但没有成功。

在下面添加的屏幕截图中,您会看到问题 - teal中的示例是BorderStyle = Adjust,以便在矩形外部绘制边框。但不幸的是,边界完全消失了。

Different BorderStyles won't help

我想得到的就是在这个手绘的例子中没有任何流血。

enter image description here

这可以通过设置或继承或覆盖ToolStripStatusLabel的特定方法来实现吗?我对程序设计解决方案持开放态度,但我不知道从哪里开始,所以任何提示都会受到欢迎。


通过合并x4rf41TaW下面的答案

来实施解决方案

由于我使用了多个能够让我走上正轨的答案,因此我在问题中添加了最终解决方案。

我扩展了ToolStripStatusLabel类并覆盖了OnPaint方法。这使我有可能使用类属性并绘制它,因为它会正常绘制,但没有流血。

public partial class ToolStripStatusLabelWithoutColorBleeding : ToolStripStatusLabel
{
    /// <summary>
    /// Bugfix to prevent bleeding of background colors outside the borders.
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle borderRectangle = new Rectangle(0, 0, Width - 1, Height - 1);

        // Background
        e.Graphics.FillRectangle(new SolidBrush(BackColor), borderRectangle);

        // Border (if required)
        if (BorderSides != ToolStripStatusLabelBorderSides.None)
            ControlPaint.DrawBorder3D(e.Graphics, borderRectangle, BorderStyle, (Border3DSide)BorderSides);

        // Draw Text if you need it
        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0,0);

    }
}

2 个答案:

答案 0 :(得分:1)

我不认为通过设置标签属性可以解决您的问题。你必须做一些自定义绘图。

我不确切知道您要对标签做些什么,但自定义绘图最简单的方法是使用标签的paint事件:

private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
{
    // Use the sender, so that you can use the same event handler for every label
    ToolStripStatusLabel label = (ToolStripStatusLabel)sender;
    // Background
    e.Graphics.FillRectangle(new SolidBrush(label.BackColor), e.ClipRectangle);
    // Border
    e.Graphics.DrawRectangle(
        new Pen(label.ForeColor),  // use any Color here for the border
        new Rectangle(e.ClipRectangle.Location,new Size(e.ClipRectangle.Width-1,e.ClipRectangle.Height-1))
    );
    // Draw Text if you need it
    e.Graphics.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), e.ClipRectangle.Location);
}

如果您将标签的BackColor设置为洋红色而ForeColor设置为右灰色,则会显示您的手绘示例。

您还可以扩展ToolStripStatusLabel类并覆盖onPaint方法。代码几乎相同,但您在自定义类中有更多选项,例如添加BorderColor属性或类似的东西。

答案 1 :(得分:1)

我使用Instant instant = Instant.ofEpochSecond( secondsSinceEpoch ) ; 玩了一下,发现它也有BackColor显示为底线和右线。

所以,类似于xfr41的回答,我试图做主人画。我的想法是使用系统的例程,但是要在剪切区域上放大绘图矩形;这样就会完全丢失错误的条纹..

ControlPaint.DrawBorder3D