Changing Background Color in a CStatic Object

Here’s a classic example of a seemingly easy problem actually being harder to implement.  I wanted a dialog with white static controls in MFC.  The resulting code is simple but divining the answer was no easy feat.  Ultimately it’s more a failure of documentation that it is a engineering challenge.  More details in my commented code which follows:

Before and After
Before and After

HBRUSH TReaderDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
HBRUSH hbr = __super::OnCtlColor(pDC, pWnd, nCtlColor);

// For static controls, draw black text on a white
// background (instead of the default Windows
// dialog background color)
if (nCtlColor == CTLCOLOR_STATIC)
// Set the background color to white
// This is a red herring. You will only affect the text
// drawn and the static control is certainly larger than
// the text drawn so you will have the original color
// "leaking" out. (see pic)
//pDC->SetBkColor(RGB(255, 255, 255));

// Set the background mode for the drawn text to
// transparent
// If you use SetBkColor with SetBkMode you will further
// compound the confusion/frustrtion. Using
// SetBkMode(TRANSPARENT) after SetBkColor cancels
// out SetBkColor.

// The real solution is to create a brush with a solid
// white background and return that from this function.
// Also set the background mode to transparent.

static CBrush mBrush(RGB(255, 255, 255)); // Solid white brush.

hbr = mBrush;

// Return a different brush if the default is
// not desired
return hbr;