Solved

BackgroundColor on a CRichEditCtrl

Posted on 1997-08-05
14
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 

Expert Comment

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

Expert Comment

by:gelbert
ID: 1303065
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
ID: 1303066
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:davmarc
ID: 1303067
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
ID: 1303068
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
ID: 1303069
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
ID: 1303070
Now I've tried that, but it will not work.

In OnInitDialog :

m_Rich.SetBackgroundColor(FALSE,RGB(255,255,192));
Invalidate();
UpdateWindow();
0
 
LVL 4

Expert Comment

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

Davide Marcato.
0
 

Author Comment

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

Expert Comment

by:vinniew
ID: 1303073
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
ID: 1303074
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
ID: 1303075
Adjusted points to 70
0
 
LVL 3

Accepted Solution

by:
stefanr earned 70 total points
ID: 1303076
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
ID: 1303077
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Shell script to login to remote m/c and execute commands 10 80
pre4 challenge 19 114
Trying to run powershell  in a batch file. How do I do this? 8 122
Problem to Office 1 62
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…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

740 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