标签组件和DrawString之间的文本外观不同 [英] Different text appearance between Label Component and DrawString

查看:92
本文介绍了标签组件和DrawString之间的文本外观不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个Windows窗体应用程序.
我在OnPaint事件中使用Font设置在此窗体上使用DrawString函数绘制了一些字符串,该事件在Form属性中设置.

我放置了一个Label组件,该组件具有与Form Font相同的文本和相同的Font,并且该文本的外观有些不同,尤其是当字体较大时.

默认字体大小为8.25,这些字符串看起来相同.

有人可以告诉我,为什么使用相同的字体设置时它们看起来会有所不同?

感谢您的答复.

彼得



感谢您的答复.

这是我的Paint事件代码:

崩溃

Hello,

I have a Windows form application.
I draw some string with DrawString function on this form in OnPaint event with Font settings, which is set in Form properties.

I put a Label component with the same text and the same Font as the Form Font and the text have a little bit different appearance, especially, when the font size are bigger.

With default font size 8.25 these strings look same.

Can somebody tell me, why they look different when is using the same font settings?

Thank you for reply.

Peter



Thank you for reply.

Here is my code of the Paint event:

Collapse

private void Form1_Paint(object sender, PaintEventArgs e)
{
    string str = "abcdefgh"
 
    SolidBrush brush = new SolidBrush(Color.Red);
 
    e.Graphics.DrawString(str, this.Font, brush, new PointF(100.0f, 100.0f),
                StringFormat.GenericTypographic);
}


并且Label组件具有与Form相同的Font设置.但是字符串看起来有些不同.


And the Label component has the same Font setting as the Form. But the strings look a little bit different.

推荐答案

可能是因为您System.Drawing.Graphics.DrawString依赖作为参数传递的字体,而不是.因此,我真的不知道您通过事件Paint使用了哪种字体来呈现字符串. (事件的名称为Paint,而不是OnPaint,这是可以替代的虚拟方法的名称.)

如果此线索仍不能帮助您解决此困惑,则可以发布几行代码:创建涉及的字体时的行,指定给属性的行或作为参数传递的行,因此请在行中加上DrawString.如果要添加此代码,请使用改善问题".

—SA
Probably this is because you System.Drawing.Graphics.DrawString relies on the font passed as a parameter, not the font of the Form. By this reason, I don''t really know which font did you use for rendering of your string via the event Paint. (The name of event is Paint, not OnPaint which is the name of the virtual method which you can override, alternatively.)

If this clue did not help you to resolve this confusion yet, you can post few lines of code: the lines when the fonts involved are created, assigned to a property or passed as a parameter, so include the line with DrawString. Please use "Improve question" if you want to add this code.

—SA


[已移动到SA对解决方案发表评论]
使用改善问题",添加评论"或回复评论.
[Moved to comment to the solution by SA]
Use "Improve question", "Add comment" or reply to comment.


标签控件(以及我认为的其他控件)是根据系统设置使用各种文本呈现的.
这是Label的OnPaint方法,它显示正在发生的事情.您可以在自己的代码中模拟相关部分.要更深入,您将需要诸如Reflector之类的工具来挖掘.NET源代码.
The label control (and other controls I assume) are drawn using various text renderings depending on system settings.
This is the OnPaint method of the Label which shows what''s going on. You could emulate the relevant parts in your own code. To go deeper you will need a tool such as Reflector to dig into the .NET source.
protected override void OnPaint(PaintEventArgs e)
{
    Color nearestColor;
    this.Animate();
    Rectangle r = LayoutUtils.DeflateRect(base.ClientRectangle, base.Padding);
    ImageAnimator.UpdateFrames();
    Image image = this.Image;
    if (image != null)
    {
        this.DrawImage(e.Graphics, image, r, base.RtlTranslateAlignment(this.ImageAlign));
    }
    IntPtr hdc = e.Graphics.GetHdc();
    try
    {
        using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc))
        {
            nearestColor = graphics.GetNearestColor(base.Enabled ? this.ForeColor : base.DisabledColor);
        }
    }
    finally
    {
        e.Graphics.ReleaseHdc();
    }
    if (this.AutoEllipsis)
    {
        Rectangle clientRectangle = base.ClientRectangle;
        Size preferredSize = this.GetPreferredSize(new Size(clientRectangle.Width, clientRectangle.Height));
        this.showToolTip = (clientRectangle.Width < preferredSize.Width) || (clientRectangle.Height < preferredSize.Height);
    }
    else
    {
        this.showToolTip = false;
    }
    if (this.UseCompatibleTextRendering)
    {
        using (StringFormat format = this.CreateStringFormat())
        {
            if (base.Enabled)
            {
                using (Brush brush = new SolidBrush(nearestColor))
                {
                    e.Graphics.DrawString(this.Text, this.Font, brush, r, format);
                    goto Label_01BF;
                }
            }
            ControlPaint.DrawStringDisabled(e.Graphics, this.Text, this.Font, nearestColor, r, format);
            goto Label_01BF;
        }
    }
    TextFormatFlags flags = this.CreateTextFormatFlags();
    if (base.Enabled)
    {
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, nearestColor, flags);
    }
    else
    {
        Color foreColor = TextRenderer.DisabledTextColor(this.BackColor);
        TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, foreColor, flags);
    }
Label_01BF:
    base.OnPaint(e);
}


这篇关于标签组件和DrawString之间的文本外观不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆