在按钮上绘画文本-外观差异 [英] Painting text on Button - Difference in look

查看:68
本文介绍了在按钮上绘画文本-外观差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义按钮,该按钮覆盖了OnPaint()并仅在其中绘制文本.在运行时,文本看起来有所不同-字符之间缺少空格.这是设计与制造的形象按钮的运行时间:

I have my custom button where I have overridden the OnPaint() and draw the text in it only. On runtime the text looks differently - spacing between chars is lacking. Here is the image of design & runtime of the button :

绘画方法如下:

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

有什么主意我要去哪里了,要怎么解决呢?

Any idea where am I going wrong and how to take care of the same?

推荐答案

Devils Child的答案将影响线路质量和圈子等等.

Devils Child's answer will affect the quality of lines and circles, etc.

但是对于文本呈现,您可以使用:

But for text rendering, you can use:

e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

这篇关于在按钮上绘画文本-外观差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆