Convert DocumentPosition to sreen coordinates

6 posts, 1 answers
  1. Alexander
    Alexander avatar
    3 posts
    Member since:
    Jun 2015

    Posted 11 Feb Link to this post

    Hi!

     

    When writing automated tests required a mouse click on the found text in RadRichTextBox. How I can convert DocumentPosition to screen coordinates. 

     

    Thanks in advance.

  2. Aylin
    Admin
    Aylin avatar
    75 posts

    Posted 16 Feb Link to this post

    Hi Alexander,

    If you need the screen coordinates in the Document, you could use GetViewPointFromDocumentPosition() method which returns Point.

    Point clickPoint = this.radRichTextBox.ActiveEditorPresenter.GetViewPointFromDocumentPosition(this.radRichTextBox.Document.CaretPosition);

    Then, you could access X and Y coordinate values of that point (clickPoint.X and clickPoint.Y)

    However, If you need the screen coordinates in the RadRichTextBox control, you could attach to the MouseMove event of RadRichTextBox as follows:

    this.radRichTextBox.MouseMove += RadRichTextBox_MouseMove;
     
    void RadRichTextBox_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
         System.Windows.Point position = e.GetPosition((RadRichTextBox)sender);
         int pX = (int)position.X;
         int pY = (int)position.Y;
    }

    You might find useful the following article about Positioning as well.

    I hope this helps.

    Regards,
    Aylin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alexander
    Alexander avatar
    3 posts
    Member since:
    Jun 2015

    Posted 17 Feb in reply to Aylin Link to this post

    Hi Aylin,

    I tried to use this method. But each time, returned coordinates were incorrect.

    My code for finding coordinates and move mouse on it:

    private void SearchRadButtonClickEventHandler(object sender, RoutedEventArgs e)
    {
        System.Drawing.Point coordinates = GetScreenCoordinates(SearchTextBox.Text);
     
        if (coordinates != System.Drawing.Point.Empty)
        {
            System.Diagnostics.Debug.WriteLine(">>> X:{0} Y:{1}", coordinates.X, coordinates.Y);
            System.Windows.Forms.Cursor.Position = coordinates;
        }
    }
     
    private System.Drawing.Point GetScreenCoordinates(string searchString)
    {
        System.Drawing.Point coordinates = System.Drawing.Point.Empty;
         
        DocumentTextSearch search = new DocumentTextSearch(TestRadRichTextBox.Document);
        TextRange foundRange = search.FindAll(searchString).FirstOrDefault();
        if (foundRange != null)
        {
            Point pt = TestRadRichTextBox.ActiveEditorPresenter.GetViewPointFromDocumentPosition(foundRange.StartPosition);
            coordinates = new System.Drawing.Point((int)pt.X, (int)pt.Y);
        }
     
        return coordinates;
    }

    Can you supporting where I did mistake?

  5. Answer
    Aylin
    Admin
    Aylin avatar
    75 posts

    Posted 19 Feb Link to this post

    Hi Alexander,

    Thank you for the provided code snippet. I used it in a sample application and realized what you mean by "incorrect coordinates".

    Actually, GetViewPointFromDocumentPosition() method returns the coordinates within RadRichTextBox control, i.e. these coordinates are relative to the upper-left corner of  RadRichTexBox control. See ReturnedCoordinates.png image from the attachment - the returned coordinates are within the red border.

    Whereas Position property sets the cursor's position starting from the top-left corner of the screen. That said, if you have anything else around the control, e.g window borders, RibbonBar, etc., you should have this in mind and include this offset in your calculations before setting the value to Position property.

    Here is SearchRadButtonClickEventHandler() method modified according to the current positions of my screen:

    private void SearchRadButtonClickEventHandler(object sender, System.Windows.RoutedEventArgs e)
    {
        System.Drawing.Point coordinates = GetScreenCoordinates(this.SearchTextBox.Text);
     
        if (coordinates != System.Drawing.Point.Empty)
        {
            System.Diagnostics.Debug.WriteLine(">>> X:{0} Y:{1}", coordinates.X, coordinates.Y);
            coordinates.Y = coordinates.Y + 50; // 50 pixels added for the window border and the StackPanel holding the TextBoxes
            System.Windows.Forms.Cursor.Position = coordinates;
            this.Xcoordinate.Text = coordinates.X.ToString();
            this.Ycoordinate.Text = coordinates.Y.ToString();
        }
    }


    Do not hesitate to get back to us if you have additional questions.

    Regards,
    Aylin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Alexander
    Alexander avatar
    3 posts
    Member since:
    Jun 2015

    Posted 19 Feb in reply to Aylin Link to this post

    Thanck you very much!

    I found universal solution. This could help someone in the future.

     

    private System.Drawing.Point GetScreenCoordinates(string searchString)
    {
        System.Drawing.Point coordinates = System.Drawing.Point.Empty;
         
        DocumentTextSearch search = new DocumentTextSearch(TestRadRichTextBox.Document);
        TextRange foundRange = search.FindAll(searchString).FirstOrDefault();
        if (foundRange != null)
        {
            System.Windows.Point foundPoint = TestRadRichTextBox.ActiveEditorPresenter.GetViewPointFromDocumentPosition(foundRange.StartPosition);
            System.Windows.Point locationContolFromScreen = TestRadRichTextBox.PointToScreen(new System.Windows.Point(0, 0));
     
            coordinates = new System.Drawing.Point((int) (foundPoint.X + locationContolFromScreen.X), (int) (foundPoint.Y + locationContolFromScreen.Y));
        }
     
        return coordinates;
    }

  7. Aylin
    Admin
    Aylin avatar
    75 posts

    Posted 24 Feb Link to this post

    Hi Alexander,

    I am glad to hear that you found a solution and thank you for sharing it with us. 

    Do not hesitate to contact us if any other questions or issues arise.

    Regards,
    Aylin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready