Can't get GetPositionFromPoint to work well (WPF, C#)

我的未来我决定 提交于 2021-02-11 14:19:37

问题


I am trying to use the GetPositionFromPoint, and in the documentation it talks about the snapToText bool which, if false, "returns null if point is not inside a character's bounding box". Anyway, I am receiving always a null TextPointer, even when my Point is on a character. I leave my code here, I am using a checkbox (called "check") to see if the TextPointer is null or not, and it is always unchecked:

Point posicion = Mouse.GetPosition(Application.Current.MainWindow);
TextPointer posAhora = richTextBox1.GetPositionFromPoint(posicion, false);
if(posAhora != null)
{
   check.IsChecked = true;
}
else
{
   check.IsChecked = false;
}

Thank you!


回答1:


When you are interested in the coordinates inside a specific control, then you must calculate the position relative to this control. If calculate the position relative to some other control positions won't match with the target control.

Example: you have a small TextBox inside a big Window and you are interested in the coordinates inside the TextBox. What you did, you calculated all positions relative to the big Window. Now let's say the TextBox with dimensions of 12 * 12 is positioned in the bottom right corner of a 32 * 32 window at the position x=20;y=20.

 ________________
|                | <--- MainWindow 32 * 32
|                |
|           _____|
|          |     |
|          |  P  | <--- TextBox 12 * 12 (x=20;y=20)
|__________|_____|

p = pointer position

The mouse pointer is now in the center of the TextBox and you calculate:

p = Mouse.GetPosition(Application.Current.MainWindow);

p is calculated relative to the big 32 * 32 window and evaluates to x=26;y=26, which is the result of your current and wrong calculations.
When you use this coordinates to look for text in the TextBox you are obviously out of bounds (in this example) as the TextBox dimension is only 12 * 12.

Now the correct calculation of the position:

p = Mouse.GetPosition(TextBox);

p is now calculated relative to the TextBox and evaluates to x=6;y=6. This coordinates are now inside the 12 * 12 bounds of the TextBox, where you may hit the box of rendered text.


The following example shows the correct implementation of a CheckBox that gets checked when the mouse pointer is over the input text of a RichtextBox:

MainWindow.xaml

<Window>
  <StackPanel>
    <CheckBox x:Name="CheckBox"/>

    <!-- Enter some text and move the mouse over the text -->
    <RichTextBox x:Name="RichTextBox" />
  </StackPanel>
</Window>

MainWindow.xaml.cs

protected override void OnMouseMove(MouseEventArgs e)
{
  base.OnMouseMove(e);

  var relativeMousePosition = e.GetPosition(this.RichTextBox);
  this.CheckBox.IsChecked = this.RichTextBox.GetPositionFromPoint(relativeMousePosition , false) != null;
}


来源:https://stackoverflow.com/questions/61371952/cant-get-getpositionfrompoint-to-work-well-wpf-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!