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

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 vbdabster 的 blog

 

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

本文关键:如何实现在TextBox中如何得知Caret所在位置
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top