2

我有我的自定义按钮,我在其中覆盖了 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 = "";
}

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

4

2 回答 2

0

Devils Child的回答会影响线条和圆圈等的质量。

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

e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
于 2012-05-09T07:05:43.990 回答
0

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

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

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

于 2012-05-09T06:51:43.287 回答