2

我有一个问题,ToolStripStatusLabelBorderSides设置为All并且我设置了与拥有的背景颜色不同的StatusStrip背景颜色时:ToolStripStatusLabels 背景颜色在边界外流血 - 这看起来很丑陋。我试图将BorderStyle属性设置为 Flat 以外的其他设置,但没有成功。

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

不同的边框样式无济于事

我想要得到的是完全没有流血,就像这个手绘的例子一样。

在此处输入图像描述

这可以通过设置或继承或覆盖的特定方法来完成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);

    }
}
4

2 回答 2

1

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

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

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 属性或类似的东西。

于 2015-07-27T14:38:17.857 回答
1

我玩了一下ControlPaint.DrawBorder3D,发现它的背景颜色也显示为底部和右侧。

因此,类似于 xfr41 的回答,我尝试进行所有者绘图。我的想法是使用系统的例程,但是在剪切区域上放大绘图矩形;这样错误的条纹就完全消失了..

private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
{
    Rectangle r = e.ClipRectangle; 
    Rectangle r2 = new Rectangle(r.X, r.Y, r.Width + 1, r.Height + 1);
    ControlPaint.DrawBorder3D(e.Graphics, r2 , Border3DStyle.SunkenInner);
}
于 2015-07-27T15:03:27.560 回答