用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧VisualBasic

如何实现在TextBox中如何得知Caret所在位置

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:47:02
 

'在form中放一个textBox两个label

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 hwnd5 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

i = SendMessage(hwnd5, EM_GETSEL, wParam, lParam)

j = i / 2 ^ 16 '取得目前Caret所在前面有多少个byte

LineNo = SendMessage(hwnd5, EM_LINEFROMCHAR, j, 0) '取得前面有多少行

LineNo = LineNo + 1

k = SendMessage(hwnd5, EM_LINEINDEX, -1, 0)

'取得目前caret所在行前面有多少个byte

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 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


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:15,281.25000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号