|
|
|
|
Forum
Please
Log In
to post a new message or reply to an existing one. If you are not registered, please
register.
NOTE: Some forums may be read-only if you are not currently subscribed to
our technical support services.
Subject |
Author |
Date |
|
tera tera
|
Nov 10, 2008 - 1:28 AM
|
Hello. Please teach a sample to load Text data or Save Text Data.
|
|
Technical Support
|
Nov 10, 2008 - 11:41 AM
|
The rich edit control supports stream based getting and setting of its text. Here is the very simple article describing how to load rich text into rich text editor control:
http://www.codeguru.com/richedit/rtf_string_streamin.shtml
Here is the source code based on article above: class CTeraRichEditCtrl : public CRichEditCtrl
{
public:
CString GetRTF();
void SetRTF( const CString & sRTF );
CString GetPlainText();
void SetPlainText( const CString & sPlainText );
protected:
static __EXT_MFC_DWORD_PTR CALLBACK stat_StreamIn( __EXT_MFC_DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb );
static __EXT_MFC_DWORD_PTR CALLBACK stat_StreamOut( __EXT_MFC_DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG* pcb );
};
CString CTeraRichEditCtrl::GetRTF()
{
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = stat_StreamOut;
CString sRTF = _T("");
es.dwCookie = (__EXT_MFC_DWORD_PTR) &sRTF;
StreamOut( SF_RTF, es );
return sRTF;
}
void CTeraRichEditCtrl::SetRTF( const CString & sRTF )
{
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = stat_StreamIn;
es.dwCookie = (__EXT_MFC_DWORD_PTR) LPVOID(&sRTF);
StreamIn( SF_RTF, es );
}
CString CTeraRichEditCtrl::GetPlainText()
{
HideSelection( TRUE, FALSE );
long nStartChar = 0, nEndChar = 0;
GetSel( nStartChar, nEndChar );
long nTextLength = GetTextLength();
SetSel( 0, nTextLength );
CString strPlainText( _T("") );
strPlainText = GetSelText();
SetSel( nStartChar, nEndChar );
HideSelection( FALSE, FALSE );
return strPlainText;
}
void CTeraRichEditCtrl::SetPlainText( const CString & sPlainText )
{
EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = stat_StreamIn;
es.dwCookie = (__EXT_MFC_DWORD_PTR) LPVOID(&sPlainText);
StreamIn( SF_TEXT, es );
}
__EXT_MFC_DWORD_PTR CALLBACK CTeraRichEditCtrl::stat_StreamIn( __EXT_MFC_DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb )
{
CString *pstr = (CString *) dwCookie;
if ( pstr->GetLength() < cb )
{
(*pcb) = pstr->GetLength();
::memcpy( pbBuff, (LPCSTR) *pstr, *pcb );
pstr->Empty();
}
else
{
*pcb = cb;
::memcpy( pbBuff, (LPCSTR) *pstr, *pcb );
*pstr = pstr->Right(pstr->GetLength() - cb);
}
return 0;
}
__EXT_MFC_DWORD_PTR CALLBACK CTeraRichEditCtrl::stat_StreamOut( __EXT_MFC_DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb )
{
CString * pStream = (CString*)dwCookie;
ASSERT( pStream != NULL );
char * tmp_buf = (char *)::malloc( cb+1 );
if( tmp_buf != NULL )
{
::memcpy( tmp_buf,pbBuff, cb );
tmp_buf[cb] = ’\0’;
(*pStream) += tmp_buf;
::free( tmp_buf );
}
return 0;
}
|
|
tera tera
|
Nov 10, 2008 - 5:30 PM
|
|
|