CRichEditCtrl::GetSelText() is not working right

后端 未结 2 1858
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  Happy的楠姐
    2021-01-25 05:12

    This is not the correct MFC source code, have you edited it? Using CStringA and LPSTR is quite inappropriate, the real code uses CString and LPTSTR so that Unicode is correctly handled. Yes, as posted the code would only return one character.


    Seeing the version helped. The bug is described in this feedback article. If you can't reasonably upgrade to VS2008 SP1, you could derive your own class from CRichEditCtrl and replace the function. For example:

    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;
        LPTSTR lpsz=strText.GetBufferSetLength((cr.cpMax - cr.cpMin + 1) * 2);
        lpsz[0] = NULL;
        ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);
        strText.ReleaseBuffer();
        return CString(strText);
    }
    

提交回复
热议问题