简体   繁体   English

了解OnPaint方法

[英]Understanding OnPaint method

I'm working on some project witch has a lot of labels. 我正在做一些有很多标签的女巫项目。 I have a task to make labels smoother because it looks ugly. 我的任务是使标签更平滑,因为它看起来很丑。 I wrote my custom LabelEx class which extends Label class. 我编写了自定义LabelEx类,该类扩展了Label类。 Then I overrided OnPaint() method like this: 然后,我像这样重写OnPaint()方法:

protected override void OnPaint(PaintEventArgs e)
{
   base.OnPaint(e);
   e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
}

But it does not work. 但这行不通。 I still have my old ugly labels. 我仍然有旧的丑标签。

Help me understand the meaning of OnPaint method. 帮助我了解OnPaint方法的含义。 How does it work? 它是如何工作的? I want my labels to have the properties which I set to them in myClass.cs[Design] (Location, Size, TextAlign, Font). 我希望我的标签具有在myClass.cs[Design]设置的属性(位置,大小,TextAlign,字体)。 I just need to make them smoother. 我只需要使它们更平滑。

这就是我所说的“丑陋”

By default Label OnPaint use GDI (System.Windows.Forms.TextRenderer), and not GDI+ (System.Drawing.Graphics), to use GDI+ you need to set UseCompatibleTextRendering True and change your GDI+ Options BEFORE call base.OnPaint(e); 默认情况下,Label OnPaint使用GDI(System.Windows.Forms.TextRenderer),而不使用GDI +(System.Drawing.Graphics)。要使用GDI +,您需要将UseCompatibleTextRendering True设置为True,并在调用base.OnPaint(e)之前更改GDI +选项。 Here's a example: 这是一个例子:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM