如何确定WPF中超链接的坐标

时间:2023-01-12 12:06:01

I have WPF window with a FlowDocument with several hyperlinks in it:

我有一个带有FlowDocument的WPF窗口,里面有几个超链接:

<FlowDocumentScrollViewer>
  <FlowDocument TextAlignment="Left" >
     <Paragraph>Some text here
       <Hyperlink Click="Hyperlink_Click">open form</Hyperlink>
     </Paragraph>           
  </FlowDocument>
</FlowDocumentScrollViewer>

In the C# code I handle Click event to create and show a new WPF Window:

在C#代码中我处理Click事件以创建并显示一个新的WPF窗口:

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    if (sender is Hyperlink)
    {
        var wnd = new SomeWindow();
        //wnd.Left = ???
        //wnd.Top = ???
        wnd.Show();
    }
}

I need this window to appear next to hyperlink's actual position. So I assume it requires assigning values to the window's Left and Top properties. But I have no idea how to obtain hyperlink position.

我需要这个窗口出现在超链接的实际位置旁边。所以我假设它需要为窗口的Left和Top属性赋值。但我不知道如何获得超链接位置。

1 个解决方案

#1


4  

You can use ContentStart or ContentEnd to get a TextPointer for the start or end of the hyperlink and then call GetCharacterRect to get the bounding box relative to the FlowDocumentScrollViewer. If you get a reference to the FlowDocumentScrollViewer, you can use PointToScreen to convert it to screen coordinates.

您可以使用ContentStart或ContentEnd为超链接的开头或结尾获取TextPointer,然后调用GetCharacterRect以获取相对于FlowDocumentScrollViewer的边界框。如果您获得对FlowDocumentScrollViewer的引用,则可以使用PointToScreen将其转换为屏幕坐标。

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    var hyperlink = sender as Hyperlink;
    if (hyperlink != null)
    {
        var rect = hyperlink.ContentStart.GetCharacterRect(
            LogicalDirection.Forward);
        var viewer = FindAncestor(hyperlink);
        if (viewer != null)
        {
            var screenLocation = viewer.PointToScreen(rect.Location);

            var wnd = new Window();
            wnd.WindowStartupLocation = WindowStartupLocation.Manual;
            wnd.Top = screenLocation.Y;
            wnd.Left = screenLocation.X;
            wnd.Show();
        }
    }
}

private static FrameworkElement FindAncestor(object element)
{
    while(element is FrameworkContentElement)
    {
        element = ((FrameworkContentElement)element).Parent;
    }
    return element as FrameworkElement;
}

#1


4  

You can use ContentStart or ContentEnd to get a TextPointer for the start or end of the hyperlink and then call GetCharacterRect to get the bounding box relative to the FlowDocumentScrollViewer. If you get a reference to the FlowDocumentScrollViewer, you can use PointToScreen to convert it to screen coordinates.

您可以使用ContentStart或ContentEnd为超链接的开头或结尾获取TextPointer,然后调用GetCharacterRect以获取相对于FlowDocumentScrollViewer的边界框。如果您获得对FlowDocumentScrollViewer的引用,则可以使用PointToScreen将其转换为屏幕坐标。

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    var hyperlink = sender as Hyperlink;
    if (hyperlink != null)
    {
        var rect = hyperlink.ContentStart.GetCharacterRect(
            LogicalDirection.Forward);
        var viewer = FindAncestor(hyperlink);
        if (viewer != null)
        {
            var screenLocation = viewer.PointToScreen(rect.Location);

            var wnd = new Window();
            wnd.WindowStartupLocation = WindowStartupLocation.Manual;
            wnd.Top = screenLocation.Y;
            wnd.Left = screenLocation.X;
            wnd.Show();
        }
    }
}

private static FrameworkElement FindAncestor(object element)
{
    while(element is FrameworkContentElement)
    {
        element = ((FrameworkContentElement)element).Parent;
    }
    return element as FrameworkElement;
}