1
我的代碼現在只能從文本框中獲取文本。我想從窗口中獲取文本,例如從網頁或文本文件到文本框。我該怎麼做?如何在Window中的光標下找到該單詞?
private void textBox2_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;
//toolTip1.SetToolTip(targetTextBox, currentWord);
textBox3.Text = currentWord;
}