Click to See Complete Forum and Search --> : Color text to a Rich Text Edit box


zapper222
February 13th, 2008, 10:13 AM
Trying to print a string in specific color with following code:

void CRichEditDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CHARFORMAT Cfm;

m_Richer.GetSelectionCharFormat(Cfm);

Cfm.cbSize = sizeof(CHARFORMAT);
Cfm.dwMask = CFM_COLOR|CFM_BOLD;
Cfm.dwEffects = CFE_BOLD;
Cfm.crTextColor = RGB(0,0,255);

m_Richer.SetSelectionCharFormat(Cfm);

CString text = "Now is the time for all good men to come to the aid of the party";

SETTEXTEX SetTextEx;
SetTextEx.codepage = 1200;
SetTextEx.flags = ST_SELECTION;

m_Richer.SetSel(-1,-1);
m_Richer.SendMessage(EM_SETTEXTEX,(WPARAM)&SetTextEx, (LPARAM) &text);
}

Unfortunatly this prints only garbage.....correct color and in bold but not the right string. Anyone know what I am doing wrong ??

kirants
February 13th, 2008, 02:19 PM
don't do a &text.
When you do a &text which is a CString object, you are passing the address of the object. This address is not really a pointer to a string. Instead you do this:
(LPARAM)(LPCTSTR)text

The LPCTSTR operator will extract a pointer to the string. Also, since you specify codepage 1200, are you sure you are using a UNICODE build ?

zapper222
February 13th, 2008, 02:24 PM
don't do a &text.
...... are you sure you are using a UNICODE build ?

Changed it to CP_ACP and all works well now.

Thanks again.
Zapper222