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

发布于 2024-09-17 06:08:52 字数 716 浏览 6 评论 0原文

我有一个带有 FlowDocument 的 WPF 窗口,其中包含多个超链接:

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

在 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();
    }
}

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

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

<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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓬勃野心 2024-09-24 06:08:52

您可以使用 ContentStart 或 < a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.textelement.contentend.aspx" rel="nofollow noreferrer">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;
}

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文