文本在 WPF 窗口和 ElementHost 内部的呈现方式不同 [英] Text renders differently on WPF window and inside ElementHost

查看:36
本文介绍了文本在 WPF 窗口和 ElementHost 内部的呈现方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WPF 互操作性在 WinForms 表单中托管 WPF 用户控件.除了丑陋的文本渲染外,一切正常:

 <标签内容=普通文本"名称=labelNormal"/><标签内容=粗体文本"名称=labelBold"FontWeight=粗体"/>

-- 这是 WPF 窗口上的简单标签:

Interop 变体完全不同:字符间隔为零,文本看起来比纯 WPF 变体更粗.当普通文本可以接受时,粗体文本是丑陋的.

有什么解决办法吗?

先谢谢你!

解决方案

(在 1 月 18 日进行了编辑以补充说它不仅仅是字体渲染模式,还有字体本身.)

这里有两个相关因素:字体和字体格式模式.

您的 Windows 窗体主机应用程序正在以 8.25pt 的字体大小强加Microsoft Sans Serif"的默认字体系列,在 WPF 的度量单位中,FontSize 为 11.但 WPF 通常会使用不同的字体 - 我使用默认 Aero 主题运行 Windows 7,WPF 默认为 Segoe UI,FontSize 为 12.

所以您在这里看到不同结果的最大原因是它们是两种不同的字体.将 FontFamily="Segoe UI" FontSize="12" 添加到 UserControl 根元素使这两个 WPF 示例在我看来更加一致.(但这当然会使您的 WPF 文本看起来与包含 Windows 窗体应用程序中的文本不一致.这就是 ElementHost 将 Windows 窗体字体选择传播到 WPF 内容的全部原因.)

在我编辑这个之前,我认为这可能归结为 WPF 文本渲染的 IdealDisplay 模式之间的差异.意识到这主要是关于字体后,我不再认为是这种情况,但我将把对另一个问题的讨论留在这里,因为它对于任何试图让他们的 WPF 文本看起来与他们的 Windows 保持一致的人来说仍然有用形成文本.WPF 的默认值是 Ideal,但如果您在 Windows 窗体应用程序中运行,可以说 Display 更好,因为这会使它看起来与 Windows 窗体通常呈现的方式一致东西.

您可以通过添加以下内容在 WPF 中基于每个元素来控制它:

TextOptions.TextFormattingMode="显示"

(或 "Display" 取决于您想要的模式).这个可附加属性是在 WPF v4 中添加的,让您可以在 WPF 自首次发布以来具有的可扩展但略微模糊的文本渲染以及 Win32 和 GDI+(因此 Windows表格)使用.这将影响您应用它的元素,以及任何后代.(例如,如果您在 StackPanel 上设置它,它应该应用于该面板中的所有元素,除非您还在子项上将其设置为本地不同的值.)

默认情况下,与 Win32 或 GDI+ 相比,WPF 会尽量保持对原始字体设计的更高保真度.它还以某种方式呈现文本,这意味着它可以始终如一地缩放 - 将字体大小增加 12% 将使文本在屏幕上的宽度增加 12%.在 Win32 或 GDI+ 中情况并非如此,您会得到更复杂的非线性变化.

但许多人抱怨说,为了换取更好的保真度,模糊度会增加.这就是 WPF 4 引入该新属性的原因.将其设置为 Display 以获得较低保真度但更清晰的旧样式渲染.

由于您可以在每个元素的基础上选择它,因此您可以为粗体和普通文本独立选择您喜欢的外观.

I use WPF interoperability to host WPF user control inside WinForms form. Everything works OK except ugly text rendering:

    <Label Content="Normal text" Name="labelNormal"/>
    <Label Content="Bold text" Name="labelBold" FontWeight="Bold" />

-- here is simple Labels on WPF Window:
WPF Window http://img525.imageshack.us/img525/7049/wpfwindow.png

-- and the same labels shown when the WPF user control is hosted in WinForms:

Interop variant is quite different: intercharacter interval is zero and the the text looks bolder that pure WPF variant. When normal text is acceptable, bold text is ugly.

Are there any ideas how to fix it?

Thank you in advance!

解决方案

(edited Jan 18th to add that it's not just the font rendering mode, but also the font itself.)

There are two relevant factors here: the font, and the font formatting mode.

Your Windows Forms host application is imposing a default font family of "Microsoft Sans Serif" at a font size of 8.25pt, which in WPF's measurement units is a FontSize of 11. But WPF will typically use a different font - I'm running Windows 7 with the default Aero theme, and WPF defaults to Segoe UI with a FontSize of 12.

So the biggest reason you're seeing different results here is that those are two different fonts. Adding FontFamily="Segoe UI" FontSize="12" to the UserControl root element makes the two WPF examples look more consistent to me. (But this will of course make your WPF text look less consistent with text in the containing Windows Forms app. That's the whole reason the ElementHost propagates the Windows Forms font choice into the WPF content.)

Before I edited this, I had thought that it might be down to the difference between the Ideal and Display modes for WPF text rendering. Having realised it's mainly about the font, I no longer think this is the case, but I'm going to leave the discussion of this other issue in here because it's still useful for anyone trying to get their WPF text to look consistent with their Windows Forms text. The default for WPF is Ideal, but if you're running in a Windows Forms app, arguably Display is better because that would make it look consistent with how Windows Forms normally renders things.

You can control this in WPF on a per-element basis by adding this:

TextOptions.TextFormattingMode="Display"

(or "Display" depending on which mode you want). This attachable property was added in WPF v4, to let you choose between the scalable-but-slightly-blurry text rendering WPF has had since its first release, and the distorted-but-sharp grid fitting rendering that Win32 and GDI+ (and therefore Windows Forms) use. That will affect the element you apply it to, and also any descendants. (E.g., if you set this on a StackPanel it should apply to all the elements in that panel, unless you also set it to a different value locally on the children.)

By default, WPF tries to maintain better fidelity to the original typeface design than Win32 or GDI+. And it also renders text in a way that means it scales consistently - increasing the font size by, say 12% will make the width of the text 12% larger on screen. That's not the case in Win32 or GDI+ where you get rather more complex non-linear changes.

But many people complained about the increase in bluriness you get in exchange for the better fidelity. So that's why WPF 4 introduced that new property. Set it to Display to get the lower-fidelity but sharper old style rendering.

Since you can choose it on a per-element basis, you can pick whichever you like the look of independently for you bold and your normal text.

这篇关于文本在 WPF 窗口和 ElementHost 内部的呈现方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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