Solved

BackgroundColor on a CRichEditCtrl

Posted on 1997-08-05
14
241 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
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Error on moodle after upgrade 3 131
Generic progress indicator 6 117
has22 challenge 11 96
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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