文本在 WPF 窗口和 ElementHost 内的呈现方式不同

发布于 2024-10-12 06:18:12 字数 700 浏览 5 评论 0原文

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

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

--这是 WPF 窗口上的简单标签:
WPF 窗口 http://img525.imageshack.us/img525/7049/wpfwindow.png< /a>

-- 当 WPF 用户控件托管在 WinForms 中时显示相同的标签:
WPF 互操作

互操作变体完全不同:字符间间隔为零,并且文本看起来比纯 WPF 变体更粗体。当普通文本可以接受时,粗体文本就很难看了。

有什么想法如何解决它吗?

先感谢您!

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:
WPF Interop

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!

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

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

发布评论

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

评论(1

原来是傀儡 2024-10-19 06:18:12

(1 月 18 日编辑补充说,这不仅仅是字体渲染模式,还包括字体本身。)

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

您的 Windows 窗体主机应用程序强制使用默认字体系列“Microsoft Sans Serif”,字体大小为 8.25pt,在 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"

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

默认情况下,WPF 会尝试维护比 Win32 或 GDI+ 更忠实于原始字体设计。它还以一种一致缩放的方式呈现文本 - 增加字体大小,例如 12% 将使屏幕上文本的宽度增大 12%。在 Win32 或 GDI+ 中情况并非如此,您会得到相当复杂的非线性变化。

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

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

(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.

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