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分的资源分,快没分了,大家谅解啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值