【问题标题】:Painting text on Button - Difference in look在按钮上绘制文本 - 外观差异
【发布时间】:2012-05-12 17:07:40
【问题描述】:

我有我的自定义按钮,我在其中覆盖了 OnPaint() 并仅在其中绘制文本。在运行时文本看起来不同 - 缺少字符之间的间距。这是按钮的设计和运行时的图像:

绘制方法如下:

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

    if (base.ContainsFocus)
    {
        // Draw inner dotted rectangle when button is on focus
        Pen pen = new Pen(Color.Gray, 3);
        Point p = base.Location;
        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
        Rectangle rectangle = new Rectangle(4, 4, Size.Width - 8, 
                                            Size.Height - 8);
        ControlPaint.DrawFocusRectangle(pevent.Graphics, rectangle);
    }

    // Draw the string to screen
    SizeF sf = pevent.Graphics.MeasureString(displayText, this.Font, 
                                             this.Width);
    Point ThePoint = new Point();
    ThePoint.X = (int)((this.Width / 2) - (sf.Width / 2));
    ThePoint.Y = (int)((this.Height / 2) - (sf.Height / 2));
    pevent.Graphics.DrawString(displayText, Font, 
              new SolidBrush(Color.FromArgb(255, 255, 254, 255)), ThePoint);
    this.Text = "";
}

知道我哪里出了问题以及如何处理吗?

【问题讨论】:

  • 你用的是什么字体?
  • @Falle1234,字体 - Arial Rounded MT Bold,9pt
  • 考虑改用TextRenderer.DrawText 方法。
  • 我已尝试重现您的问题,但无法重现。这向我表明,您的 onPaint 方法没有问题。也许您的自定义按钮控件中的其他一些设置以某种方式搞砸了。
  • 我从未尝试过任何这些,但默认情况下,一些代码被添加到 Program 类中:Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false); - 这些可能与问题有关吗?这些可能在设计时没有设置。

标签: c# winforms fonts paint


【解决方案1】:

您需要像这样设置正确的平滑模式:

Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality

然后,结果应该看起来不错。

【讨论】:

    【解决方案2】:

    Devils Child's answer会影响线条和圆等的质量

    但是对于文本渲染,你可以使用:

    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    

    【讨论】:

      猜你喜欢
      • 2013-05-27
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多