Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:19:19+00:00 2026-05-15T06:19:19+00:00

I would like to get the word that a user has clicked on in

  • 0

I would like to get the word that a user has clicked on in a FlowDocument.

I am currently adding an event handler to every Run in the document and iterating through the TextPointers in the Run that was clicked, calling GetCharacterRect() on each one and checking if the rectangle contains the point.

However, when the click occurs near the end of a long Run this takes > 10 seconds.

Is there any more efficient method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:19:20+00:00Added an answer on May 15, 2026 at 6:19 am

    I’d say the easiest way is to use the Automation interfaces:

    using System.Windows.Automation.Peers;
    using System.Windows.Automation.Provider;
    
    FlowDocument flowDocument = ...;
    Point point = ...;
    
    var peer = new DocumentAutomationPeer(flowDocument);
    var textProvider = (ITextProvider)peer.GetPattern(PatternInterface.Text);
    var rangeProvider = textProvider.RangeFromPoint(point);
    

    The ITextProvider usage requires a reference to the UIAutomationProvider assembly. This assembly is not commonly referenced, so you may need to add it. UIAutomationTypes will also be needed to use some of its methods.

    Note that there are many options for creating your automation peer depending on how you are presenting the FlowDocument:

    var peer = new DocumentAutomationPeer(flowDocument);
    var peer = new DocumentAutomationPeer(textBlock);
    var peer = new DocumentAutomationPeer(flowDocumentScrollViewer);
    var peer = new TextBoxAutomationPeer(textBox);
    var peer = new RichTextBoxAutomationPeer(richTextBox);
    

    Update

    I tried this and it works well, though converting from an ITextRangeProvider to a TextPointer proved more difficult than I expected.

    I packaged the algorithm in an extension method ScreenPointToTextPointer for easy use. Here is an example of how my extension method can be used to bold all text before the mouse pointer and un-bold all text after it:

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
      var document = this.Viewer.Document;
      var screenPoint = PointToScreen(e.GetPosition(this));
    
      TextPointer pointer = document.ScreenPointToTextPointer(screenPoint);
    
      new TextRange(document.ContentStart, pointer).ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
      new TextRange(pointer, document.ContentEnd).ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
    }
    

    Here is the code for the extension method:

    using System.Windows.Automation.Peers;
    using System.Windows.Automation.Provider;
    using System.Windows.Automation.Text;
    
    public static class DocumentExtensions
    {
      // Point is specified relative to the given visual
      public static TextPointer ScreenPointToTextPointer(this FlowDocument document, Point screenPoint)
      {
        // Get text before point using automation
        var peer = new DocumentAutomationPeer(document);
        var textProvider = (ITextProvider)peer.GetPattern(PatternInterface.Text);
        var rangeProvider = textProvider.RangeFromPoint(screenPoint);
        rangeProvider.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Document, 1);
        int charsBeforePoint = rangeProvider.GetText(int.MaxValue).Length;
    
        // Find the pointer that corresponds to the TextPointer
        var pointer = document.ContentStart.GetPositionAtOffset(charsBeforePoint);
    
        // Adjust for difference between "text offset" and actual number of characters before pointer
        for(int i=0; i<10; i++)  // Limit to 10 adjustments
        {
          int error = charsBeforePoint - new TextRange(document.ContentStart, pointer).Text.Length;
          if(error==0) break;
          pointer = pointer.GetPositionAtOffset(error);
        }
        return pointer;
      }
    
    }
    

    Also note the use of PointToScreen in the example MouseMove method to get a screen point to pass into the extension method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get some information on list of points that needs to
I would like to get the dimensions (coordinates) for all the HTML elements of
I would like to get the most out of working in cch or tcsh
I would like to get a reference to all objects in the Java heap,
I would like to get contents of some stream or project I can find
I would like to get the AssemblyCompany attribute from a WinForm project inside of
I would like to get the final SQL query, with parameter values bound (replaced)
I would like to get the XAML source of a WPF Window (MainWindow). Clicking
I would like to get the first item from a list matching a condition.
I would like to get some feedback on what is one of my first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.