如何扩展 TextBlock 以显示轮廓文本? [英] How can I extend a TextBlock to display Outlined Text?
问题描述
我的问题与这个问题有关.
这个问题已经过时了,答案更是如此,感觉有点缺乏.用户从头开始创建了一个有点缺乏的控件,但最大的一点是感觉没有必要(至少对我而言)创建一个全新的控件以获得可以支持的 TextBlock描边(轮廓)文本.
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.
我试图通过提供 Brush 属性和 double 属性来扩展 TextBlock 控件.我曾希望我可以覆盖 OnRender 方法,但它是密封的,所以我不能.
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 ); }
}
我花了一些时间查看 TextBlock 控件,以便跟踪它在屏幕上实际将字符串呈现为文本的位置(我想是否可以找到我可以复制该方法),但我希望有人可能碰巧已经知道答案并为我节省了一些时间,因为 TextBlock 控件只是......疯了.
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.
那么 - 我是否可以扩展此 TextBlock 以便我可以随意描边文本?
So - is it possible for me to extend this TextBlock so that I can stroke the text like I want?
为了清楚起见,似乎对我要做什么存在误解 - 我不在乎概述文本块.我想概述TEXT本身.
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.
推荐答案
是这样的:
<TextBlock >
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="0"
Color="Red"
Opacity="1"
BlurRadius="5"/>
</TextBlock.Effect>
Some text that we want outlined
</TextBlock>
使用 DropShadowEffect 模拟发光/轮廓效果.
Using DropShadowEffect to simulate the Glow/Outline effect.
这篇关于如何扩展 TextBlock 以显示轮廓文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!