main.frm
来自「很好的教程原代码!」· FRM 代码 · 共 120 行
FRM
120 行
VERSION 5.00
Begin VB.Form Main
Caption = "读取鼠标所在位置的单词"
ClientHeight = 4395
ClientLeft = 60
ClientTop = 345
ClientWidth = 5820
LinkTopic = "Form1"
ScaleHeight = 4395
ScaleWidth = 5820
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
BeginProperty Font
Name = "MS Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 3615
Left = 240
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 0
Text = "Main.frx":0000
Top = 240
Width = 5175
End
End
Attribute VB_Name = "Main"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private 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
' 单击鼠标时或的当前行和当前单词
pos = X / Screen.TwipsPerPixelX + Y / Screen.TwipsPerPixelY * 65536
lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)
Line = lc \ 65536
CharPos = lc Mod 65536
' 输出
MsgBox " 选择行 = " & GetLine(Text1, Line) & vbCrLf & "选择单词= " & GetWord(Text1, CharPos)
End Sub
' 获得指定位置的单词
Function GetWord(txt As TextBox, pos As Integer) As String
Dim bArr() As Byte, pos1 As Integer, pos2 As Integer, i As Integer
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)
SendMessage txt.hwnd, EM_SETSEL, pos1, ByVal CLng(pos2 + 1)
Else
GetWord = ""
End If
End Function
' 判断是否是单词分隔符
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
' 获取指定行的内容
Function GetLine(txt As TextBox, ByVal Line As Integer) As String
Dim S As String, Length As Integer, pos As Long
GetLine = ""
pos = SendMessage(txt.hwnd, EM_LINEINDEX, Line, ByVal 0&)
Length = SendMessage(txt.hwnd, EM_LINELENGTH, pos, ByVal 0&)
S = String(Length, Chr(0))
RtlMoveMemory ByVal S, Length, 2
If SendMessage(Text1.hwnd, EM_GETLINE, Line, ByVal S) > 0 Then
GetLine = S
End If
End Function
' 鼠标移动时获取单词
Private Sub Text1_MouseMove(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
pos = X / Screen.TwipsPerPixelX + Y / Screen.TwipsPerPixelY * 65536
lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)
Line = lc \ 65536
CharPos = lc Mod 65536
Text1.ToolTipText = GetWord(Text1, CharPos)
End Sub
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?