Background color for controls in MFC C++ is unable to set

710 views Asked by At

I am trying to set the text color using SetTextColor and background color using SetBkColor in on_wm_ctlcoloredit function. But whenever I try to set only text color, the output actually paints the background as black where the text is not present and white where text is present. I am expecting it to be of the light grey colour for windows forms. How can I get that default background color of windows forms?

2

There are 2 answers

0
Flaviu_ On BEST ANSWER

Are you referring to

GetSysColor(COLOR_STATIC)

?

See more here: GetSysColor

0
xMRi On

It is not sufficient to set the text color and the background color of the DC. You also Need to return a brush for the correct Background Color when you handle any WM_CTLCOLOR message. The easiest way is always to call the base class implementation.

By default the DC is prepared with the default values (text color black, background white). And because you need to determine and return a brush there is no default.

So to get the brush and settings of the parent window, you can send WM_CTLCOLORDLG to your parent window and you should get the correct text color, backgroundcolor and the brush as a return value. You can use CBrush::FromHandle to use it as a return value for yout ON_WM_CTLCOLOR handler.