This is a migrated thread and some comments may be shown as answers.

Convert DocumentPosition to sreen coordinates

5 Answers 66 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 11 Feb 2016, 02:17 PM

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.

5 Answers, 1 is accepted

Sort by
0
Aylin
Telerik team
answered on 16 Feb 2016, 12:47 PM
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
0
Alexander
Top achievements
Rank 1
answered on 17 Feb 2016, 09:00 AM

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?

0
Accepted
Aylin
Telerik team
answered on 19 Feb 2016, 11:38 AM
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 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
0
Alexander
Top achievements
Rank 1
answered on 19 Feb 2016, 01:44 PM

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;
}

0
Aylin
Telerik team
answered on 24 Feb 2016, 09:31 AM
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
Tags
RichTextBox
Asked by
Alexander
Top achievements
Rank 1
Answers by
Aylin
Telerik team
Alexander
Top achievements
Rank 1
Share this question
or