WPF文字描边的解决方法

简介: 原文:WPF文字描边的解决方法  由于项目原因,今天研究了一下午WPF的文字描边,网上这方面的资料奇少,搞了半天才发现强大的WPF原来不直接支持文字描边啊。
原文: WPF文字描边的解决方法



由于项目原因,今天研究了一下午WPF的文字描边,网上这方面的资料奇少,搞了半天才发现强大的WPF原来不直接支持文字描边啊。最后求助于MSDN,找到了方案,和大家分享一下:


主要思路:FormattedText将字符串转换为Geometry,再在重写的OnRender(DrawingContext drawingContext)方法中绘制Geometry效果如图。


组件的主要属性:

Text属性设置文字

Fill属性设置文本本身的画刷

Stroke属性是描边画刷

StrokeThicknes是描边宽度

其他属性同Label(继承自Label)

XML配置:

<Window x:Class="StrokeableLabelTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpflib="clr-namespace:BLCTClassLibrary.WpfLib;assembly=BLCTClassLibrary.WpfLib"
        Title="MainWindow" Height="422" Width="579">
    <Grid ShowGridLines="True">
        <StackPanel Orientation="Vertical" >
            <Label Margin="10"  Content="以下是StrokeableLabel类(相对轻量级)"/>
            <wpflib:StrokeableLabel Text="测试文本" Fill="Yellow" Stroke="Black" StrokeThickness="0.3" FontWeight="Bold" FontSize="50"/>
            <wpflib:StrokeableLabel Text="测试文本" Fill="Yellow" Stroke="Red" StrokeThickness="0.7" FontWeight="DemiBold" FontSize="50">
                <wpflib:StrokeableLabel.Effect>
                    <DropShadowEffect Color="Black" BlurRadius="15" RenderingBias="Quality" Direction="290" ShadowDepth="5" Opacity="1"  />
                </wpflib:StrokeableLabel.Effect>
            </wpflib:StrokeableLabel>
            <wpflib:StrokeableLabel Text="测试文本" Fill="White" StrokeThickness="2" FontWeight="Bold" FontSize="50">
                <wpflib:StrokeableLabel.Stroke>
                    <LinearGradientBrush>
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Color="Blue" Offset="0.2"/>
                            <GradientStop Color="Brown" Offset="0.3"/>
                            <GradientStop Color="PowderBlue" Offset="0.7"/>
                            <GradientStop Color="Red" Offset="1"/>
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </wpflib:StrokeableLabel.Stroke>
            </wpflib:StrokeableLabel>
            <wpflib:StrokeableLabel Text="测试文本" Stroke="red" StrokeThickness="2"  FontWeight="Bold" FontSize="50">
                <wpflib:StrokeableLabel.Fill>
                    <ImageBrush ImageSource="/StrokeableLabelTest;component/Images/20085385922474_2.jpg" />
                </wpflib:StrokeableLabel.Fill>
            </wpflib:StrokeableLabel>
            <wpflib:StrokeableLabel Fill="Transparent" FontSize="50" FontWeight="Light" StrokeThickness="8" Text="测试文本" >
                <wpflib:StrokeableLabel.Stroke>
                    <ImageBrush ImageSource="/StrokeableLabelTest;component/Images/05.jpg" />
                </wpflib:StrokeableLabel.Stroke>
            </wpflib:StrokeableLabel>
        </StackPanel>

    </Grid>
</Window>


库核心代码:

        private void getformattedText(string str)
        {
            // Create the formatted text based on the properties set.
            FormattedText formattedText = new FormattedText(
                str,
                CultureInfo.GetCultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface(
                    FontFamily,
                    FontStyle,
                    FontWeight,
                    FontStretch),
                FontSize,
                System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text. 
                );

            this.Width = formattedText.Width;
            this.Height = formattedText.Height;
            // Build the geometry object that represents the text.
            //pg.AddGeometry(formattedText.BuildGeometry(new System.Windows.Point(5, 5)));
            TextGeometry = formattedText.BuildGeometry(new System.Windows.Point(0,0));
            // Build the geometry object that represents the text hightlight.
            if (Highlight == true)
            {
                TextHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0));
            }
        }

        /// <summary>
        /// OnRender override draws the geometry of the text and optional highlight.
        /// </summary>
        /// <param name="drawingContext">Drawing context of the OutlineText control.</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            CreateText();
            // Draw the outline based on the properties that are set.
            drawingContext.DrawGeometry(Fill, new System.Windows.Media.Pen(Stroke, StrokeThickness), TextGeometry);
            // Draw the text highlight based on the properties that are set.
            if (Highlight == true)
            {
                drawingContext.DrawGeometry(null, new System.Windows.Media.Pen(Stroke, StrokeThickness), TextHighLightGeometry);
            }
        }

具体代码请参照如下资源:

http://download.csdn.net/detail/wblct/8697005

需要2分的资源分,快没分了,大家谅解啊。

目录
相关文章
|
C# Windows
WPF Windows背景透明其中的文字保持不透明
原文:WPF Windows背景透明其中的文字保持不透明 版权声明:本文为博主原创,未经允许不得转载。交流、源码资料加群:161154103 https://blog.
1726 0
|
算法 容器 数据可视化
WPF_界面_图片/界面/文字模糊解决之道整理
原文:WPF_界面_图片/界面/文字模糊解决之道整理 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010265681/article/details/76651792 图片模糊: 图片尺寸:  检查图片,png, DPI=72,Stretch="None",原图尺寸和xaml里面写的尺寸一致。
1200 0
|
C#
好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字
原文:好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.
987 0
|
C# 开发者
WPF 程序无法触摸操作?我们一起来找原因和解决方法!
原文:WPF 程序无法触摸操作?我们一起来找原因和解决方法! 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名吕毅(包含链接:http://blog.csdn.net/wpwalter/),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
1250 0
|
C#
WPF图片放大后模糊的解决方法
原文:WPF图片放大后模糊的解决方法 WPF中显示图片的方式很多,可以用Image控件来显示图像,或者直接设置一个控件的Background。
1284 0
|
C#
【C#/WPF】TextBlock/TextBox/Label编辑文字的问题
原文:【C#/WPF】TextBlock/TextBox/Label编辑文字的问题 标题有点描述不清,就当是为了方便自己用时易于搜索到。
1345 0
|
C# Windows
WPF中的文字修饰——上划线,中划线,基线与下划线
原文:WPF中的文字修饰——上划线,中划线,基线与下划线 我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。
1468 0
|
C#
WPF控件TextBlock中文字自动换行
原文:WPF控件TextBlock中文字自动换行 在很多的WPF项目中,往往为了追求界面的美观,需要控制控件中文字的换行显示,现对TextBlock控件换行的实现方式进行总结,希望大家多多拍砖!!! 1.
2763 0
|
11天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
4月前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
59 1