BackgroundColor on a CRichEditCtrl

How do I change the backgroundcolor on a CRichEditCtrl??
I've tried to call SetBackgroundColor in OninitialUpdate and in OninitDialog.
mattiasoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnWeidnerCommented:
You might try handling WM_ERASEBKGRND and paint the entire area using something like FillRect()
0
gelbertCommented:
Use CWnd::OnCtlColor() to return brush to paint background og your control. You can check detail information about this function in VC++ help.
0
mattiasoAuthor Commented:
If i want to change the backgroundcolor of an editcontrol I can
catch the message like this:

// m_Brush is the desired backgroundcolor
if(nCtlColor==CtlColor_Edit)
{
         return m_Brush;
}

But there is no message for a RichEditCtrl.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

davmarcCommented:
mattiaso,

CRichEditCtrl::SetBackgroundColor() should do exactly what you need. Have you tried instructing the rich edit control to redraw itself after the method invocation?
BTW, have you tried changing the back color in other points of your app?

Davide Marcato.
0
mattiasoAuthor Commented:
I've tried Invalidate(), but that will not help.
Can I redraw the control in other ways than with Invalidate and InvalidateRect???
Can you give me an example of that and of how you change the background of an Richedit???
I've succed with editboxes and listboxes in OnCtlColor.
0
davmarcCommented:
Did you call UpdateWindow() after Invalidate()/InvalidateRect()?
It sends the WM_PAINT message to the window (the rich edit control in this case) immediately.

Davide Marcato.
0
mattiasoAuthor Commented:
Now I've tried that, but it will not work.

In OnInitDialog :

m_Rich.SetBackgroundColor(FALSE,RGB(255,255,192));
Invalidate();
UpdateWindow();
0
davmarcCommented:
Last attempt:
m_Rich.SetBackgroundColor(FALSE, RGB(128,128,128));
m_Rich.Invalidate();
m_Rich.UpdateWindow();

Davide Marcato.
0
mattiasoAuthor Commented:
Now I've tried that to but it's not working.
0
vinniewCommented:
This problem is documented somewhere in MSDN...

Your trying to do the same thing as what VB does, right?  You're trying to just change the background color of a single control?

If that's what you're doing,  search MSDN for changing the color of an Edit control and you should find the code snippet.  I remember because the author said that it was a pain in the ass to do it in C/C++.
0
mattiasoAuthor Commented:
I know how to change the backgroundcolor of an EditCtrl.
But it looks like changing the backgroundcolor of a RichEditCtrl is different.
I will check i MSDN.
0
mattiasoAuthor Commented:
Adjusted points to 70
0
stefanrCommented:
Extracts from my test app (a standard single document CFormView AppWizard created) that WORKS:

...
/////////////////////////////////////////////////////////////////////////////
// CRichEditCtrlApp initialization

BOOL CRichEditCtrlApp::InitInstance()
{
      AfxEnableControlContainer();

   if (!::AfxInitRichEdit())
   {
      ::AfxMessageBox("AfxInitRichEdit failed");
      return FALSE;
   }

      // Standard initialization
      // If you are not using these features and wish to reduce the size
      //  of your final executable, you should remove from the following
      //  the specific initialization routines you do not need.

#ifdef _AFXDLL
      Enable3dControls();                  // Call this when using MFC in a shared DLL
#else
      Enable3dControlsStatic();      // Call this when linking to MFC statically
#endif

      // Change the registry key under which our settings are stored.
      // You should modify this string to be something appropriate
      // such as the name of your company or organization.
      SetRegistryKey(_T("Local AppWizard-Generated Applications"));

      LoadStdProfileSettings();  // Load standard INI file options (including MRU)

      // Register the application's document templates.  Document templates
      //  serve as the connection between documents, frame windows and views.

      CSingleDocTemplate* pDocTemplate;
      pDocTemplate = new CSingleDocTemplate(
            IDR_MAINFRAME,
            RUNTIME_CLASS(CRichEditCtrlDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
            RUNTIME_CLASS(CRichEditCtrlView));
      AddDocTemplate(pDocTemplate);

      // Parse command line for standard shell commands, DDE, file open
      CCommandLineInfo cmdInfo;
      ParseCommandLine(cmdInfo);

      // Dispatch commands specified on the command line
      if (!ProcessShellCommand(cmdInfo))
            return FALSE;

      // The one and only window has been initialized, so show and update it.
      m_pMainWnd->ShowWindow(SW_SHOW);
      m_pMainWnd->UpdateWindow();

      return TRUE;
}

...

class CRichEditCtrlView : public CFormView
{
protected: // create from serialization only
      CRichEditCtrlView();
      DECLARE_DYNCREATE(CRichEditCtrlView)

public:
      //{{AFX_DATA(CRichEditCtrlView)
      enum{ IDD = IDD_RICHEDITCTRL_FORM };
            // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA
   CRichEditCtrl m_richeditctrl;
...
};

...

void CRichEditCtrlView::OnInitialUpdate()
{
      CFormView::OnInitialUpdate();

   COLORREF cr = RGB(128, 128, 128);
   m_richeditctrl.SetBackgroundColor(FALSE, cr);
}

I also tested to create a plain MFC ClassWizard CView, CPlainView. Then I replaced
      pDocTemplate = new CSingleDocTemplate(
            IDR_MAINFRAME,
            RUNTIME_CLASS(CRichEditCtrlDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
            RUNTIME_CLASS(CRichEditCtrlView));
with
      pDocTemplate = new CSingleDocTemplate(
            IDR_MAINFRAME,
            RUNTIME_CLASS(CRichEditCtrlDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
            RUNTIME_CLASS(CPlainView));
in CRichEditCtrlApp::InitInstance()

Then I created a CRichEditCtrl member variable of CPlainView and added the OnInitUpdate() function:

void CPlainView::OnInitialUpdate()
{
      CView::OnInitialUpdate();

   if (m_richeditctrl.Create(WS_CHILD| WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE, CRect(0, 0, 100, 100), this, 0))
   {
      COLORREF cr = RGB(128, 128, 128);
      m_richeditctrl.SetBackgroundColor(FALSE, cr);
   }
   else
      ::AfxMessageBox("Failed to create CRichEditCtrl");
}

/Stefan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mattiasoAuthor Commented:
My graphiccard(Cirrus Logic) couldn't show the colour i used  (RGB(255,255,192)) with 256 colours).
But your answer was rigth....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.