我在从字符串内部的RichEdit控件保存新闻行时遇到问题。
我正在做的是:


从RichEdit控件获取文本
分割所有以空格分隔的内容
添加一些RTF格式
“保险丝”两个词重新组合在一起
发送文字进行控制


我不确定是什么原因引起的,所以这是最相关的部分:

int RichEdit::GetTextLength() const
{
    GETTEXTLENGTHEX len;
    len.codepage = 1200;
    len.flags = GTL_NUMBYTES;
    return (int)SendMessage(this->handle, EM_GETTEXTLENGTHEX, (WPARAM)&len, 0) + 1;
}

tstring RichEdit::GetText() const
{
    auto len = this->GetTextLength();
    GETTEXTEX str;

    TCHAR* tmp = new TCHAR[len];
    str.cb = len;
    str.flags = GT_USECRLF;
    str.codepage = 1200;


    str.lpDefaultChar = NULL;
        str.lpUsedDefChar = NULL;

        (void)SendMessage(this->handle, EM_GETTEXTEX, (WPARAM)&str, (LPARAM)tmp);

        tstring ret(tmp);

        delete[] tmp;
        return ret;
    }

void RichEdit::SetRtfText(const tstring& text, int flags)
    {
        DWORD WideLength = text.length();
        DWORD Length     = WideLength * 4;
        PSTR Utf8        = (PSTR)malloc(Length);

        int ReturnedLength = WideCharToMultiByte(CP_UTF8,
            0,
            text.c_str(),
            WideLength-1,
            Utf8,
            Length-1,
            NULL,
            NULL);

        if (ReturnedLength)
            Utf8[ReturnedLength] = 0;

        SETTEXTEX st = {0};
        st.flags = flags;
        st.codepage = CP_UTF8;
        (void)SendMessage(this->handle, EM_SETTEXTEX, (WPARAM)&st, (LPARAM)Utf8 );

        free(Utf8);
    }

void split ( tstring input , tstring split_id, std::vector<std::pair<tstring,bool>>& res ) {
    std::vector<std::pair<tstring,bool>> result;
    int i = 0;
    bool add;
    tstring temp;
    std::wstringstream ss;
    size_t found;
    tstring real;
    int r = 0;
    while ( i != input.length() )
    {
        add = false;
        ss << input.at(i);
        temp = ss.str();

        found = temp.find(split_id);
        if ( found != tstring::npos )
        {
            add = true;
            real.append ( temp , 0 , found );
        } else if ( r > 0 &&  ( i+1 ) == input.length() )
        {
            add = true;
            real.append ( temp , 0 , found );
        }
        if ( add )
        {
            result.emplace_back(std::make_pair(real,false));
            ss.str(tstring());
            ss.clear();
            temp.clear();
            real.clear();
            r = 0;
        }
        i++;
        r++;
    }
    res = result;
}


ps:tstring只是std :: wstring / std :: string的typedef

如何保存换行符?

最佳答案

您的代码有很多问题。

您的代码基于TCHAR,但是您实际上并没有正确地使用TCHAR检索/设置RTF数据。

检索文本时,您正在将换行符归一化为CRLF,但是对文本长度进行归一化时并没有进行同样的归一化,因此它们将彼此不同步。

您正在使用UTF-8将数据写入RichEdit,但是RTF是基于ASCII的格式,对Unicode数据使用了转义序列。如果要以Unicode检索数据,则最好也使用Unicode编写数据,并确保从一开始就正确地进行了所有操作。让RichEdit控件为您处理Unicode。

您使用WideCharToMultiByte()是错误的。您根本不应该从字符串长度中减去-1。您可能要考虑空终止符,但长度值不包括以空终止符开头的值。如果要坚持使用UTF-8,则应该使用WideCharToMultiByte()来计算正确的UTF-8长度,而不是对其进行硬编码。

int Length = WideCharToMultiByte(CP_UTF8, 0, text.c_str(), text.length(), NULL, 0, NULL, NULL);
char Utf8 = new char[Length+1];
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), text.length(), Utf8, Length, NULL, NULL);
Utf8[Length] = 0;
...
delete[] Utf8;


话虽如此,如果您要坚持使用TCHAR,请尝试以下操作:

#ifdef UNICODE
#define RTFCodePage 1200
#else
#define RTFCodePage CP_ACP
#endif

int RichEdit::GetTextLength() const
{
    GETTEXTLENGTHEX len = {0};
    len.codepage = RTFCodePage;
    len.flags = GTL_NUMCHARS | GTL_USECRLF;
    return SendMessage(this->handle, EM_GETTEXTLENGTHEX, (WPARAM)&len, 0);
}

tstring RichEdit::GetText() const
{
    int len = this->GetTextLength() + 1;

    GETTEXTEX str = {0};
    str.cb = len * sizeof(TCHAR);
    str.flags = GT_USECRLF;
    str.codepage = RTFCodePage;

    vector<TCHAR> tmp(len);
    len = SendMessage(this->handle, EM_GETTEXTEX, (WPARAM)&str, (LPARAM)&tmp[0]);

    return tstring(&tmp[0], len-1);
}

void RichEdit::SetRtfText(const tstring& text, int flags)
{
    SETTEXTEX st = {0};
    st.flags = flags;
    st.codepage = RTFCodePage;

    #ifdef UNICODE
    st.flags |= ST_UNICODE;
    #endif

    SendMessage(this->handle, EM_SETTEXTEX, (WPARAM)&st, (LPARAM)text.c_str());
}


最好删除TCHAR并仅对所有内容使用Unicode:

int RichEdit::GetTextLength() const
{
    GETTEXTLENGTHEX len = {0};
    len.codepage = 1200;
    len.flags = GTL_NUMCHARS | GTL_USECRLF;
    return SendMessage(this->handle, EM_GETTEXTLENGTHEX, (WPARAM)&len, 0);
}

wstring RichEdit::GetText() const
{
    int len = this->GetTextLength() + 1;

    GETTEXTEX str = {0};
    str.cb = len * sizeof(WCHAR);
    str.flags = GT_USECRLF;
    str.codepage = 1200;

    vector<WCHAR> tmp(len);
    len = SendMessage(this->handle, EM_GETTEXTEX, (WPARAM)&str, (LPARAM)&tmp[0]);

    return wstring(tmp, len-1);
}

void RichEdit::SetRtfText(const wstring& text, int flags)
{
    SETTEXTEX st = {0};
    st.flags = flags | ST_UNICODE;
    st.codepage = 1200;

    SendMessage(this->handle, EM_SETTEXTEX, (WPARAM)&st, (LPARAM)text.c_str());
}


更新:如果必须返回UTF-8的EM_SETTEXTEX消息,请尝试以下操作:

void RichEdit::SetRtfText(const tstring& text, int flags)
{
    string Utf8;
    int Length;

    #ifdef UNICODE

    Length = WideCharToMultiByte(CP_UTF8, 0, text.c_str(), text.length(), NULL, 0, NULL, NULL);
    if (Length > 0)
    {
        Utf8.resize(Length);
        WideCharToMultiByte(CP_UTF8, 0, text.c_str(), text.length(), &Utf8[0], Length, NULL, NULL);
    }

    #else

    Length = MultiByteToWideChar(CP_ACP, 0, text.c_str(), text.length(), NULL, 0);
    if (Length > 0)
    {
        vector<WCHAR> tmp(Length);
        MultiByteToWideChar(CP_ACP, 0, text.c_str(), text.length(), &tmp[0], Length);

        Length = WideCharToMultiByte(CP_UTF8, 0, tmp.c_str(), tmp.length(), NULL, 0, NULL, NULL);
        if (Length > 0)
        {
            Utf8.resize(Length);
            WideCharToMultiByte(CP_UTF8, 0, tmp.c_str(), tmp.length(), &Utf8[0], Length, NULL, NULL);
        }
    }

    #endif

    SETTEXTEX st = {0};
    st.flags = flags & ~ST_UNICODE;
    st.codepage = CP_UTF8;
    SendMessage(this->handle, EM_SETTEXTEX, (WPARAM)&st, (LPARAM)Utf8.c_str());
}

关于c++ - 无法在RichEdit中保留换行符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15263528/

10-11 23:16