按钮上的文字画 - 差异看(Painting text on Button - Difference

2019-09-16 23:53发布

我),我已经覆盖了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 = "";
}

任何想法,我要去哪里错了,以及如何采取同样的照顾?

Answer 1:

您需要设置正确的平滑处理模式是这样的:

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

然后,结果应该OK。



Answer 2:

魔鬼孩子的答案会影响线和圆等的质量

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

e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;


文章来源: Painting text on Button - Difference in look