CRichEditCtrl::GetSelText() is not working right

后端 未结 2 1868
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 04:30

MFC File: winctrl4.cpp

(C:\\Program Files\\Microsoft Visual Studio 8\\VC\\atlmfc\\src\\mfc)

CString CRichEditCtrl::GetSelText() const
{
    ASSERT(::         


        
2条回答
  •  余生分开走
    2021-01-25 05:01

    To get a wide char string you have to use the EM_GETTEXTEX message. CRichEditCtrl source does not contain a method which utilizes such message. Here is a correct implementation of GetSelText() which actually does return Unicode characters:

    CString CRichEditCtrlFix::GetSelText() const
    {
        ASSERT(::IsWindow(m_hWnd));
        CHARRANGE cr;
        cr.cpMin = cr.cpMax = 0;
        ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
        CString strText;
        int sz = (cr.cpMax - cr.cpMin + 1) * sizeof(tchar);
        LPTSTR lpsz = strText.GetBufferSetLength(sz);
        lpsz[0] = NULL;
        GETTEXTEX gte;
        memset( >e, 0, sizeof(GETTEXTEX) );
        gte.cb = sz;
        gte.flags = GT_SELECTION;
        if( sizeof(tchar) == 2 ) gte.codepage = 1200;
        ::SendMessage(m_hWnd, EM_GETTEXTEX, (WPARAM)>e, (LPARAM)lpsz);
        strText.ReleaseBuffer();
        return CString(strText);
    }
    

    1200 here means UTF-16LE

提交回复
热议问题