5 Answers, 1 is accepted
0
Hi Alexander,
If you need the screen coordinates in the Document, you could use GetViewPointFromDocumentPosition() method which returns Point.
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:
You might find useful the following article about Positioning as well.
I hope this helps.
Regards,
Aylin
Telerik
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
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:
Do not hesitate to get back to us if you have additional questions.
Regards,
Aylin
Telerik
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
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
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