C# 为什么装饰层不能在WPF文本框上正确渲染?
我想在文本框的特定文本下绘制波浪线图案。但装饰层中的图案绘制不正确 示例链接: 在下图中,您可以看到,波浪线图案在许多地方没有正确渲染。以绿色突出显示的是一些正确渲染的图案 如果我画了一条简单的线,也不会正确渲染。请注意下图。以绿色亮显的是一些正确渲染的线。C# 为什么装饰层不能在WPF文本框上正确渲染?,c#,.net,wpf,C#,.net,Wpf,我想在文本框的特定文本下绘制波浪线图案。但装饰层中的图案绘制不正确 示例链接: 在下图中,您可以看到,波浪线图案在许多地方没有正确渲染。以绿色突出显示的是一些正确渲染的图案 如果我画了一条简单的线,也不会正确渲染。请注意下图。以绿色亮显的是一些正确渲染的线。 protected override void OnRender(DrawingContext DrawingContext) { TextBox TextBox=this.AdornedElement作为TextBox; //Word
protected override void OnRender(DrawingContext DrawingContext)
{
TextBox TextBox=this.AdornedElement作为TextBox;
//WordList将单词的起始索引和单词存储为字典集合。
Clear();
//SetRedUnderline方法将提供需要加下划线的单词列表。
SetRedUnderline(textBox.Text);
//pathPen用于在起点和终点绘制带有方形封口的直线。
笔路径笔=新笔(新SolidColorBrush(Colors.Red),0.2;
pathPen.EndLineCap=PenLineCap.Square;
pathPen.StartLineCap=PenLineCap.Square;
//pathGeometry用于使用BezierSegment绘制波浪线段。
点路径开始=新点(0,1);
BezierSegment路径段=新BezierSegment(新点(1,0)、新点(2,2)、新点(3,1),真);
PathFigure PathFigure=新的PathFigure(pathStart,新的PathSegment[]{PathSegment},false);
PathGeometry PathGeometry=新的PathGeometry(新的PathFigure[]{PathFigure});
//squigglyBrush是自定义的,用于为pathGeometry的平铺加暗。
DrawingBrush SwigglyBrush=新的DrawingBrush();
Viewport=newrect(0,0,6,4.8);
squigglyBrush.ViewportUnits=BrushMappingMode.Absolute;
squigglyBrush.TileMode=TileMode.Tile;
squigglyBrush.Drawing=新几何图形绘制(null、pathPen、pathGeometry);
foreach(单词列表中的KeyValuePair KeyValuePair)
{
Rect startPosition=textBox.GetRectFromCharacterIndex(keyValuePair.Key);
Rect endPosition=textBox.GetRectFromCharacterIndex(keyValuePair.Key+keyValuePair.Value.ToString().Length-1,true);
Rect rectUnion=Rect.Union(起始位置,结束位置);
drawingContext.DrawLine(新钢笔(SwigglyBrush,4),rectUnion.BottomLeft,rectUnion.BottomRight);
//drawingContext.DrawLine(新笔(画笔:红色,1),rectUnion.BottomLeft,rectUnion.BottomRight);
}
}
谢谢 如果用画笔填充一个四像素高的矩形,而不是用钢笔画一条线,会发生什么?
protected override void OnRender(DrawingContext drawingContext)
{
TextBox textBox = this.AdornedElement as TextBox;
//WordList stores the start index of a word and word as a dictionary collection.
wordList.Clear();
// SetRedUnderline method will provide the wordList which needs to be underline.
SetRedUnderline(textBox.Text);
//pathPen is used to draw line with square cap at start and end.
Pen pathPen = new Pen(new SolidColorBrush(Colors.Red), 0.2);
pathPen.EndLineCap = PenLineCap.Square;
pathPen.StartLineCap = PenLineCap.Square;
//pathGeometry is used to draw segment of wave line by usig BezierSegment.
Point pathStart = new Point(0, 1);
BezierSegment pathSegment = new BezierSegment(new Point(1, 0), new Point(2, 2), new Point(3, 1), true);
PathFigure pathFigure = new PathFigure(pathStart, new PathSegment[] { pathSegment }, false);
PathGeometry pathGeometry = new PathGeometry(new PathFigure[] { pathFigure });
//squigglyBrush is customized to darw a tile of pathGeometry.
DrawingBrush squigglyBrush = new DrawingBrush();
squigglyBrush.Viewport = new Rect(0, 0, 6, 4.8);
squigglyBrush.ViewportUnits = BrushMappingMode.Absolute;
squigglyBrush.TileMode = TileMode.Tile;
squigglyBrush.Drawing = new GeometryDrawing(null, pathPen, pathGeometry);
foreach (KeyValuePair<int, object> keyValuePair in wordList)
{
Rect startPosition = textBox.GetRectFromCharacterIndex(keyValuePair.Key);
Rect endPosition = textBox.GetRectFromCharacterIndex(keyValuePair.Key + keyValuePair.Value.ToString().Length - 1, true);
Rect rectUnion = Rect.Union(startPosition, endPosition);
drawingContext.DrawLine(new Pen(squigglyBrush,4), rectUnion.BottomLeft, rectUnion.BottomRight);
//drawingContext.DrawLine(new Pen(Brushes.Red, 1), rectUnion.BottomLeft, rectUnion.BottomRight);
}
}