VB获取richtextbox光标所在的位置
在网上找到如下代码,但是不清楚怎样将光标的位置显示在一个TextBox里,请高手指点,谢谢'支持中文,一个汉字算一列ConstWM_USER=&H400ConstEM_E...
在网上找到如下代码,但是不清楚怎样将光标的位置显示在一个TextBox里,请高手指点,谢谢
'支持中文,一个汉字算一列
Const WM_USER = &H400
Const EM_EXGETSEL = WM_USER + 52
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Const EM_GETSEL = &HB0
Private Type CHARRANGE
cpMin As Long
cpMax As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
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
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Function GetCurPos(ByRef TextControl As Control) As POINTAPI
Dim LineIndex As Long
Dim SelRange As CHARRANGE
Dim TempStr As String
Dim TempArray() As Byte
Dim CurRow As Long
Dim CurPos As POINTAPI
TempArray = StrConv(TextControl.Text, vbFromUnicode)
Call SendMessage(TextControl.hWnd, EM_EXGETSEL, 0, SelRange)
CurRow = SendMessage(TextControl.hWnd, EM_LINEFROMCHAR, SelRange.cpMin, 0)
LineIndex = SendMessage(TextControl.hWnd, EM_LINEINDEX, CurRow, 0)
If SelRange.cpMin = LineIndex Then
GetCurPos.x = 1
Else
TempStr = String(SelRange.cpMin - LineIndex, 13)
CopyMemory ByVal StrPtr(TempStr), ByVal StrPtr(TempArray) + LineIndex, SelRange.cpMin - LineIndex
TempArray = TempStr
ReDim Preserve TempArray(SelRange.cpMin - LineIndex - 1)
TempStr = StrConv(TempArray, vbUnicode)
GetCurPos.x = Len(TempStr) + 1
End If
GetCurPos.y = CurRow + 1
End Function 展开
'支持中文,一个汉字算一列
Const WM_USER = &H400
Const EM_EXGETSEL = WM_USER + 52
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Const EM_GETSEL = &HB0
Private Type CHARRANGE
cpMin As Long
cpMax As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
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
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Function GetCurPos(ByRef TextControl As Control) As POINTAPI
Dim LineIndex As Long
Dim SelRange As CHARRANGE
Dim TempStr As String
Dim TempArray() As Byte
Dim CurRow As Long
Dim CurPos As POINTAPI
TempArray = StrConv(TextControl.Text, vbFromUnicode)
Call SendMessage(TextControl.hWnd, EM_EXGETSEL, 0, SelRange)
CurRow = SendMessage(TextControl.hWnd, EM_LINEFROMCHAR, SelRange.cpMin, 0)
LineIndex = SendMessage(TextControl.hWnd, EM_LINEINDEX, CurRow, 0)
If SelRange.cpMin = LineIndex Then
GetCurPos.x = 1
Else
TempStr = String(SelRange.cpMin - LineIndex, 13)
CopyMemory ByVal StrPtr(TempStr), ByVal StrPtr(TempArray) + LineIndex, SelRange.cpMin - LineIndex
TempArray = TempStr
ReDim Preserve TempArray(SelRange.cpMin - LineIndex - 1)
TempStr = StrConv(TempArray, vbUnicode)
GetCurPos.x = Len(TempStr) + 1
End If
GetCurPos.y = CurRow + 1
End Function 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询