Solved

BackgroundColor on a CRichEditCtrl

Posted on 1997-08-05
14
240 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 75
WinWaitActive parameters 12 31
viewing source code from eclipse 13 88
JQuery serialize and unserialize 8 51
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 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

15 Experts available now in Live!

Get 1:1 Help Now