|
|
Title | See what word is under the mouse in a RichTextBox and look it up in a list of known words |
Description | This example shows how to see what word is under the mouse in a RichTextBox and look it up in a list of known words in Visual Basic 6. |
Keywords | RichTextBox, TextBox, word, mouse, cursor |
Categories | Controls, Tips and Tricks, API |
|
|
Thanks to Gamal.
When the form loads, it puts some text in the RichTextBox. It then initializes the m_Words and m_Tips collections to hold known words and their tooltips.
|
|
Private m_Words As Collection
Private m_Tips As Collection
Private Sub Form_Load()
...
' Make some lookup definitions.
Set m_Words = New Collection
Set m_Tips = New Collection
m_Words.Add "http://www.vb-helper.com/vba.htm"
m_Tips.Add "Visual Basic Algorithms Web page"
m_Words.Add _
"(http://www.vb-helper.com/howto_richtextbox_lookup_word_under_mouse.html)"
m_Tips.Add "This example's Web page"
m_Words.Add "more."
m_Tips.Add "(Including some cool fractal graphics!)"
End Sub
|
|
When the mouse moves over the RichTextBox, its MouseMove event handler calls function RichWordOver to see what word is under the mouse. It looks for the word in m_Words and displays the corresponding tooltip.
|
|
Private Sub rchMainText_MouseMove(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
Dim txt As String
Dim tip_txt As String
Dim i As Integer
' Get the word under the mouse.
txt = RichWordOver(rchMainText, X, Y)
' Look up the word.
For i = 1 To m_Words.Count
If LCase$(txt) = LCase$(m_Words(i)) Then
tip_txt = m_Tips(i)
Exit For
End If
Next i
' Update the label and tooltip.
If lblCurrentWord.Caption <> tip_txt Then
lblCurrentWord.Caption = tip_txt
rchMainText.ToolTipText = tip_txt
End If
End Sub
|
|
Subroutine RichWordOver figures out where the mouse is and sends the RichTextBox the message EM_CHARFROMPOS to see what character is there. It then looks forward and backward from that point to determine where the word ends.
Helper function IsWordSeparator returns True if a character is not part of a word and thus marks the boundary between words.
|
|
' Return the word the mouse is over.
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
' Convert the position to pixels.
pt.X = X \ Screen.TwipsPerPixelX
pt.Y = Y \ Screen.TwipsPerPixelY
' Get the character number
pos = SendMessage(rch.hWnd, EM_CHARFROMPOS, 0&, pt)
If pos <= 0 Then Exit Function
' Find the start of the word.
txt = rch.Text
For start_pos = pos To 1 Step -1
' See if this is a word separator.
ch = Mid$(rch.Text, start_pos, 1)
If IsWordSeparator(ch) Then Exit For
Next start_pos
start_pos = start_pos + 1
' Find the end of the word.
txtlen = Len(txt)
For end_pos = pos To txtlen
' See if this is a word separator.
ch = Mid$(txt, end_pos, 1)
If IsWordSeparator(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
' Return True if this is a word-separating character.
' Count all non-visible characters as separators.
Private Function IsWordSeparator(ByVal ch As String) As _
Boolean
IsWordSeparator = (ch <= " " Or ch > "~")
End Function
|
|
|
|
|
|