柏林春天的专栏



OnRender(DrawingContext drawingContext)

组件的主要属性:

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

Stroke属性是描边画刷

StrokeThicknes是描边宽度

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

XML配置:

<Window x:Class="StrokeableLabelTest.MainWindow"xmlns=""xmlns:x=""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);}}

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

,偶尔因高山流水的美丽停留,

柏林春天的专栏

相关文章:

你感兴趣的文章:

标签云: