我在做一个有很多标签的项目。我有一项任务要使标签变得更光滑,因为它看起来很难看。我编写了我的自定义LabelEx类,它扩展了Label类。然后,我像这样高估了OnPaint()方法:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
}但不起作用。我还留着旧的丑陋的标签。
帮助我理解OnPaint方法的含义。它怎麽工作?我希望我的标签拥有我在myClass.cs[Design]中设置的属性(位置、大小、TextAlign、字体)。我只需要让它们变得更光滑。

发布于 2014-10-17 12:39:12
默认情况下,标签OnPaint使用GDI (System.Windows.Forms.TextRenderer),而不是GDI+ (System.Drawing.Graphics),要使用GDI+,您需要在调用base.OnPaint(E)之前设置UseCompatibleTextRendering True并更改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);
}
}https://stackoverflow.com/questions/26424911
复制相似问题