' Returnerar ordet som muspekaren står på i RichTextboxen
Public Function RichWordOver(rch As RichTextBox, X As Single, Y As Single) As String
Dim pt As POINTAPI
Dim pos As Integer
Dim start_pos As Integer
Dim end_pos As Integer
Dim ch As String
Dim txt As String
Dim txtlen As Integer
' Konverterar position till pixels.
pt.X = X \ Screen.TwipsPerPixelX
pt.Y = Y \ Screen.TwipsPerPixelY
' Hämta muspositionen
pos = SendMessage(RichTextBox1.hWnd, EM_CHARFROMPOS, 0&, pt)
If pos <= 0 Then Exit Function
' Finn början på ordet
txt = rch.Text
For start_pos = pos To 1 Step -1
ch = Mid$(rch.Text, start_pos, 1)
' Tillåt siffror, bokstäver och underscore
If Not ( _
(ch >= "0" And ch <= "9") Or _
(ch >= "a" And ch <= "z") Or _
(ch >= "A" And ch <= "Z") Or _
ch = "_" _
) Then Exit For
Next start_pos
start_pos = start_pos + 1
' Finn slutet av ordet
txtlen = Len(txt)
For end_pos = pos To txtlen
ch = Mid$(txt, end_pos, 1)
' Tillåt siffror, bokstäver och underscore
If Not ( _
(ch >= "0" And ch <= "9") Or _
(ch >= "a" And ch <= "z") Or _
(ch >= "A" And ch <= "Z") Or _
ch = "_" _
) Then Exit For
Next end_pos
end_pos = end_pos - 1
If start_pos <= end_pos Then _
RichWordOver = Mid$(txt, start_pos, end_pos - start_pos + 1)
End Function
Private Sub Form_Load()
RichTextBox1.Text = "Detta exempel presenterar det ord som du står över" & _
vbCrLf & vbCrLf & "med muspekaren. Används för många syften. Se: " & _
vbCrLf & vbCrLf & "http://www.greenlight.se/pellesoft för mer tips."
End Sub
Private Sub richtextbox1_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Dim txt As String
txt = RichWordOver(RichTextBox1, X, Y)
' presentera ordet som du står över
If Label1.Caption <> txt