将 Tahoma、TextRenderingHint.ClearTypeGridFit 和 AutoSize (WindowsXP) 组合用于标签控件

发布于 2024-12-21 09:26:44 字数 2918 浏览 1 评论 0原文

使用 AutoSize 作为由 Tahoma 字体和选项 TextRenderingHint.ClearTypeGridFit 绘制的标签时存在问题。 如果没有 ClearTypeGridFit,它看起来不错,但是使用 - 它被父容器裁剪(参见附图:第一个标签“,”被裁剪)

我发现它仅使用 Tahoma 字体。

自定义标签的代码:

class CustomLabel: Label
{
    public CustomLabel ()
        :base()
    {
    }

    protected override void OnPaint ( PaintEventArgs e )
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        base.OnPaint( e );            
    }
}

设计器文件中的代码和平:

        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.label1.Font = new System.Drawing.Font( "Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ) );
        this.label1.Location = new System.Drawing.Point( 54, 95 );
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size( 35, 13 );
        this.label1.TabIndex = 1;
        this.label1.Text = resources.GetString( "label1.Text" );
        // 
        // customLabel1
        // 
        this.customLabel1.AutoSize = true;
        this.customLabel1.BackColor = System.Drawing.Color.NavajoWhite;
        this.customLabel1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.customLabel1.Font = new System.Drawing.Font( "Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ) );
        this.customLabel1.ForeColor = System.Drawing.Color.Black;
        this.customLabel1.Location = new System.Drawing.Point( 1, 1 );
        this.customLabel1.Margin = new System.Windows.Forms.Padding( 0 );
        this.customLabel1.Name = "customLabel1";
        this.customLabel1.Size = new System.Drawing.Size( 192, 154 );
        this.customLabel1.TabIndex = 0;
        this.customLabel1.Text = resources.GetString( "customLabel1.Text" );

在此处输入图像描述

我发现只是覆盖了 GetPreferedSize 函数:

    public override Size GetPreferredSize ( Size theProposedSize )
    {            
        if ( TextRendering == TextRenderingHint.ClearTypeGridFit )
        {
            Graphics aGraphics = Graphics.FromHwnd( this.Handle );
            if ( aGraphics != null )
            {
                aGraphics.TextRenderingHint = theTextRendering;
                Size aResult = TextRenderer.MeasureText( aGraphics, Text, Font, theProposedSize );
                //apply control minimum size
                aResult.Height = Math.Max( aResult.Height, MinimumSize.Height );
                aResult.Width = Math.Max( aResult.Width, MinimumSize.Width );
                return aResult;
            }
        }

        return base.GetPreferredSize( theProposedSize );
    }

但我无法对结果应用最大尺寸。 为什么我需要最大尺寸?例如,我希望能够按宽度限制标签。

有什么想法吗?

It is problem of using AutoSize for label which are drawn by Tahoma font with option TextRenderingHint.ClearTypeGridFit.
Without ClearTypeGridFit it looks ok, but with - it cropped by parent container (see attached image: on first label ',' is cropped)

I've found it is only with Tahoma font.

Code of customlabel:

class CustomLabel: Label
{
    public CustomLabel ()
        :base()
    {
    }

    protected override void OnPaint ( PaintEventArgs e )
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        base.OnPaint( e );            
    }
}

Peace of code from designer file:

        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.label1.Font = new System.Drawing.Font( "Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ) );
        this.label1.Location = new System.Drawing.Point( 54, 95 );
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size( 35, 13 );
        this.label1.TabIndex = 1;
        this.label1.Text = resources.GetString( "label1.Text" );
        // 
        // customLabel1
        // 
        this.customLabel1.AutoSize = true;
        this.customLabel1.BackColor = System.Drawing.Color.NavajoWhite;
        this.customLabel1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.customLabel1.Font = new System.Drawing.Font( "Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ) );
        this.customLabel1.ForeColor = System.Drawing.Color.Black;
        this.customLabel1.Location = new System.Drawing.Point( 1, 1 );
        this.customLabel1.Margin = new System.Windows.Forms.Padding( 0 );
        this.customLabel1.Name = "customLabel1";
        this.customLabel1.Size = new System.Drawing.Size( 192, 154 );
        this.customLabel1.TabIndex = 0;
        this.customLabel1.Text = resources.GetString( "customLabel1.Text" );

enter image description here

What I have found just a overridden a GetPreferedSize function:

    public override Size GetPreferredSize ( Size theProposedSize )
    {            
        if ( TextRendering == TextRenderingHint.ClearTypeGridFit )
        {
            Graphics aGraphics = Graphics.FromHwnd( this.Handle );
            if ( aGraphics != null )
            {
                aGraphics.TextRenderingHint = theTextRendering;
                Size aResult = TextRenderer.MeasureText( aGraphics, Text, Font, theProposedSize );
                //apply control minimum size
                aResult.Height = Math.Max( aResult.Height, MinimumSize.Height );
                aResult.Width = Math.Max( aResult.Width, MinimumSize.Width );
                return aResult;
            }
        }

        return base.GetPreferredSize( theProposedSize );
    }

But I can't apply a Maximum size to the result.
Why I need a Maximum size? For example, I want to have a possibility to limit labels by width.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2024-12-28 09:26:44

所以,我的最终解决方案看起来是这样的:

    public override Size GetPreferredSize ( Size theProposedSize )
    {
        if ( SystemInfo.WinVersion == SystemInfo.Version.WindowsXP )
        {
            if ( theTextRendering == TextRenderingHint.ClearTypeGridFit )
            {
                Graphics aGraphics = Graphics.FromHwnd( this.Handle );
                if ( aGraphics != null )
                {
                    aGraphics.TextRenderingHint = theTextRendering;
                    Size aResult = TextRenderer.MeasureText( aGraphics, Text, Font, theProposedSize );
                    //apply padding and margin
                    aResult.Width += Margin.Horizontal + Padding.Horizontal;
                    aResult.Height += Margin.Vertical + Padding.Vertical;
                    //apply control minimum size
                    aResult.Height = Math.Max( aResult.Height, MinimumSize.Height );
                    aResult.Width = Math.Max( aResult.Width, MinimumSize.Width );

                    //adopt maximum width
                    if ( MaximumSize.Width > 0 )
                    {
                        while ( aResult.Width > MaximumSize.Width )
                        {
                            aResult.Width -= MaximumSize.Width;
                            aResult.Height += Font.Height;
                            if ( aResult.Width < MaximumSize.Width )
                            {
                                aResult.Width = MaximumSize.Width;
                            }
                        }
                    }

                    return aResult;
                }
            }
        }

        return base.GetPreferredSize( theProposedSize );
    }

So, my final solution looks so:

    public override Size GetPreferredSize ( Size theProposedSize )
    {
        if ( SystemInfo.WinVersion == SystemInfo.Version.WindowsXP )
        {
            if ( theTextRendering == TextRenderingHint.ClearTypeGridFit )
            {
                Graphics aGraphics = Graphics.FromHwnd( this.Handle );
                if ( aGraphics != null )
                {
                    aGraphics.TextRenderingHint = theTextRendering;
                    Size aResult = TextRenderer.MeasureText( aGraphics, Text, Font, theProposedSize );
                    //apply padding and margin
                    aResult.Width += Margin.Horizontal + Padding.Horizontal;
                    aResult.Height += Margin.Vertical + Padding.Vertical;
                    //apply control minimum size
                    aResult.Height = Math.Max( aResult.Height, MinimumSize.Height );
                    aResult.Width = Math.Max( aResult.Width, MinimumSize.Width );

                    //adopt maximum width
                    if ( MaximumSize.Width > 0 )
                    {
                        while ( aResult.Width > MaximumSize.Width )
                        {
                            aResult.Width -= MaximumSize.Width;
                            aResult.Height += Font.Height;
                            if ( aResult.Width < MaximumSize.Width )
                            {
                                aResult.Width = MaximumSize.Width;
                            }
                        }
                    }

                    return aResult;
                }
            }
        }

        return base.GetPreferredSize( theProposedSize );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文