Solved

BackgroundColor on a CRichEditCtrl

Posted on 1997-08-05
14
238 Views
Last Modified: 2013-11-20
How do I change the backgroundcolor on a CRichEditCtrl??
I've tried to call SetBackgroundColor in OninitialUpdate and in OninitDialog.
0
Comment
Question by:mattiaso
14 Comments
 

Expert Comment

by:JohnWeidner
Comment Utility
You might try handling WM_ERASEBKGRND and paint the entire area using something like FillRect()
0
 
LVL 8

Expert Comment

by:gelbert
Comment Utility
Use CWnd::OnCtlColor() to return brush to paint background og your control. You can check detail information about this function in VC++ help.
0
 

Author Comment

by:mattiaso
Comment Utility
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
 
LVL 4

Expert Comment

by:davmarc
Comment Utility
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
 

Author Comment

by:mattiaso
Comment Utility
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
 
LVL 4

Expert Comment

by:davmarc
Comment Utility
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
 

Author Comment

by:mattiaso
Comment Utility
Now I've tried that, but it will not work.

In OnInitDialog :

m_Rich.SetBackgroundColor(FALSE,RGB(255,255,192));
Invalidate();
UpdateWindow();
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:davmarc
Comment Utility
Last attempt:
m_Rich.SetBackgroundColor(FALSE, RGB(128,128,128));
m_Rich.Invalidate();
m_Rich.UpdateWindow();

Davide Marcato.
0
 

Author Comment

by:mattiaso
Comment Utility
Now I've tried that to but it's not working.
0
 
LVL 3

Expert Comment

by:vinniew
Comment Utility
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
 

Author Comment

by:mattiaso
Comment Utility
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
 

Author Comment

by:mattiaso
Comment Utility
Adjusted points to 70
0
 
LVL 3

Accepted Solution

by:
stefanr earned 70 total points
Comment Utility
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
 

Author Comment

by:mattiaso
Comment Utility
My graphiccard(Cirrus Logic) couldn't show the colour i used  (RGB(255,255,192)) with 256 colours).
But your answer was rigth....
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now