如何获取 TextBlock (WPF C#) 的右锯齿内联元素的字符边界?
How to obtain the character bounds of a right-jagged inline element of a TextBlock (WPF C#)?
我没能找到这个问题的答案。我正在使用在滚动查看器中显示的 TextBlock。 TextWrapping 设置为换行:
<ScrollViewer VerticalScrollBarVisibility="Auto"
Width="{Binding Parent.ActualWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Height="{Binding Parent.ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Name="theScrollViewer">
<Grid Background="White" >
<wc:CustomTextBlock
InLineText="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
TextWrapping="Wrap"
WordPad="{Binding WordPad, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
/>
TextBlock 由内联元素填充,每个元素包含一个逻辑词,定义如下:
Regex.Split(text, @"(\s+)")
现在,在内联元素的 MouseEnter 事件中,我尝试创建一个
边界矩形:
static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Run Sender = sender as Run;
TextPointer tp0 = Sender.ContentStart;
TextPointer tp1 = Sender.ContentEnd;
Rect StartRect = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect EndRect = tp1.GetCharacterRect(LogicalDirection.Backward);
StartRect.Union(EndRect);
.........................
}
当内联元素换行到 TextBlock 的下一行时,这显然失败了。
行内元素"wrapped"到下一行时如何找到显示位置。我正在寻找一种生成两个矩形的方法:
围绕行尾内联的第一部分的矩形,
下一行开始处内联结束部分周围的矩形。
非常感谢任何帮助,因为我是 flowdocuments 的新手。谢谢
作为这方面的新手,能有专家评论肯定会很好。与此同时,我发现这在我的情况下可以正常工作:
static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Run Sender = sender as Run;
TextPointer tp0 = Sender.ElementStart;
TextPointer tp1 = Sender.ElementEnd;
Rect r0 = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect r1 = tp1.GetCharacterRect(LogicalDirection.Backward);
if (r1.Top != r0.Top)
{
/* this inline element spans two physical rows */
Rect StartRect0 = r0;
Rect EndRect0 = tp1.GetLineStartPosition(0).GetCharacterRect(LogicalDirection.Backward);
StartRect0.Union(EndRect0);
Rect StartRect1 = new Rect(0, r1.Top, 0, r1.Height);
Rect EndRect1 = r1;
StartRect1.Union(EndRect1);
}
else
{
Rect StartRect0 = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect EndRect0 = tp1.GetCharacterRect(LogicalDirection.Backward);
StartRect0.Union(EndRect0);
}
}
希望这对某人有所帮助。
我没能找到这个问题的答案。我正在使用在滚动查看器中显示的 TextBlock。 TextWrapping 设置为换行:
<ScrollViewer VerticalScrollBarVisibility="Auto"
Width="{Binding Parent.ActualWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Height="{Binding Parent.ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Name="theScrollViewer">
<Grid Background="White" >
<wc:CustomTextBlock
InLineText="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
TextWrapping="Wrap"
WordPad="{Binding WordPad, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
/>
TextBlock 由内联元素填充,每个元素包含一个逻辑词,定义如下:
Regex.Split(text, @"(\s+)")
现在,在内联元素的 MouseEnter 事件中,我尝试创建一个 边界矩形:
static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Run Sender = sender as Run;
TextPointer tp0 = Sender.ContentStart;
TextPointer tp1 = Sender.ContentEnd;
Rect StartRect = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect EndRect = tp1.GetCharacterRect(LogicalDirection.Backward);
StartRect.Union(EndRect);
.........................
}
当内联元素换行到 TextBlock 的下一行时,这显然失败了。
行内元素"wrapped"到下一行时如何找到显示位置。我正在寻找一种生成两个矩形的方法:
围绕行尾内联的第一部分的矩形,
下一行开始处内联结束部分周围的矩形。
非常感谢任何帮助,因为我是 flowdocuments 的新手。谢谢
作为这方面的新手,能有专家评论肯定会很好。与此同时,我发现这在我的情况下可以正常工作:
static void inline_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Run Sender = sender as Run;
TextPointer tp0 = Sender.ElementStart;
TextPointer tp1 = Sender.ElementEnd;
Rect r0 = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect r1 = tp1.GetCharacterRect(LogicalDirection.Backward);
if (r1.Top != r0.Top)
{
/* this inline element spans two physical rows */
Rect StartRect0 = r0;
Rect EndRect0 = tp1.GetLineStartPosition(0).GetCharacterRect(LogicalDirection.Backward);
StartRect0.Union(EndRect0);
Rect StartRect1 = new Rect(0, r1.Top, 0, r1.Height);
Rect EndRect1 = r1;
StartRect1.Union(EndRect1);
}
else
{
Rect StartRect0 = tp0.GetCharacterRect(LogicalDirection.Forward);
Rect EndRect0 = tp1.GetCharacterRect(LogicalDirection.Backward);
StartRect0.Union(EndRect0);
}
}
希望这对某人有所帮助。