Painting text on Button - Difference in look

耗尽温柔 提交于 2019-12-11 02:55:30

问题


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 :

The paint methods is as:

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?


回答1:


You need to set the correct smoothing mode like this:

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

Then, the result should look OK.




回答2:


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;


来源:https://stackoverflow.com/questions/10427069/painting-text-on-button-difference-in-look

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!