Richedit中RTF文本和纯文本之间的转换 - vc
我用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;
}