[英]Understanding OnPaint method
我正在做一些有很多标签的女巫项目。 我的任务是使标签更平滑,因为它看起来很丑。 我编写了自定义LabelEx
类,该类扩展了Label
类。 然后,我像这样重写OnPaint()
方法:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
}
但这行不通。 我仍然有旧的丑标签。
帮助我了解OnPaint
方法的含义。 它是如何工作的? 我希望我的标签具有在myClass.cs[Design]
设置的属性(位置,大小,TextAlign,字体)。 我只需要使它们更平滑。
默认情况下,Label OnPaint使用GDI(System.Windows.Forms.TextRenderer),而不使用GDI +(System.Drawing.Graphics)。要使用GDI +,您需要将UseCompatibleTextRendering True设置为True,并在调用base.OnPaint(e)之前更改GDI +选项。 这是一个例子:
public class LabelEx : System.Windows.Forms.Label
{
public LabelEx()
{
UseCompatibleTextRendering = true;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
// You can try this two options too.
//e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
base.OnPaint(e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.