Wednesday, February 22, 2006

Change the Child Control Color in MFC

There is no SetTextColor function available for controls in MFC. If you want change the color of the Child control Pleasefollow these steps.It is aplicable to any CWnd derived Windows such asCFrameWnd,CDialog, CView,CComboBox etc.Using Class Wizard create this function.I am assuming we have CDialog Derived class

HBRUSH CDialog::OnCtlColor(CDC* pDC, CWnd* pWnd,UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
If you have a static text control IDC_STATIC_NAME.You can change the color by modifying the code as shown below.
HBRUSH CDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CMyDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_STATIC)
{
if(pWnd->GetDlgCtrlID() == IDC_STATIC_NAME)
{
pDC->SetTextColor(RGB(0,0,255)); //Blue Color
pDC->SetBkColor(RGB(255,0,0)); //Red Color
}
}
return hbr;
}
Above code will change the text color as Blue and back color as black. Same thing you can do for edit control using CTLCOLOR_EDIT

1 Comments:

At 4:59 AM, Blogger Jenna said...

Just "Rolling" through. Nice blog. Jennas Blog

 

Post a Comment

<< Home