Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long 利用lstrlen函数取出每个字符的占位符长度,再进行判断!
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 Const EM_CHARFROMPOS = &HD7 '注释:在API浏览器里无此值请自己加上.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
X = X / Screen.TwipsPerPixelX Y = Y / Screen.TwipsPerPixelY pos = X + Y * 65536 Lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)
Line = Lc \ 65536 '注释:第几行 CharPos = Lc Mod 65536 '注释:第几个字符 Label1 = "Line=" & Line & ",Char=" & CharPos '在Label1中显示结果 End Sub
要测试下面的例子,你需要在Form中放置以下控件: 1. 一个PictureBox控件,名为Picture1,Visible属性设为False。 2. 一个Label控件,名为Label1。 3. 一个TextBox控件,名为Text1。 Option ExplicitPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim iWidth As Long, iLoop As Long, iPos As Long Picture1.Font = Text1.Font iPos = -1 For iLoop = 1 To Len(Text1.Text) iWidth = Picture1.TextWidth(Left(Text1.Text, iLoop)) If iWidth > X Then iPos = iLoop Exit For End If Next iLoop If iPos > 0 Then Label1.Caption = iPos End Sub
利用lstrlen函数取出每个字符的占位符长度,再进行判断!
Const EM_CHARFROMPOS = &HD7 '注释:在API浏览器里无此值请自己加上.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
X = X / Screen.TwipsPerPixelX
Y = Y / Screen.TwipsPerPixelY
pos = X + Y * 65536
Lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)
Line = Lc \ 65536 '注释:第几行
CharPos = Lc Mod 65536 '注释:第几个字符
Label1 = "Line=" & Line & ",Char=" & CharPos '在Label1中显示结果
End Sub
1. 一个PictureBox控件,名为Picture1,Visible属性设为False。
2. 一个Label控件,名为Label1。
3. 一个TextBox控件,名为Text1。
Option ExplicitPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iWidth As Long, iLoop As Long, iPos As Long
Picture1.Font = Text1.Font
iPos = -1
For iLoop = 1 To Len(Text1.Text)
iWidth = Picture1.TextWidth(Left(Text1.Text, iLoop))
If iWidth > X Then
iPos = iLoop
Exit For
End If
Next iLoop
If iPos > 0 Then Label1.Caption = iPos
End Sub
http://community.csdn.net/Expert/TopicView.asp?id=4777511