我遇到 ToolStripStatusLabel
问题,当 BorderSides
设置为 All
并且我设置的背景颜色与所属颜色不同时,就会出现此问题StatusStrip
背景颜色:ToolStripStatusLabels 背景颜色超出边框 - 看起来非常难看。我尝试将 BorderStyle
属性设置为 Flat 以外的其他设置,但没有成功。
在下面添加的屏幕截图中,您会看到问题 - 青色示例是使用 BorderStyle = Adjust
将边框绘制在矩形之外。但不幸的是,边界完全消失了。
我想要的是完全没有出血,就像这个手绘的例子一样。
是否可以通过设置或继承或重写 ToolStripStatusLabel
的特定方法来完成此操作?我对编程解决方案持开放态度,但我不知道从哪里开始,所以欢迎任何提示。
通过结合 x4rf41 实现解决方案和 TaW答案如下
由于我使用了多个答案,这些答案使我走上了正确的道路,所以我添加了问题的最终解决方案。
我扩展了 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);
}
}
最佳答案
我认为您的问题不能通过设置标签属性来解决。您必须进行一些自定义绘图。
我不知道你到底想用标签做什么,但自定义绘图的最简单方法是使用标签的绘制事件:
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 属性或类似的内容。
关于c# - 如何修复带边框的 ToolStripStatusLabel 中的背景颜色渗色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655565/