您的位置:程序门 -> vb -> 基础类



对richedit20控件发送wm_gettext得到的是乱码?如何解决呀


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


对richedit20控件发送wm_gettext得到的是乱码?如何解决呀[已结贴,结贴人:spark_li]
发表于:2007-07-18 16:07:38 楼主
xhwnd是一个richedit20a控件的句柄

        i   =   sendmessage(xhwnd,   wm_gettextlength,   0,   byval   0&)
       
        dim   buff()   as   byte
        redim   buff(i)
        i   =   sendmessage(xhwnd,   wm_gettext,   i   +   1,   buff(0))
     
        msgbox   strconv(buff,   vbunicode)


不知道为什么?我确保   xhwnd是正确的
是不是要对返回的数据做适当处理呀?
发表于:2007-07-18 20:40:091楼 得分:10
试试em_gettextex   消息
发表于:2007-07-18 21:22:452楼 得分:40
this   is   impossible   mission!
http://community.csdn.net/expert/topicview3.asp?id=5648720
http://community.csdn.net/expert/topicview3.asp?id=5646744

重申:
(不考虑win9x,因为更复杂)对于xp英文/中文系统,如果你的non-unicode设定是english,   sendmessage   根本就不可能得到中文.但如果non-unicode设定是chinese   (prc),上面的编码可以取得中文。原因在于sendmessage有经过unicode到ansi/dbcs的几次转换,造成unicode的丢失。

我挣扎多年都没成功!!!不信你们将你的non-unicode设定为english!!!
control   panel   -->   language   and   region   options   -->   advance   tab   --->   english   as   non-unicode

'richedit   getline   function   from   vbadvisor
public   type   textrange
        chrg   as   charrange
        lpstrtext   as   long        
end   type

public   function   getlinetext(byval   hwnd   as   long,byval   linenum   as   long)   as   string
dim   linecount   as   long
dim   lc   as   long,   j   as   long
dim   charfrom   as   long
dim   charend   as   long
dim   cr   as   charrange
dim   tr   as   textrange

linecount   =   sendmessagelong(hwnd,   em_getlinecount,   byval   0&,   byval   0&)
if   linenum   >   linecount   then
      getlinetext   =   vbnullstring
      exit   function
end   if
charfrom   =   sendmessagelong(hwnd,   em_lineindex,   linenum,   byval   0&)
lc   =   sendmessagelong(hwnd,   em_linelength,   byval   charfrom,   byval   0&)
if   lc   =   0   then
    getlinetext   =   vbnullstring
    exit   function
end   if

getlinetext   =   textinrange(charfrom,   charfrom   +   lc)

end   function

public   sub   textinrange(byval   hwnd   as   long,byval   lstart   as   long,   byval   lend   as   long)

dim   tr   as   textrange
dim   stext   as   string
dim   lr   as   long
dim   b()   as   byte
       
        tr.chrg.cpmin   =   lstart
        tr.chrg.cpmax   =   lend

        '   vb   won 't   do   the   terminating   null   for   you!
        stext   =   string$(lend   -   lstart   +   1,   0)
        b   =   stext
        redim   preserve   b(0   to   (lend   -   lstart   +   1))   as   byte
        tr.lpstrtext   =   varptr(b(0))
       
        lr   =   sendmessagelong(hwnd,   em_gettextrange,   0,   varptr(tr))
       
        if   (lr   >   0)   then
                '   lstrlen   assumes   that   lpstring   is   a   null-terminated   string   !!!
                copymemory   byval   stext,   byval   tr.lpstrtext,   lr  
                textinrange   =   left$(stext,   lr)
        end   if

end   sub

'textbox   getline   function   from   vbadvisor
public   function   getline(byval   hwnd   as   long,byval   whichline   as   long)   as   string

dim   nlen   as   long,   barr()   as   byte,   barr2()   as   byte,   lreturn   as   long

        lreturn   =   sendmessage(hwnd   ,   em_lineindex,   whichline,   byval   0&)

        nlen   =   sendmessage(hwnd   ,   em_linelength,   lreturn,   byval   0&)

        if   nlen   >   0   then
                redim   barr(2   *   nlen   +   1)   as   byte,   barr2(2   *   nlen   -   1)   as   byte
                call   copymemory(barr(0),   2   *   nlen,   2)  
                call   sendmessage(hwnd   ,   em_getline,   whichline,   barr(0))
                call   copymemory(barr2(0),   barr(0),   2   *   nlen)

                getline   =   string$(ubound(barr2)   +   1,   vbnullchar)
                copymemory   byval   getlinestring,   barr2(0),   ubound(barr)   +   1

        else
                getline   =   vbnullstring
        end   if

end   function
发表于:2007-07-18 21:34:393楼 得分:10
帮你顶一下
发表于:2007-07-19 12:23:484楼 得分:0
re
发表于:2007-07-19 14:24:205楼 得分:10
......................................
发表于:2007-07-19 16:35:426楼 得分:30
英文的没试过,不过中文下这个函数获取qq的richedit没问题

public   function   getwindowcontent(hwnd   as   long)   as   string
        dim   sbuffer   as   string
        dim   llength   as   long
        llength   =   sendmessage(hwnd,   wm_gettextlength,   0,   byval   0&)
        sbuffer   =   string(llength   +   1,   0)
        sendmessage   hwnd,   wm_gettext,   llength   +   1,   byval   sbuffer
        getwindowcontent   =   left(sbuffer,instr(1,sbuffer,chr(0)))
end   function
发表于:2007-07-19 20:37:557楼 得分:0
奇怪,用楼上的代码
我在qq2006下不行,

hwnd肯定是没有问题的


快速检索

最新资讯
热门点击