Click to See Complete Forum and Search --> : Dialog CEdit Control Color


James A. Johnson
April 17th, 1999, 12:58 PM
I have created a single dialog box with 3 controls. I created the following function and attatched it to WM_CTLCOLOR of the dialog box using the appwizard. However, the dialog is created and my custom colors are not even posted to the box. I can step through the code with debug and it is finding the controls, just not setting the right colors.

I'm new to MFC and this is driving me crazy. You would think there should be an easier way to change a dialog controls background and text color.

TIA
Andy


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

if(nCtlColor == CTLCOLOR_EDIT)
{
int ID = pWnd->GetDlgCtrlID();
if(ID == IDC_EDIT1)
{
pDC->SetTextColor(RGB(23, 29, 230));
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)(RGB(253, 253, 0));
}
if(ID == IDC_EDIT2)
{
pDC->SetTextColor(RGB(23, 29, 230));
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)(RGB(253, 253, 0));
}

if(ID == IDC_STATIC)
{
pDC->SetTextColor(RGB(255,255,254));
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)::GetStockObject(BLACK_BRUSH);
}
}
return hbr;
}


τΏτ
«=»

James A. Johnson
April 17th, 1999, 09:12 PM
Answered my own question and hopefully this will help other newbies.

Using the ClassWizard, create a function for the WM_CTLCOLOR for you dialog class.

Then use some thing like this.

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

if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_STATIC)
{
pDC->SetTextColor(RGB(255,255,255));
pDC->SetBkColor(RGB(0,0,0));
hbr = CreateSolidBrush(RGB(0,0,0));
}

return hbr;
}


Then, use SetDialogItemText(<control name>, <text>)

Example:
SetDialogItem(IDC_EDIT1, "This is some text");

When the control is drawn, it will have the colors you specified in the OnCtlColor().

For other CTL controls, check out the help text for OnCtlColor().

τΏτ
«=»