2013-05-02 143 views
0

我可以在任何應用程序中找到插入位置,但我需要知道當前插入位置處有哪些文本(單詞)。在插入位置查找文本

如何獲取文本?

+0

一些代碼將是有益的 – 2013-05-02 18:38:54

+0

我會感到驚訝,如果你能找到任何* *應用程序(至少不會比也許其他任何方式插入位置屏幕截圖,然後OCRing)...大多數應用程序可能不會有任何接口來請求,並有很好的理由。您是否在談論在控制代碼的自己的應用程序中獲取控件的插入位置? – neminem 2013-05-02 18:43:39

回答

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 
2

如果您使用的WinForms應用程序,並通過插入位置你的意思是插入符位置在一個文本框。那麼你可以做一些這樣的事情。

  • 1.將事件處理程序KEYUP和MouseUp事件
  • 2.獲取當前文本框和文本插入位置
  • 3.通過這個到那個位置
  • 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 - get whole word at current caret position
  • 對於Windows河粉ne 7插入位置由textBox1.SelectionStart表示。看到這個線程,如果你的應用程序是一個Windows Phone應用程序:Selecting the tapped-on word on a single click in textbox
  • 相關問題