I was trying to combine different styles in a CRichEditCtrl control but couldn't find out how.
My goal:
to send this string "
Normal text:\n```some code```\nMore text\n" and have the '
some code' part shown differently.
What I have tried:
I tried the following but the text looks the same.
DWORD CALLBACK EditStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG* pcb)
{
CString* pStr = reinterpret_cast<CString*>(dwCookie);
*pcb = pStr->GetLength() * sizeof(TCHAR);
memcpy(pbBuff, pStr->GetBuffer(), *pcb);
return 0;
}
void myDialog::ApplySourceCodeStyleToText(CRichEditCtrl& m_editControl)
{
CString text;
m_editControl.GetWindowTextW(text);
int startPos = text.Find(L"```");
int endPos = text.Find(L"```", startPos + 3);
if (startPos != -1 && endPos != -1 && endPos > startPos)
{
CString code = text.Mid(startPos + 3, endPos - startPos - 3);
CString rtfSourceCodeStyle = L"{\\rtf1\\ansi\\deff0{\\colortbl;\\red0\\green0\\blue255;}{\\f1\\fs20\\cf1 ";
CString rtfEnd = L"}}";
CString rtfText;
rtfText.Format(L"%s%s%s", text.Left(startPos + 3), rtfSourceCodeStyle, code);
rtfText.AppendFormat(L"%s%s", rtfEnd, text.Mid(endPos));
CString plainText;
EDITSTREAM es;
es.dwCookie = reinterpret_cast<DWORD_PTR>(&plainText);
es.pfnCallback = EditStreamCallback;
m_editControl.StreamIn(SF_TEXT, es);
m_editControl.SetWindowTextW(rtfText);
m_editControl.SetSel(0, -1);
m_editControl.ReplaceSel(plainText, TRUE);
m_editControl.SetSel(-1, 0);
}
}