Click here to Skip to main content
15,909,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I have created a CRichEditCtrl object as bellow and i tried to apply red color as background using SetBackgroundColor() method but its not giving result as i hope.
m_oRichEditCtrl.Create(WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_READONLY|WS_DISABLED,CRect(0,0,150,150),this,IDC_RICHEDIT);
COLORREF oldColor = m_oRichEditCtrl.SetBackgroundColor(FALSE,COLORREF(RGB(255,0,0)));
m_oRichEditCtrl.SetWindowText("Hello");


I found that when i give WS_DISABLED style, its not applying background color as red.
Is there any way to change background color of richedit control when its disabled?

Should i go for CDialog::OnCtlColor() method to change background color of RichEditCtrl??

Thanks All.
Posted
Updated 19-Mar-12 3:52am
v2
Comments
Resmi Anna 20-Mar-12 2:11am    
This code should work fine. Are you seing the control as disbled without red background?I tried with the same code. I am getting the rich edit control without red background and i can not edit it since disbled.
Bhavin Jagad 20-Mar-12 3:05am    
Thanks,
ya, you are right but i want red background even when the richedit control disabled. do u have any idea?
Resmi Anna 20-Mar-12 4:49am    
you mean u want the red colour only when it is disbled?
Bhavin Jagad 20-Mar-12 5:05am    
yes

1 solution

I am not sure whether OnCtlColor() gets invoked for this rich edit control since created dynamically. So one easiest way to do this is
1. Create a class called CMyRichEditCtrl derived from MFC CRichEditCtrl
2. Handle the WM_ENABLE message within this new class by overriding OnEnable() function
3. Inside the OnEnable() function check its parameter and change the background color accordingly(parameter indicates disable or enable)
4. Make the type of your m_oRichEditCtrl as CMyRichEditCtrl
5. Create the Rich edit control within the OnInitDialog() of your dialog class as below
C++
m_oRichEditCtrl.Create( WS_CHILD|WS_VISIBLE|ES_MULTILINE,
                        CRect0,0,150,150),this,IDC_RICHEDIT );
m_oRichEditCtrl.SetWindowText("Hello");

6. Do not forget to comment CRichEditCtrl::OnEnable(bEnable); inside the OnEnable() function
C++
//CRichEditCtrl::OnEnable(bEnable);

If any clarification needed I can help
 
Share this answer
 
Comments
Bhavin Jagad 20-Mar-12 6:17am    
i do this,

void CMyRichEdit::OnEnable(BOOL bEnable)
{
SetBackgroundColor(FALSE,RGB(255,0,0));
//CRichEditCtrl::OnEnable(bEnable);
}

but not worked.
Resmi Anna 20-Mar-12 6:39am    
when you are disabling the richedit control?have you written the code to disable it like below from your main dialog class?
m_oRichEditCtrl->EnableWindow(false);
Bhavin Jagad 20-Mar-12 6:47am    
hey, its changing color but not making edit box disabled.
because commented code:

//CRichEditCtrl::OnEnable(bEnable);

I need disabled too, so i uncomment it but it is reverting background color.
so its similar as removing WS_DISABLE style from Create() function.
Resmi Anna 20-Mar-12 7:00am    
means you can still type something in the rich edit control and select some text from there after calling EnableWindow(false);????
Bhavin Jagad 20-Mar-12 7:08am    
Oh!! sry.. there was my mistake in DDXing control.
Thanks Resmi..Its working as i need.

I had a second problem also,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900