WPF 入门教程文本渲染介绍

92 篇文章 24 订阅
92 篇文章 18 订阅

在本文中,我们将讨论为什么使用 WPF 有时会使文本渲染得更模糊、后来如何解决这个问题以及如何自己控制文本渲染。

正如在本教程中已经提到的,与 WinForms 等其他 UI 框架相比,WPF 本身可以做更多的事情,WinForms 将在很多很多事情上使用 Windows API。这在文本渲染方面也很明显——WinForms 使用来自 Windows 的 GDI API,而 WPF 有自己的文本渲染实现,以更好地支持动画以及 WPF 的设备无关特性。

不幸的是,这导致文本变得有点模糊,尤其是小字体。一段时间以来,这对 WPF 程序员来说是一个相当大的问题,但幸运的是,微软在 .NET 框架 4.0 版中对 WPF 文本渲染引擎进行了大量改进。这意味着如果您使用此版本或更高版本,您的文本应该几乎与像素完美一样好。

控制文本呈现

在 .NET framework 4.0 中,Microsoft 还决定通过引入 具有TextFormattingModeTextRenderingMode属性的TextOptions类,为程序员提供更多的文本呈现控制权。这允许您专门决定如何在控件级别上格式化和呈现文本。这可能最好通过示例来说明,因此请查看下面的代码和屏幕截图,了解如何使用这些属性影响文本呈现。

文本格式模式

使用 TextFormattingMode 属性,您可以决定在格式化文本时应使用哪种算法。您可以在Ideal(默认值)和Display之间进行。您通常希望保持此属性不变,因为理想设置最适合大多数情况,但在需要渲染非常小的文本的情况下,显示设置有时会产生更好的结果。这是一个您可以看到差异的示例:

<Window x:Class="WpfTutorialSamples.Control_concepts.TextFormattingModeSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextFormattingModeSample" Height="200" Width="400">
    <StackPanel Margin="10">
        <Label TextOptions.TextFormattingMode="Ideal" FontSize="9">TextFormattingMode.Ideal, small text</Label>
        <Label TextOptions.TextFormattingMode="Display" FontSize="9">TextFormattingMode.Display, small text</Label>
        <Label TextOptions.TextFormattingMode="Ideal" FontSize="20">TextFormattingMode.Ideal, large text</Label>
        <Label TextOptions.TextFormattingMode="Display" FontSize="20">TextFormattingMode.Display, large text</Label>
    </StackPanel>
</Window>

文本渲染模式

TextRenderingMode属性让你控制哪些抗锯齿算法渲染文本时使用的。它与TextFormattingMode属性的Display设置结合使用的效果最大,我们将在本示例中使用它来说明差异:

<Window x:Class="WpfTutorialSamples.Control_concepts.TextRenderingModeSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextRenderingModeSample" Height="300" Width="400">
    <StackPanel Margin="10" TextOptions.TextFormattingMode="Display">
        <Label TextOptions.TextRenderingMode="Auto" FontSize="9">TextRenderingMode.Auto, small text</Label>
        <Label TextOptions.TextRenderingMode="Aliased" FontSize="9">TextRenderingMode.Aliased, small text</Label>
        <Label TextOptions.TextRenderingMode="ClearType" FontSize="9">TextRenderingMode.ClearType, small text</Label>
        <Label TextOptions.TextRenderingMode="Grayscale" FontSize="9">TextRenderingMode.Grayscale, small text</Label>
        <Label TextOptions.TextRenderingMode="Auto" FontSize="18">TextRenderingMode.Auto, large text</Label>
        <Label TextOptions.TextRenderingMode="Aliased" FontSize="18">TextRenderingMode.Aliased, large text</Label>
        <Label TextOptions.TextRenderingMode="ClearType" FontSize="18">TextRenderingMode.ClearType, large text</Label>
        <Label TextOptions.TextRenderingMode="Grayscale" FontSize="18">TextRenderingMode.Grayscale, large text</Label>
    </StackPanel>
</Window>

如您所见,生成的文本在外观上有很大不同,再次强调,您应该主要在特殊情况下进行更改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值