如何根据鼠标位置从文本框中获取特定文本值

我有一个多行文本框,根据给出的数据显示一些值(通常每行一个值)。

(为了让工具提示弹出一些’替代’数据)我想得到鼠标hover在上面的字(或至少是这一行),这样我就可以找到显示的替代方法。

我有一些想法如何通过基于文本框和字体大小的计算来做到这一点,但我不知道要走这条路,因为大小和字体可能经常变化。

那么……有没有办法使用鼠标位置抓取特定的文本框文本?

这是另一种解决方案。 将此MouseMove事件添加到TextBox:

private void txtHoverWord_MouseMove(object sender, MouseEventArgs e) { if (!(sender is TextBox)) return; var targetTextBox = sender as TextBox; if(targetTextBox.TextLength < 1) return; var currentTextIndex = targetTextBox.GetCharIndexFromPosition(e.Location); var wordRegex = new Regex(@"(\w+)"); var words = wordRegex.Matches(targetTextBox.Text); if(words.Count < 1) return; var currentWord = string.Empty; for (var i = words.Count - 1; i >= 0; i--) { if (words[i].Index <= currentTextIndex) { currentWord = words[i].Value; break; } } if(currentWord == string.Empty) return; toolTip.SetToolTip(targetTextBox, currentWord); } 

使用GetCharIndexFromPosition方法将鼠标的位置映射到整个Text中的索引。 从那个位置开始,左右进步直到你拥有整个单词。

要获得鼠标位置,请使用MouseHover事件,以便在它仍然存在时获取它,而不是每次都这样(这会使事情变慢)。

我的解决方案使用技巧来实现你想要的。

在文本区域内双击时,它会选择基础单词。

因此,在表单上使用RichTextBoxTextBox在鼠标事件上执行闪存)时,可以在单击鼠标中键时模拟双击(类似于Babylon字典)。 如果您愿意,也可以使用MouseHover而不是MouseDown 。 有用。

 public partial class Form3 : Form { System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); public Form3() { InitializeComponent(); timer.Interval = 50; timer.Tick += timer_Tick; } void timer_Tick(object sender, EventArgs e) { timer.Stop(); MessageBox.Show(richTextBox1.SelectedText); // do more stuff here, eg display your tooltip for the selected word or anything else richTextBox1.SelectionLength = 0; // remove the highlighted color of selection } [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); private const uint MOUSEEVENTF_LEFTDOWN = 0x02; private const uint MOUSEEVENTF_LEFTUP = 0x04; private const uint MOUSEEVENTF_RIGHTDOWN = 0x08; private const uint MOUSEEVENTF_RIGHTUP = 0x10; public void DoMouseDoubleClick() { //Call the imported function with the cursor's current position uint X = (uint)Cursor.Position.X; uint Y = (uint)Cursor.Position.Y; mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0); mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0); timer.Start(); // some delay is required so that mouse event reach to RichTextBox and the word get selected } private void richTextBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { DoMouseDoubleClick(); } } }