在按钮上绘画文本 - 外观上的差异

时间:2012-05-03 07:37:51

标签: c# winforms fonts paint

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

绘画方法如下:

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 = "";
}

知道我哪里出错了以及如何照顾它?

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

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

e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;