简体   繁体   中英

How can I extend a TextBlock to display Outlined Text?

My question relates to this question.

This question is quite dated, and the answer more so and it feels like it is lacking somewhat. The user created a control from scratch that is lacking somewhat, but the biggest point is that it doesn't feel like it should be necessary ( to me, at least ) to create an entirely new control in order to have a TextBlock that can support stroked ( outlined ) text.

I'm trying to extend the TextBlock control by providing a Brush property and a double property. I had hoped I could override the OnRender method but it's sealed, so I can't.

This is what I have so far ( it's not much ) :

public class StrokedText: TextBlock {
    public static readonly DependencyProperty
        StrokeProperty = DependencyProperty.Register(
            "Stroke",
            typeof( Brush ),
            typeof( StrokedText ),
            new PropertyMetadata(
                Brushes.Red,
                ( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) ),
        StrokeWidthProperty = DependencyProperty.Register(
            "StrokeWidth",
            typeof( double ),
            typeof( StrokedText ),
            new PropertyMetadata(
                2.0D,
                ( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) );

    /// <summary>
    /// Get or Set Stroke Brush.
    /// </summary>
    public Brush Stroke {
        get { return this.GetValue( StrokeProperty ) as Brush; }
        set { this.SetValue( StrokeProperty, value ); }
    }

    /// <summary>
    /// Get or Set Stroke Width
    /// </summary>
    public double StrokeWidth {
        get { return ( double )this.GetValue( StrokeWidthProperty ); }
        set { this.SetValue( StrokeWidthProperty, value ); }
    }

I've been spending some time looking at the TextBlock control in order to trace where it actually renders the string as text on the screen ( I figured if I could find that I could copy the method ), but I was hoping someone might happen to already know the answer and save me some time as the TextBlock control is just... insane.

So - is it possible for me to extend this TextBlock so that I can stroke the text like I want?


EDIT 1 :

For clarity, there seems to have been a misunderstanding as to for what I am going - I do not care about outlining the text block. I want to outline the TEXT itself.

Something like this:

<TextBlock >
    <TextBlock.Effect>
        <DropShadowEffect ShadowDepth="0"
                    Color="Red"
                    Opacity="1"
                    BlurRadius="5"/>
    </TextBlock.Effect>
    Some text that we want outlined
</TextBlock>

Using DropShadowEffect to simulate the Glow/Outline effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM