You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF箭头图形添加文本失败求助:仅显示箭头,注释箭头后文本可见

Hey Benny, let's break down why your text isn't showing up with the arrow, and fix it step by step!

What's Going Wrong?

The main issue here is likely incorrect text positioning combined with how WPF handles FormattedText coordinates. Let me explain:

  • WPF's FormattedText.BuildGeometry() uses the bottom-left corner of the text as its starting point, not the top-left. Right now you're placing the text at (20, 5), which means the bottom of your 12pt text sits at Y=5—most of the text would be above that, outside the visible area of your arrow's bounds.
  • While your drawing order (arrow first, text second) is correct (later items in a DrawingGroup draw on top), the text's off-screen position makes it invisible even when the arrow is there.

Fixes to Try

1. Correct the Text's Starting Position

Calculate the Y-coordinate so the text sits vertically centered inside the arrow's shaft:

// Get the text's height and center it relative to the arrow's vertical center
double textY = centerY - formattedText.Height / 2;
Geometry textGeometry = formattedText.BuildGeometry(new System.Windows.Point(20, textY));

This will place the text's bottom-left corner at a position that keeps the entire text block within the arrow's shaft (between Y=50 and Y=150 in your current setup).

2. Optional: Adjust DrawingBrush Behavior

If you're using this DrawingBrush as a control background, the default Stretch=Fill might distort your arrow and text. Set it to maintain proportions:

var brush = new DrawingBrush(drawingGroup)
{
    Stretch = Stretch.Uniform,
    AlignmentX = AlignmentX.Left,
    AlignmentY = AlignmentY.Center
};

Full Corrected Code Snippet

int Width = 200; int Length = 1000; 
double arrowHeadWidth = Width; 
double arrowHeadLength = Width / 2; 
double arrowWidth = Width - arrowHeadWidth / 2; 
double arrowLength = Length; 
double centerY = Width / 2.0; 

var figure = new PathFigure { IsClosed = true, StartPoint = new Point(0, centerY - arrowWidth / 2.0) }; 
figure.Segments.Add(new LineSegment(new Point(arrowLength - arrowHeadLength, centerY - arrowWidth / 2.0), true)); 
figure.Segments.Add(new LineSegment(new Point(arrowLength - arrowHeadLength, centerY - arrowHeadWidth / 2.0), true)); 
figure.Segments.Add(new LineSegment(new Point(arrowLength, centerY), true)); 
figure.Segments.Add(new LineSegment(new Point(arrowLength - arrowHeadLength, centerY + arrowHeadWidth / 2.0), true)); 
figure.Segments.Add(new LineSegment(new Point(arrowLength - arrowHeadLength, centerY + arrowWidth / 2.0), true)); 
figure.Segments.Add(new LineSegment(new Point(0, centerY + arrowWidth / 2.0), true)); 

var geometry = new PathGeometry(); 
geometry.Figures.Add(figure); 

var borderPen = new Pen(Brushes.White, 10) { LineJoin = PenLineJoin.Round }; 
var arrowDrawing = new GeometryDrawing(Brushes.Transparent, borderPen, geometry); 

FormattedText formattedText = new FormattedText( 
    "Test", 
    System.Globalization.CultureInfo.CurrentUICulture, 
    FlowDirection.LeftToRight, 
    new Typeface("Tahoma"), 
    12, 
    Brushes.Black); 
formattedText.MaxTextWidth = arrowLength - 40; 
formattedText.MaxTextHeight = arrowWidth - 10; 

// Fix text position to center vertically in the arrow shaft
double textY = centerY - formattedText.Height / 2;
Geometry textGeometry = formattedText.BuildGeometry(new System.Windows.Point(20, textY)); 

GeometryDrawing textDrawing = new GeometryDrawing(Brushes.Red, new Pen(Brushes.Red, 1), textGeometry); 

var drawingGroup = new DrawingGroup(); 
drawingGroup.Children.Add(arrowDrawing); 
drawingGroup.Children.Add(textDrawing); 

// Optional: Preserve aspect ratio when using the brush
var brush = new DrawingBrush(drawingGroup)
{
    Stretch = Stretch.Uniform,
    AlignmentX = AlignmentX.Left,
    AlignmentY = AlignmentY.Center
};

Give this a try—your text should now show up neatly inside the arrow's shaft!

内容的提问来源于stack exchange,提问作者Benny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:42:59