Question : Formatting CString for rich edit control.

I have a series of edit boxes that I combine the contents of and stream into a CRichEditCtrl for further processing.  I piece the contents together with something like this:

m_richeditstr.Format(_T("This " + m_box1 + " and " + m_box2... etc.

Then, I stream it in with the basic (more to it than this, but you get the point):

      EDITSTREAM es;
      es.dwError = 0;
      es.pfnCallback = CBStreamIn;
      es.dwCookie = (DWORD) &sRTF;
      StreamIn(SF_RTF, es);

It loads nicely as plain text, etc.  Which is fine, but I'd like to add something to this so when it loads in the rich text edit box, it'll be formatted.  

I tried something like m_richeditstr = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033...etc.  
But this isn't working.  I've tried several variations.  

When I save a document and restore it I put it into a CString and stream it in and it works fine.  I just want to add some formatting to basic edit box text just before streaming into a rich edit box for the first time.  Any suggestions would be appreciated.  

Answer : Formatting CString for rich edit control.

Here is the data of a very simple RTF file:

{\\rtf1 Hi {\\b there} you guys!}

The trick is that SetWindowText does not interpret the RTF; you have to stream it in:

//---------- theres' probably a lot easier way to stream in from a CString.  I'm an idiot

static DWORD CALLBACK
MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CString* pStr= (CString *)dwCookie;
   int nCurLen= pStr->GetLength();

   if ( nCurLen== 0 ) {
     *pcb= 0;
     return 0;
   }
   if ( nCurLen > cb ) {
      memmove( pbBuff, (LPCSTR)pStr, cb );
      *pStr= pStr->Mid(cb);
      *pcb= cb;
   }
   else {
      memmove( pbBuff, (LPCSTR)(*pStr), nCurLen );
      *pcb= nCurLen;
      *pStr= "";
   }
   return 0;
}

// in OnButton1 ....


CString sRtf="{\\rtf1 Hi {\\b there} you guys!}"

EDITSTREAM es;
es.dwError = 0;
es.pfnCallback = MyStreamInCallback;
es.dwCookie = (DWORD) &sRtf;
m_ctrlRichEdit.StreamIn(SF_RTF, es);

-=-==-=--==--=-==-=-=-=-
in "hand-crafting RTF, just remember to double-up the backslashes and to match each { with a }

-- Dan
Random Solutions  
 
programming4us programming4us