4个回答
展开全部
在很多的编辑软件中有这样一个功能,即当用户在编辑区中输入字符或者按动鼠标使得光标的位置改变时,在编辑软件下方的状态栏中就能显示出光标所在位置的行和列的值。下面就介绍如何用VB编程实现在文本框中的这一功能。
首先,在Form中添加一个文本框(TextBox),将其MultiLine属性设置为True,以便输入多行文本;再在Form中添加两个标题栏(Label),以便显示光标位置;然后,在Form的代码窗口中添加程序中的代码。
Option Explici
Const EM_GETSEL = &HB0
Const EM_LINEFROMCHAR = &HC9
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
'再向文本框传递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
Private Sub Form_Load()
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(Text1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo
Label2.Caption = ColNo
End Sub
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(Text1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo
Label2.Caption = ColNo
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(Text1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo
Label2.Caption = ColNo
End Sub
(程序)
运行上面的程序,在TextBox中输入字符或者按控制键来移动光标时,你可以看到屏幕下面的Label中就能显示出的光标所在的位置。以上程序在Win 95,VB 5.0下运行通过。
首先,在Form中添加一个文本框(TextBox),将其MultiLine属性设置为True,以便输入多行文本;再在Form中添加两个标题栏(Label),以便显示光标位置;然后,在Form的代码窗口中添加程序中的代码。
Option Explici
Const EM_GETSEL = &HB0
Const EM_LINEFROMCHAR = &HC9
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
'再向文本框传递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
Private Sub Form_Load()
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(Text1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo
Label2.Caption = ColNo
End Sub
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(Text1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo
Label2.Caption = ColNo
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(Text1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo
Label2.Caption = ColNo
End Sub
(程序)
运行上面的程序,在TextBox中输入字符或者按控制键来移动光标时,你可以看到屏幕下面的Label中就能显示出的光标所在的位置。以上程序在Win 95,VB 5.0下运行通过。
展开全部
在代码中用selstart属性,比方是text1的光标位置:text1.selstart
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
人家2L (mvlcrx )的也是一个参考答案,又不是毫不相关的,这样骂人,谁愿意帮你。虽然这么久了,看你这么没素质,忍不住说你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Print Text1.SelStart
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询