Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

win32gui - C++ WIN32 - Load RTF Data to Rich Edit Control

I try to load text (formatting in RTF) to my rich text control but it doesn't work. I've even tried to use

WriteFile((HANDLE)dwCookie, myBuff, cb, (DWORD*)pcb, NULL); 

instead of

*pcb = rtf->readsome((char*)pbBuff, cb);

void CreateRichEdit(HWND hwndOwner, int x, int y, int width, int height, HINSTANCE hinst)
{
    LoadLibrary(TEXT("Msftedit.dll"));

    edittext = CreateWindowEx(0, TEXT("RICHEDIT50W"), TEXT("Type here"), ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_AUTOVSCROLL | WS_VSCROLL,
        x, y, width, height,
        hwndOwner, NULL, hinst, 0);

    std::string teext = "{
tf1ansi{fonttbl{ f0fnilfcharset0fprq0fttruetype Helvetica; }{f1fnilfcharset0fprq0fttruetype Bitstream Charter; }}{f1fs24 Ceci est un texte accentu'e9}par{ f0fs24 avec des caract'e8res { gras},}par{ f1 des{ fs18 petits } et des{ fs32 gros }. }}";

    std::stringstream rtf("{
tf1ansi{fonttbl{ f0fnilfcharset0fprq0fttruetype Helvetica; }{f1fnilfcharset0fprq0fttruetype Bitstream Charter; }}{f1fs24 Ceci est un texte accentu'e9}par{ f0fs24 avec des caract'e8res { gras},}par{ f1 des{ fs18 petits } et des{ fs32 gros }. }}");
    //std::stringstream rtf("...");

    EDITSTREAM es = { 0 };
    es.dwError = 0;
    es.dwCookie = (DWORD_PTR)&rtf;
    es.pfnCallback = EditStreamInCallback;

    SendMessage(edittext, EM_STREAMIN, SF_RTF, (LPARAM)&es);
}


DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb)
{

    std::stringstream * rtf = (std::stringstream*) dwCookie;
    std::string text = (*rtf).str();
    char myBuff[500];
    *pcb = rtf->readsome((char*)pbBuff, cb);

    return *pcb;
}

I've also tried to uncomment std::stringstream rtf("..."); just for writing ... in my edit control but it doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By returning the number of bytes read from the stream (in this case a nonzero number of bytes), you're telling the control that the edit stream callback was unsuccessful. Try return *pcb > 0 ? 0 : 1; for the return from EditStreamInCallback. You could also consider using rtf->fail() to determine success of this callback. Additionally, testing rtf against NULL or nullptr would be a good idea (as well as indication of success or failure).

https://docs.microsoft.com/en-us/windows/desktop/api/Richedit/nc-richedit-editstreamcallback

The callback function returns zero to indicate success.

The callback function returns a nonzero value to indicate an error. If an error occurs, the read or write operation ends and the rich edit control discards any data in the pbBuff buffer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...