Hallo,
ja, laut Api-Doku kann die Funktion eine nullterminierte Zeichenfolge zurückliefern, sodass man den String beim Auftreten eines \0-Zeichens an dieser Stelle abschneiden sollte. Zum Konvertieren würde ich schon StrConv() verwenden, wobei man allerdings auch gleich die Unicodevariante von SendMessage (also SendMessageW) verwenden könnte, womit die Konvertierung ANSI->Unicode entfällt:
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" ( _
lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" ( _
ByVal xPoint As Long, _
ByVal yPoint As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageW" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Sub Timer1_Timer()
Static lasthwnd As Long
Dim oPoint As POINTAPI
Dim hWnd As Long, i As Long
Dim lLength As Long
'Dim buffer() As Byte
' Mausposition ermitteln
If GetCursorPos(oPoint) <> 0 Then
' Fensterhandle des Controls, auf dem sich
' der Mauszeiger befindet
hWnd = WindowFromPoint(oPoint.x, oPoint.y)
'Fenster gewechselt (seit letztem Timer-Event)
If lasthwnd <> hWnd Then
'Textlänge ermitteln
lLength = SendMessage(hWnd, WM_GETTEXTLENGTH, 0&, 0&)
Me.Caption = ""
'Text vorhanden?
If lLength > 0 Then
'Buffer einrichten
Dim buffer As String
buffer = String((lLength + 1), ChrW(0))
'Text abfragen
SendMessage hWnd, WM_GETTEXT, lLength + 1, ByVal StrPtr(buffer)
Dim strg As String
strg = buffer
' Am \0-Zeichen abschneiden
If InStr(1, strg, ChrW(0)) > 0 Then strg = Mid(strg, 1, InStr(1, _
strg, ChrW(0)) - 1)
'Infozeile im Fensterkopf ausgeben
Me.Caption = _
"Handle: " & CStr(hWnd) & _
" x= " & CStr(oPoint.x) & " y= " & CStr(oPoint.y) & _
" Länge = " & CStr(lLength)
'Text in Control übertragen
RichTextBox1.Text = strg
RichTextBox1.SelStart = Len(RichTextBox1.Text)
'Fensterhandle notieren
lasthwnd = hWnd
End If 'lLength > 0
End If 'LastHwnd <> hWnd
End If 'GetCursorPos
End Sub
Beitrag wurde zuletzt am 02.02.12 um 20:21:46 editiert. |