波浪下划线作为 TextDecoration:我做错了什么?
我想使用 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 时。
我做错了什么,有没有办法获得清晰的下划线?
提前致谢。
I want to create wavy underlines using TextDecoration (in a control similar to RichTextBox).
I did the following thing:
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;
}
This almost works, but depending on the underlined word position in text, underlines often show up as a pattern of several superimposed waves. Also the underlines are a bit blurry even when they are correct (wpf problem with drawing between pixels, I suppose).
My solution was kind of a trial-and-error, so I might have gone a wrong way, especially with Viewport/ViewportUnits.
What am I doing wrong and is there a way to get crisp underlines?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
bstoney 在此处。 关键似乎是设置
Viewbox
以及Viewport
以使波浪垂直分开,因此下划线中只能得到 1。波浪中存在一些中断,可以通过将其向右延伸并更改
Viewbox
来消除,使其从 X=1 而不是 0 开始:bstoney had a solution to this problem here. The key seems to be setting the
Viewbox
as well as theViewport
such that the waves are seperated vertically, so you only get 1 in the underline.There are some breaks in the wave that can be eliminated by extending it to the right and changing the
Viewbox
from so it starts from X=1 instead of 0: