vb中如何判断text文件中光标的位置,并在光标所在行的下一行插入一行数

2024-12-18 23:57:45
推荐回答(4个)
回答1:

反正我只知道可以通过api函数,知道文本框中的光标位于第几行第几列,用法如下:
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB

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

Public Sub getCaretPos(ByVal texthwnd As Long, lineno As Long, colno As Long)
Dim i As Long, j As Long
Dim lparam As Long, wparam As Long
Dim k As Long
'向文本框传递EM_GETSEL消息以获取从起始位置到光标所在位置的字符数
i = SendMessage(texthwnd, EM_GETSEL, wparam, lparam)
j = i / 2 ^ 16
P = j
'向文本框传递EM_LINEFROMCHAR消息根据获得的字符数确定光标所在行数
lineno = SendMessage(texthwnd, EM_LINEFROMCHAR, j, 0)
lineno = lineno + 1
'向文本框传递EM_LINEINDEX消息以获取所在列数
k = SendMessage(texthwnd, EM_LINEINDEX, -1, 0)
colno = j - k + 1
End Sub

回答2:

反正我只知道可以通过api函数,知道文本框中的光标位于第几行第几列,用法如下:
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB

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

Public Sub getCaretPos(ByVal texthwnd As Long, lineno As Long, colno As Long)
Dim i As Long, j As Long
Dim lparam As Long, wparam As Long
Dim k As Long
'向文本框传递EM_GETSEL消息以获取从起始位置到光标所在位置的字符数
i = SendMessage(texthwnd, EM_GETSEL, wparam, lparam)
j = i / 2 ^ 16
P = j

回答3:

Private Sub Text1_Click()
If Text1.SelStart = 0 Then Text1.SelStart = 1
p = InStr(Text1.SelStart, Text1, vbCrLf)
If p > 0 Then
Text1 = Left(Text1, p - 1) & vbCrLf & vbCrLf & Mid(Text1, p + 1)
Text1.SelStart = p + 1
Text1.SetFocus
End If
End Sub

Private Sub Text1_GotFocus()
Text1_Click
End Sub

回答4:

用text1.selstart属性