Richedit中RTF文本和纯文本之间的转换 - vc

shaxunyeman 2008-10-17 04:51:29
我用API创建 RichEdit 控件(2.0版),通过 GetRTF (此方法调用了 StreamOut 、EditStreamOutCallback 方法,下面有方法的实现) 函数我获取到了输入 RichEdit 控件中的RTF格式的文本,现在我想把RTF文本写入到 RichEdit 控件中,并且显示的要是纯文本格式的.
如RTF文本:{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fswiss\fprq2\fcharset134 Arial;}}
\viewkind4\uc1\pard\lang2052\f0\fs18 this is a test!\f1\par
}

显示的时候,要是 this is a test! 这样的纯文本。请各位大虾赐教。


//函数实现

long StreamOut(HWND hwnd,int nFormat,EDITSTREAM& es)
{
_ASSERT(hwnd != NULL);
_ASSERT(::IsWindow(hwnd));

LRESULT lresult = ::SendMessage(hwnd,EM_STREAMOUT,(WPARAM)nFormat,(LPARAM)&es);
return lresult;
}

DWORD CALLBACK EditStreamOutCallback(DWORD_PTR dwCookie,LPBYTE pbBuff,LONG cb,LONG *pcb)
{
_ASSERT(dwCookie != NULL);
TRACE("cb = %d *pcb = %d\n",cb,*pcb);
wstring *dwEntry = (wstring*)dwCookie;

vector<char> _vectorRTF ;
_vectorRTF.resize(cb);
memcpy(&_vectorRTF[0],pbBuff,cb);
char *pstr = &_vectorRTF[0];

wchar_t *pwstr = new wchar_t[cb*sizeof(wchar_t)];
if (pwstr)
{
ZeroMemory(pwstr,cb);
MultiByteToWideChar(CP_ACP, 0, (char*)pstr, -1, pwstr, cb*sizeof(wchar_t));
*dwEntry += pwstr;
delete []pwstr;
pwstr = NULL;
}
_vectorRTF.clear();
return 0;
}

long GetRTF(HWND hwnd,wstring &_wstrRTF)
{
_ASSERT(hwnd != NULL);
_ASSERT(::IsWindow(hwnd));

EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = EditStreamOutCallback;
es.dwCookie = (DWORD_PTR) &_wstrRTF;
StreamOut(hwnd,SF_RTF,es);
size_t iLen = _wstrRTF.size();

return iLen;
}
...全文
158 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-10-22
  • 打赏
  • 举报
回复
呵呵,会的不难,难的不会!

友情up~~
shaxunyeman 2008-10-22
  • 打赏
  • 举报
回复
今天晚上总算琢磨出来了,以下是我简略代码:
简单示例:
DWORD CALLBACK CTestRicheditDlg::EditStreamCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb,
LONG *pcb)
{
wstring *pstr = (wstring *)dwCookie;
WideCharToMultiByte(CP_OEMCP,NULL,pstr->c_str(),-1,(LPSTR)pbBuff,cb,NULL,FALSE); //这个很重要,千万要转换为单字节
return 0;
}

//实现
EDITSTREAM es = {(DWORD)&str, 0, EditStreamCallBack};

//api的话,发送EM_STREAMIN 消息实现StreamIn函数就是喽
((CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21))->StreamIn(SF_RTF | SFF_SELECTION, es);
帅得不敢出门 2008-10-22
  • 打赏
  • 举报
回复
Retrieves the text from the current selection in this CRichEditCtrl object.
long GetSelText(
LPSTR lpBuf
) const;
CString GetSelText( ) const;

这个不行么?

shaxunyeman 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zmlovelx 的回复:]
Retrieves the text from the current selection in this CRichEditCtrl object.
long GetSelText(
LPSTR lpBuf
) const;
CString GetSelText( ) const;

这个不行么?
[/Quote]
我现在是要设置,把RTF格式的文本设置为纯文本格式,不是获取。
要求是api实现。^_^
shaxunyeman 2008-10-21
  • 打赏
  • 举报
回复
开贴几天了,竟然没人回答?很难吗?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧