0

我可以在任何应用程序中找到插入符号的位置,但我需要知道当前插入符号位置的文本(单词)。

我怎样才能得到文本?

4

2 回答 2

2

很难理解您的问题,这似乎主要是作为陈述来表达的。

假设我理解你的问题,试试这样的方法......

Private Sub CheckPosition()
Dim char_pos As Long
Dim row As Long
Dim col As Long

char_pos = SendMessage(Text1.hwnd, EM_GETSEL, 0, 0)
char_pos = char_pos \ &H10000

row = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, _
char_pos, 0) + 1
col = char_pos - SendMessage(Text1.hwnd, EM_LINEINDEX, _
-1, 0) + 1

lblPosition.Caption = "(" & Format$(row) & ", " & _
Format$(col) & ")"
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As _
Integer)
CheckPosition
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As _
Integer)
CheckPosition
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
CheckPosition
End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
CheckPosition
End Sub 
于 2013-05-02T18:46:51.173 回答
2

如果您使用的是 WinForms 应用程序,并且插入符号位置是指文本框中的插入符号位置。然后你可以做一些这样的事情。

  • 1. 将事件处理程序附加到 KeyUp 和 MouseUp 事件
  • 2.获取当前文本框Text和插入符号位置
  • 3. 将 this 传递给返回该位置下单词的函数
  •     private void textBox1_KeyUp(object sender, EventArgs e)
        {
            GetWordFromCaretPosition(textBox1.Text, textBox1.SelectionStart);
        }
    
        private void textBox1_MouseUp(object sender, EventArgs e)
        {
            GetWordFromCaretPosition(textBox1.Text, textBox1.SelectionStart);
        }
    
        private string GetWordFromCaretPosition(string input, int position)
        {
            string word = string.Empty;
            //Yet to be implemented.
            return word;
        }
    

  • 对于 WPF 文本框插入符号位置表示为textBox1.CaretIndex
  • 对于 WPF RichTextBox,请参阅此线程:WPF RichTextBox - 在当前插入符号位置获取整个单词
  • 对于 Windows Phone 7,插入符号位置由 表示textBox1.SelectionStart。如果您的应用程序是 Windows Phone 应用程序,请查看此线程:在文本框中单击一次选择点击的单词
  • 于 2013-05-02T19:06:02.890 回答