WPF 文本框设置了阴影效果后,因左右的transform变化引发的拉伸渲染问题
背景
最近遇到一个动画执行时,文本位置变化的问题。如下图:

如果你仔细看的话,当星星变小时,文本往下降了几个像素。
貌似有点莫名其妙,因为控件之间并不在同一个Panel布局控件中,不存在高度限制变化引发此类问题。所以有了如下测试
测试场景
字体类型影响
1. 首先新建了一个空项目,前面是一个带阴影的文本,后面用一张普通图片循环变更它的高度。尝试了下,还是会移动Y轴的像素
影响很大
2. 后面使用用普通的布局控件Grid代替。依然如此
影响较大
所以此问题不是图片动画造成的。
3. 于是,我再添加个按钮,测试带阴影的非文本控件

只有文本被影响了,按钮不会被影响?
我们使用放大镜,放大到500%,发现按钮中的文本,上下位置其实还是会有细微的变化 。
所以,按钮等控件其实也是会被影响的。只是幅度较小。
5. 给按钮设置,被影响文本同样的字体系列。

按钮也被影响了。。。所以,是字体原因!那么,这种字体类型是什么呢?
当前字体: FontFamily="Microsoft YaHei Bold"。
而上一步操作4中,按钮的字体类型是默认字体,即为Microsoft YaHei UI。
所以Microsoft YaHei Bold的影响比Microsoft YaHei UI大很多?
6. 我们回到只有文本的测试模式
影响较大
影响较小
所以,我们可以得出是Y轴位置变化,的确与字体类型有关。


变动很大

变动很小,使用放大镜500%才能看到细微的变化
通过如上测试,发现只有微软雅黑UI字体类型,影响较小。并且在步骤6中,测试通的是没有设置字体类型的,没有设置字体类型,其实默认是 Microsoft YaHei UI。所以字体类型影响相对较小的是Microsoft YaHei UI
字体大小
根据上述的字体类型测试,我们添加俩个文本框,使用Microsoft YaHei UI作为字体类型,设置字体大小分别为30和60。

通过如上对比,发现字体大小30的文本,受到的影响很明显。字体为60的文本,受到的影响较小。
综上,得出的结论是,Y轴变化的幅度,与字体类型、字体大小有关。具体的详细幅度,有待确认~~~
显示区域影响
单个影响因素
我们将高度变换的区域移动下位置,也不会有影响。
测试通过
多个影响因素
有多个影响因素时,不要设置在左右,否则也有影响。
测试不通过
测试通过
Demo前端代码:
<Grid>
<Border VerticalAlignment="Center" BorderBrush="Red" BorderThickness="0 1 0 0"></Border> <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="80" Width="60" Margin="-460 0 0 0">
<Grid x:Name="StoryControl" Background="Red"
Height="30" Width="30" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5"/>
</Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="80" Width="60" Margin="460 0 0 0">
<Grid x:Name="StoryControl1" Background="Red"
Height="30" Width="30" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5"/>
</Grid> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock x:Name="TestTextBlock0" VerticalAlignment="Center" HorizontalAlignment="Center"
Text="YaHei Bold" Foreground="White" LineHeight="18" FontSize="60" FontFamily="Microsoft YaHei Bold">
<TextBlock.Effect>
<DropShadowEffect Color="#000000" BlurRadius="4" ShadowDepth="2" Opacity="0.24"/>
</TextBlock.Effect>
</TextBlock>
</StackPanel>
</Grid>
阴影效果
再尝试将阴影效果删除,也不会有影响
测试通过
重现步骤
1.添加一个文本/按钮控件
2.此显示控件设置阴影(条件一)
3.此显示控件设置字体类型FontFamily=“Microsoft YaHei Bold”(影响因素,不是条件),如下
1 <TextBlock x:Name="TestTextBlock1" VerticalAlignment="Center" HorizontalAlignment="Center"
2 Text="微软雅黑加粗" Foreground="White" LineHeight="18" FontSize="60" FontFamily="Microsoft YaHei Bold">
3 <TextBlock.Effect>
4 <DropShadowEffect Color="#000000" BlurRadius="4" ShadowDepth="2" Opacity="0.24"/>
5 </TextBlock.Effect>
6 </TextBlock>
4.在此显示控件的显示区域,变更其它控件的高度(条件二)。
完整案例如下:
1 <Window x:Class="TextBlockShadowEffectForStoryBoardDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:local="clr-namespace:TextBlockShadowEffectForStoryBoardDemo"
7 mc:Ignorable="d" Title="MainWindow" Height="600" Width="800" Background="LightGray">
8 <Window.Resources>
9 <Storyboard x:Key="Storyboard.ChangeHeight" DesiredFrameRate="20">
10 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="StoryControl" Storyboard.TargetProperty="Height" RepeatBehavior="Forever">
11 <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0" />
12 <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="15" />
13 <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="30" />
14 <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="30" />
15 <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="15" />
16 <EasingDoubleKeyFrame KeyTime="0:0:1.0" Value="0" />
17 <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0" />
18 </DoubleAnimationUsingKeyFrames>
19 </Storyboard>
20 </Window.Resources>
21 <Grid>
22 <Border VerticalAlignment="Center" BorderBrush="Red" BorderThickness="0 1 0 0"></Border>
23
24 <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="80" Width="60" Margin="0 60 0 0">
25 <Grid x:Name="StoryControl" Background="Red"
26 Height="30" Width="30" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5"/>
27 </Grid>
28 <TextBlock x:Name="TestTextBlock1" VerticalAlignment="Center" HorizontalAlignment="Center"
29 Text="微软雅黑加粗" Foreground="White" LineHeight="18" FontSize="60" FontFamily="Microsoft YaHei Bold">
30 <TextBlock.Effect>
31 <DropShadowEffect Color="#000000" BlurRadius="4" ShadowDepth="2" Opacity="0.24"/>
32 </TextBlock.Effect>
33 </TextBlock>
34 </Grid>
35 </Window>
1 /// <summary>
2 /// MainWindow.xaml 的交互逻辑
3 /// </summary>
4 public partial class MainWindow : Window
5 {
6 public MainWindow()
7 {
8 InitializeComponent();
9 this.Loaded += MainWindow_Loaded;
10 }
11
12 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
13 {
14 var storyboard = Resources["Storyboard.ChangeHeight"] as Storyboard;
15 storyboard?.Begin();
16 }
17 }
界面显示:

测试Demo请点击下载:https://files.cnblogs.com/files/kybs0/TextBlockShadowEffectDemo.zip
解决方案
TextOptions.TextFormattingMode,有两种设置:
- Ideal —— WPF4之前的模式
- Display —— 新的模式,可以使字体显示更清晰。
TextOptions.TextFormattingMode="Display",可以提高字体的清晰度。
测试OK
TextOptions.TextFormattingMode是依赖属性,设置后子元素等都能解决此类问题。
WPF 文本框设置了阴影效果后,因左右的transform变化引发的拉伸渲染问题的更多相关文章
- WPF 设置了阴影效果后,Y轴位置会有变化的问题
原文:WPF 设置了阴影效果后,Y轴位置会有变化的问题 背景 最近遇到一个动画执行时,文本位置变化的问题.如下图: 如果你仔细看的话,当星星变小时,文本往下降了几个像素. 貌似有点莫名其妙,因为控件之 ...
- WPF 文本框添加水印效果
有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...
- WPF文本框密码框添加水印效果
WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...
- input文本框设置和移除默认值
input文本框设置和移除默认值 这里想实现的效果是:设置和移除文本框默认值,如下图鼠标放到文本框中的时候,灰字消失. 1.可以用简单的方式,就是给input文本框加上onfocus属性,如下代码: ...
- HTML input文本框设置和移除默认值
这里想实现的效果是:设置和移除文本框默认值,如下图鼠标放到文本框中的时候,灰字消失. 1.可以用简单的方式,就是给input文本框加上onfocus属性,如下代码: <input id=&quo ...
- js中input文本框设置和移除默认值
这里想实现的效果是:设置和移除文本框默认值,如下图鼠标放到文本框中的时候,灰字消失. 1.可以用简单的方式,就是给input文本框加上onfocus属性,如下代码: <input id=&quo ...
- html之给文本框设置宽度和高度/input的无边框效果
<input name="" type="text" style="width:200px; height:20px;" /> ...
- MFC~~~~~~edit_control 和 静态文本框设置透明和字体设置
一 字体设置比较简单~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 .在 testdlg.h(假设工程名字为test)中 设置一个 public 的 ...
- css 好看的div文本框 渐变+ 背景 + 阴影 实际应用
效果图 css <style> .box{ padding: 3px 5px 3px 18px; margin: 3px 0 3px 5px; position: relative; li ...
随机推荐
- 程序执行流程/布尔类型与布尔:运算猜数字游戏;库的使用:turtle
myPrice = 6 while True: guess = int(input()) if guess > myPrice: print('>') elif guess < my ...
- 使用kolin开发你的android应用
转载请注明出处,谢谢! 前段时间花了大概三周时间学习了kotlin,借着kotlin正好发布1.2,使用kotlin撸了一个android demo Github地址:https://github.c ...
- rest_framework之认证源码剖析
如果我们写API有人能访问,有人不能访问,则需要些认证. 如何知道该用户是否已登入? 如果用户登入成功,则给用户一个随机字符串,去访问另一个页面. 以前写session的时候,都是把session写c ...
- Android找回密码功能 手机找回、邮箱找回
找回密码功能设计:https://blog.csdn.net/qq_33472765/article/details/82287404?utm_source=blogxgwz0 手机找回:https: ...
- topic的leader显示为none的解决办法
1.查看kafka的topic详细信息 bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic test --describe 配置delete. ...
- C# 控制台应用程序中输出彩色字体
using System; class Example { public static void Main() { // Get a string array with the names of Co ...
- 深入理解Spring Redis的使用 (五)、常见问题汇总
目前我所知道的Redistemplate里面,我没有使用到的就是管道.这个可以进行批量的读写.类似于jdbc的batch.还有就是Redis的集群部署.但是由于我业务里没有这种需求,所以没有使用无法给 ...
- [Swift]LeetCode112. 路径总和 | Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [Swift]LeetCode257. 二叉树的所有路径 | Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [Swift]LeetCode891. 子序列宽度之和 | Sum of Subsequence Widths
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the ...