问题描述
我正在尝试使用 GetPositionFromPoint,并且在文档中它谈到了 snapToText bool,如果为 false,如果点不在字符的边界框内,则返回 null".无论如何,我总是收到一个空的 TextPointer,即使我的 Point 是在一个字符上.我将代码留在这里,我使用复选框(称为检查")来查看 TextPointer 是否为空,并且始终未选中:
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;
}
谢谢!
推荐答案
当您对特定控件内部的坐标感兴趣时,您必须计算相对于该控件的位置.如果计算相对于其他控件位置的位置将与目标控件不匹配.
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.
示例:您在一个大 Window
中有一个小 TextBox
,并且您对 TextBox
内的坐标感兴趣.你做了什么,你计算了相对于大Window
的所有位置.现在假设尺寸为 12 * 12 的 TextBox
位于 32 * 32 窗口的右下角 x=20;y=20 位置.
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
鼠标指针现在位于 TextBox
的中心,您可以计算:
The mouse pointer is now in the center of the TextBox
and you calculate:
p = Mouse.GetPosition(Application.Current.MainWindow);
p
是相对于 32 * 32 大窗口计算的,计算结果为 x=26;y=26,这是您当前和错误计算的结果.
当您使用此坐标在 TextBox
中查找文本时,您显然超出了边界(在此示例中),因为 TextBox
尺寸仅为 12 * 12.
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.
现在正确计算位置:
p = Mouse.GetPosition(TextBox);
p
现在相对于 TextBox
计算并计算为 x=6;y=6.该坐标现在位于 TextBox
的 12 * 12 边界内,您可以在此处点击渲染文本框.
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.
以下示例显示了 CheckBox
的正确实现,当鼠标指针悬停在 RichtextBox
的输入文本上时,它会被检查:
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;
}
这篇关于无法让 GetPositionFromPoint 正常工作(WPF、C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!