我要创建wavy underlines使用 TextDecoration(在类似于 RichTextBox 的控件中)。
我做了以下事情:
private static Pen CreateErrorPen() {
var geometry = new StreamGeometry();
using (var context = geometry.Open()) {
context.BeginFigure(new Point(0.0, 0.0), false, false);
context.PolyLineTo(new[] {
new Point(0.75, 0.75),
new Point(1.5, 0.0),
new Point(2.25, 0.75),
new Point(3.0, 0.0)
}, true, true);
}
var brushPattern = new GeometryDrawing {
Pen = new Pen(Brushes.Red, 0.2),
Geometry = geometry
};
var brush = new DrawingBrush(brushPattern) {
TileMode = TileMode.Tile,
Viewport = new Rect(0.0, 1.5, 9.0, 3.0),
ViewportUnits = BrushMappingMode.Absolute
};
var pen = new Pen(brush, 3.0);
pen.Freeze();
return pen;
}
这几乎有效,但根据文本中带下划线的单词位置,下划线通常显示为多个叠加波浪的图案。即使下划线是正确的,下划线也有点模糊(我想是像素之间绘制的 wpf 问题)。
我的解决方案是一种反复试验,所以我可能走错了路,尤其是使用 Viewport/ViewportUnits。
我做错了什么,有没有办法获得清晰的下划线?
提前致谢。
最佳答案
bstoney 有此问题的解决方案here .关键似乎是设置 Viewbox
以及 Viewport
这样波浪垂直分开,所以你只能在下划线中得到 1。
波浪中有一些中断可以通过将其向右扩展并更改 Viewbox
来消除。 from 所以它从 X=1 而不是 0 开始:
<VisualBrush x:Key="WavyBrush" TileMode="Tile" Viewbox="1,0,3,3" ViewboxUnits="Absolute" Viewport="0,-1,6,4" ViewportUnits="Absolute">
<VisualBrush.Visual>
<Path Data="M 0,1 C 1,0 2,2 3,1 4,0 5,2 6,1" Stroke="Red" StrokeThickness="0.2"/>
</VisualBrush.Visual>
</VisualBrush>
关于wpf - 波浪下划线作为 TextDecoration : what I am doing wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/633678/