Setting a BGColor for my dialog changes my CEdit boxes backgrounds

Hello everyone,

I'm using the "OnCtlColor" function to modify my dialog background color, and this URL to change my background on the CEdit boxes:

The bgcolor for the dialog window overwrites the background for the CEdit boxes. I just want a Red dialog window...with white CEdits boxes.

Can someone point me in the right direction please.  Thanks.
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.

Knut HunstadCommented:
Your dialog's OnCtlColor will catch the WM_CTLCOLOR message instead of CColorEdit. You need to do something like this in the dialog's OnCtlColor:

   if (pWnd == &m_ebCtl) {
     return brWhite; // A white brush that exists throughout the dialogs lifetime

or if you want to keep the Codeproject system, you could change CColorEdit::CtlColor to be public and then all you need is this change in the dialog's OnCtlColor:

   if (pWnd == &m_ebCtl)
      return m_ebCtl.CtlColor(pDC, nCtlColor);

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
VirtueTechAuthor Commented:
Well...see I am doing that in OnCtlColor

However whatever I set for the background of the dialog is the same for the CEdit boxes. If I don't set the dialog BGColor then CEdits boxes backgrounds work perfectly. I can control their colors and everything.

When I color the dialog it seems as if the CEdits boxes are transparent. But the transparent option for the CEdit boxes is set to FALSE.
To change dialog background use OnEraseBkgnd function. OnCtlColor is used only for child controls.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Knut HunstadCommented:
I didn't say so, but my suggestion was based on an actual test, using the Codeproject solution and getting the exact same problem after defining OnCtlColor for the dialog. So I know his works, OnCtlColor is called for the dialog itself as well! Which doesn't mean AlexFM couldn't be right about using OnEraseBkgnd, I haven't tried that :-)

Here is my whole code for OnCtlColor, I guess I should have shown that:

HBRUSH CTest2Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // Not sure if this is really necessary, but it doesn't seem to hurt?
   if (pWnd == &m_ctlCColorEdit) // This is the CColorEdit control asking which color to use
      return m_ctlCColorEdit.CtlColor(pDC, nCtlColor); // note that you need to _return_ the brush, calling SetBkColor doesn't do it!
   return m_brBlue; // This is the brush for the dialog itself

and remember to change CtlColor for CColorEdit to public first.

Try this, or show us your version of OnCtlColor, I'm guessing you are missing some detail here! Or see if OnEraseBkgnd does the trick...
Right, OnCtlColor with nCtlColor = CTLCOLOR_DLG allows to change dialog background, assuming that OnEraseBkgnd is default.
"OnCtlColor is used only for child controls." This was mistake.
Knut HunstadCommented:
"So I know his works"

As a stand alone phrase, to my (non-english) ears this could sound like an accusation of some "he" doing things in not so good ways! Should be: "So I know this works", of course :-)

Better safe than sorry, I became victim of a nasty flame war (not on EE) because of a similar mistake once...
VirtueTechAuthor Commented:

I must have been tired last night when I read your 1st reply. It made perfect sense this morning.

I have made the CColorEdit::CtlColor public and updated the mian dialog like you said.

It worked perfectly. Thanks for your time.
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

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.