Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const EM_CHARFROMPOS = &HD7Private Sub Command1_Click() Dim word As String word = getword(Text1.Text, ps) MsgBox word End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim pos As Long, lc As Long Dim line As Integer, charpos As Integer X = X / Screen.TwipsPerPixelX Y = Y / Screen.TwipsPerPixelY pos = X + Y * 65536 lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos) line = lc \ 65536 charpos = lc Mod 65536 Dim word As String word = getword(Text1, charpos) MsgBox word End SubPrivate Function getword(txt As TextBox, pos As Integer) As String Dim bArr() As Byte, bArr2() As Byte, pos1 As Integer, pos2 As Integer, i As Long bArr = StrConv(txt.Text, vbFromUnicode) pos1 = 0: pos2 = UBound(bArr) For i = pos - 1 To 0 Step -1 If IsDelimiter(bArr(i)) Then pos1 = i + 1 Exit For End If Next For i = pos To UBound(bArr) If IsDelimiter(bArr(i)) Then pos2 = i - 1 Exit For End If Next If pos2 > pos1 Then ReDim bArr2(pos2 - pos1) As Byte For i = pos1 To pos2 bArr2(i - pos1) = bArr(i) Next getword = StrConv(bArr2, vbUnicode) Else getword = "" End If End FunctionPrivate Function IsDelimiter(ByVal Char As Byte) As Boolean Dim s As String s = Chr(Char) IsDelimiter = False If s = " " Or s = "," Or s = "." Or s = "?" Or s = vbCr Or s = vbLf Then IsDelimiter = True End If
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_CHARFROMPOS = &HD7Private Sub Command1_Click()
Dim word As String
word = getword(Text1.Text, ps)
MsgBox word
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pos As Long, lc As Long
Dim line As Integer, charpos As Integer
X = X / Screen.TwipsPerPixelX
Y = Y / Screen.TwipsPerPixelY
pos = X + Y * 65536
lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)
line = lc \ 65536
charpos = lc Mod 65536
Dim word As String
word = getword(Text1, charpos)
MsgBox word
End SubPrivate Function getword(txt As TextBox, pos As Integer) As String
Dim bArr() As Byte, bArr2() As Byte, pos1 As Integer, pos2 As Integer, i As Long
bArr = StrConv(txt.Text, vbFromUnicode)
pos1 = 0: pos2 = UBound(bArr)
For i = pos - 1 To 0 Step -1
If IsDelimiter(bArr(i)) Then
pos1 = i + 1
Exit For
End If
Next
For i = pos To UBound(bArr)
If IsDelimiter(bArr(i)) Then
pos2 = i - 1
Exit For
End If
Next
If pos2 > pos1 Then
ReDim bArr2(pos2 - pos1) As Byte
For i = pos1 To pos2
bArr2(i - pos1) = bArr(i)
Next
getword = StrConv(bArr2, vbUnicode)
Else
getword = ""
End If
End FunctionPrivate Function IsDelimiter(ByVal Char As Byte) As Boolean
Dim s As String
s = Chr(Char)
IsDelimiter = False
If s = " " Or s = "," Or s = "." Or s = "?" Or s = vbCr Or s = vbLf Then
IsDelimiter = True
End If
End Function