How to achieve the location of the Caret in TextBox

zhaozj2021-02-11  161

'Put a TextBox in Form 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 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 'How many BYTEs have to be in front of Caret?

Lineno = SendMessage (hwnd5, em_linefromchar, j, 0) 'How many lines have you got?

LINENO = LINENO 1

K = sendMessage (HWND5, EM_LINEINDEX, -1, 0)

'How many BYTEs have to be in front of the Caret in the Caret?

COLNO = J - K 1

End Sub

Private sub flow_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

转载请注明原文地址:https://www.9cbs.com/read-5628.html

New Post(0)