| 发表于:2007-07-04 18:04:247楼 得分:0 |
private const wm_user = &h400 private const em_exgetsel = wm_user + 52 private const em_linefromchar = &hc9 private const em_lineindex = &hbb private 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) '取得当前被选中文本的位置适用于richtextbox 'textcontrol用em_getsel消息 call sendmessage(textcontrol.hwnd, em_exgetsel, 0, selrange) '根据参数wparam指定的字符位置返回该字符所在的行号 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) '转换为unicode tempstr = strconv(temparray, vbunicode) getcurpos.x = len(tempstr) + 1 end if getcurpos.y = currow + 1 end function private sub richedit1_click() debug.print "y= " & getcurpos(richedit1).y debug.print "x= " & getcurpos(richedit1).x end sub | | |
|