本文介绍了C ++ WIN32-将RTF数据加载到Rich Edit控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将文本(以 RTF 格式)加载到我的RTF控件中,但是它不起作用.我什至尝试使用

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);

代替

*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;
}

我也试图取消注释std::stringstream rtf("...");只是为了在编辑控件中编写...,但这不起作用.

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

推荐答案

通过返回从流中读取的字节数(在本例中为非零字节),您告诉控件编辑流回调是不成功.尝试从EditStreamInCallback返回的return *pcb > 0 ? 0 : 1;.您也可以考虑使用rtf->fail()确定此回调是否成功.此外,针对NULLnullptr测试rtf是一个好主意(以及成功或失败的指示).

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/zh-CN/windows/desktop/api/Richedit/nc-richedit-editstreamcallback

回调函数返回一个非零值以指示错误.如果发生错误,则读或写操作将结束,并且丰富编辑控件将丢弃pbBuff缓冲区中的所有数据.

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.

这篇关于C ++ WIN32-将RTF数据加载到Rich Edit控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:00