简体   繁体   English

如何获取键盘光标的位置作为文本框中的点

[英]How to get Keyboard cursor position as Point in textbox

i want to get Keyboard cursor position in textbox or RichtextBox. 我想在文本框或RichtextBox中获取键盘光标的位置。 On WinFrom i did this by using this code 在WinFrom上,我通过使用以下代码做到了这一点

Point p = rtb.GetPositionFromCharIndex(rtb.SelectionStart);

p.Y += (int)rtb.Font.GetHeight()*2;               
lstboxIntelli.Location = p;
lstboxbIntelli.Show();
ActiveControl = lstboxIntelli;

But in WPF i cant get GetPositionFromCharIndex property is there any other way to achieved this 但是在WPF中,我无法获取GetPositionFromCharIndex属性,是否有其他方法可以实现这一点

I want to position a Listbox right under the keyboard cursor (Like Intellisense) 我想在键盘光标的正下方放置一个列表框(如Intellisense)

Any help will be appreciate 任何帮助将不胜感激

In WPF TextBox you can get caret position in multiple ways: WPF TextBox您可以通过多种方式获得插入符的位置:

  1. By accessing TextBox.SelectionStart and TextBox.SelectionLength properties. 通过访问TextBox.SelectionStartTextBox.SelectionLength属性。
  2. TextBox.CaretIndex property. TextBox.CaretIndex属性。

Unfortunately there is no GetPositionFromCharIndex in TextBox so you'll have to do a little trick using GetRectFromCharacterIndex to get starting point for intellisense: 不幸的是, TextBox没有GetPositionFromCharIndex ,因此您必须使用GetRectFromCharacterIndex来做一些技巧,以获取智能感知的起点:

var rect = rtb.GetRectFromCharacterIndex(rtb.CaretIndex);
var point = rect.BottomRight;

Note: We are using BottomRight to make put intellisense in proper place. 注意:我们正在使用BottomRight将智能感知放置在适当的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM