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

百般思念 提交于 2019-12-08 14:29:46

问题


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 = "{\rtf1\ansi{\fonttbl{ \f0\fnil\fcharset0\fprq0\fttruetype Helvetica; }{\f1\fnil\fcharset0\fprq0\fttruetype Bitstream Charter; }}{\f1\fs24 Ceci est un texte accentu\'e9}\par{ \f0\fs24 avec des caract\'e8res {\b gras},}\par{ \f1 des{ \fs18 petits } et des{ \fs32 gros }. }}";

    std::stringstream rtf("{\rtf1\ansi{\fonttbl{ \f0\fnil\fcharset0\fprq0\fttruetype Helvetica; }{\f1\fnil\fcharset0\fprq0\fttruetype Bitstream Charter; }}{\f1\fs24 Ceci est un texte accentu\'e9}\par{ \f0\fs24 avec des caract\'e8res {\b 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.


回答1:


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.



来源:https://stackoverflow.com/questions/51560550/c-win32-load-rtf-data-to-rich-edit-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!