Saturday, February 21, 2009

How do I change the colour or background of a static text item on a dialog?

By handling the WM_CTLCOLOR message. Here's a sample in MFC-style code that illustrates the technique.

HBRUSH CTestbed2Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;

switch (nCtlColor)
{
case CTLCOLOR_STATIC:
pDC->SetTextColor (RGB(128,0,0)); // red text.
hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
break;

default:
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
break;
}
return hbr;
}

If you wanted to change the background colour of your statics, you could do it by adding a call to SetBkColor. For instance

pDC->SetBkColor (RGB(0,0,0));

would give you a black background for your static text.

Incidentally, you can combine this technique with This Tip to neaten up dialogs with bitmap backgrounds. Just add the line

pDC->SetBkMode (TRANSPARENT);

to the CTLCOLOR_STATIC handler, and your bitmap will show through all the static item backgrounds.

Author : Bob Moore

0 nhận xét:

Post a Comment