【问题标题】:OnPaint override cannot draw white text on top of picture boxesOnPaint 覆盖无法在图片框顶部绘制白色文本
【发布时间】:2011-02-14 22:40:01
【问题描述】:

我正在编写一个编辑歌曲元数据的应用程序。为此,我有一个选择歌曲标签的窗口,您可以在旧的和新的之间进行选择。我有一个自定义控件,每个标签框都有 3 个图片框。左帽一个,中间一个,右帽一个。然后我覆盖了 UserControl 的 OnPaint 以将文本绘制到控件上。这很好用,除非我尝试在其中包含图像的图片框顶部使用白色文本。白色似乎变得半透明。我在下面附上了图片来证明这一点。

黑色文字

Image of Black Text http://bentrengrove.com.au/blackText.PNG

白色文字

Image of White Text http://bentrengrove.com.au/WhiteText.PNG

这是我的 OnPaint 方法的代码

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Graphics g = e.Graphics;
    Brush b;
    Font f;

    if (_isSelected && this.Enabled) //Determines if the tag has the boxes visible, i.e is a selected tag
    {
        b = new SolidBrush(Color.White);
        f = new System.Drawing.Font("Segoe UI", 8, FontStyle.Regular);
    }
    else
    {
        b = new SolidBrush(Color.Gray);
        f = new System.Drawing.Font("Segoe UI", 8, FontStyle.Regular);
    }

    var textSize = g.MeasureString(_text, f); //We will resize the tag boxes based on the size of the text
    StringFormat drawFormat = new StringFormat();
    drawFormat.Alignment = StringAlignment.Near;

    RectangleF layoutRectangle = new RectangleF(leftCap.Width, 1, textSize.Width, 16);

    if (textSize.Width >= 105)
        _text = String.Format("{0}...", _text.Substring(0, 15)); //There is only so much room to display text

    middle.Width = (int)textSize.Width + rightCap.Width;
    rightCap.Left = middle.Left + middle.Width - rightCap.Width;

    g.DrawString(_text, f, b, layoutRectangle, drawFormat); //Draw the string for this control based on what has been set to text

    //Clean up
    g.Dispose();
    b.Dispose();
    f.Dispose();
}

如果有人知道为什么我不能用白色画出任何想法,我们将不胜感激。

【问题讨论】:

  • 如果你使用更大的字体,它会显示得更好吗?将 FontStyle 设置为 FontStyle.Bold 有帮助吗?我觉得这是一个抗锯齿问题。
  • 不,不幸的是它也有同样的问题,看起来有点灰色,但加粗。
  • 加粗文字图片:link

标签: c# winforms windows-7 gdi+


【解决方案1】:

我自己解决了这个问题。问题是由于订单项目被绘制在表单上。因为我是在控件的 OnPaint 方法中绘制图片框,所以首先调用 OnPaint。即使 base.OnPaint 是我的 onPaint 方法中的第一项,控件的绘画也是在此方法完成后绘制的。通过删除中间的图片框并在 OnPaint 内绘制其图像,文本正确绘制为白色。我仍然不确定为什么这个问题只出现在白色文本上,而不会出现在其他颜色上。

【讨论】:

    【解决方案2】:

    你确定你最终使用的刷子是白色的吗?你能验证_isSelectedthis.Enabled 都是true 吗?签入调试器以查看值。

    我在DimGray 背景上放了一些Gray 文本,它看起来与您现在的样子相似。

    protected override void OnPaint(PaintEventArgs pe)
    {
        using (Font f = new Font(this.Font.FontFamily, 8f, FontStyle.Regular))
        {
            pe.Graphics.DrawString(
                "Foo",
                f,
                Brushes.Black,
                new PointF(5, 5),
                new StringFormat
                {
                    Alignment = StringAlignment.Near,
                });
            pe.Graphics.DrawString(
                "Bar",
                f,
                Brushes.Gray,
                new PointF(5, 20),
                new StringFormat
                {
                    Alignment = StringAlignment.Near,
                });
            pe.Graphics.DrawString(
                "Bar",
                f,
                Brushes.White,
                new PointF(5, 35),
                new StringFormat
                {
                    Alignment = StringAlignment.Near,
                });
        }
    }
    

    【讨论】:

    • 感谢您的帮助,但我最终得到的刷子确实是白色的。我已经对其进行了测试,例如我可以将 Color.White 的颜色更改为任何颜色,它会以该颜色正确绘制。只是白色似乎有问题。
    • 如何增加字体大小,将背景颜色更改为更暗的颜色等。操作系统可能会接管“可读性”。编辑:Jeff M 的字体参数有“8f”而不是“8”。试试看?
    • 将字体大小更改为 8f 也不起作用。我也尝试过使用 TextContrast 和 TextRenderingHint 的设置,但没有成功。我现在将尝试删除图片框并改用 GraphicsContext 绘制图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多