Link to home
Start Free TrialLog in
Avatar of worker018
worker018

asked on

changing label text color

It's very smple question but I can't find solution.

I wanted to use a differnt style label, so changed
the font and font size, but couldn't change text
colot and background color.

It's there any suggestion?
Avatar of chensu
chensu
Flag of Canada image

What is a label?
Avatar of mikeblas
mikeblas

You can't set this in the dialog editor.

At runtime, you need to handle WM_CTLCOLOR for the control and set its color that way.  That means you need to give the control a real ID, and not IDC_STATIC (which is -1).

..B ekiM
Here's a code framgnet:

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

   if (pWnd->GetDlgCtrlID() == IDC_FOO)
   {
      pDC->SetTextColor(RGB(255, 0, 0));
   }

   return hbr;
}


Obviously, you'll use whatever ID you want in place of IDC_FOO. Also, you can pick any color; I used RGB(255, 0, 0) which is red.

..B ekiM
Is worker018 talking about a static control?
> Is worker018 talking about a static control?
 
I would say so.  Some Windows environments call 'em labels.

..B ekiM
Avatar of worker018

ASKER


Oh sorry, I mean static control.
but, I can't understand how can I use your code

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

      if (pWnd->GetDlgCtrlID() == IDC_FOO){
            pDC->SetTextColor(RGB(255,0,0));
      }
      return hbr;
}

===========================================

I inserted this code in my dialog, and wrote the header file.
and replace IDC_FOO to IDC_POSITION (my control name)
what can I do next?

Oh sorry, I mean static control.
but, I can't understand how can I use your code

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

      if (pWnd->GetDlgCtrlID() == IDC_FOO){
            pDC->SetTextColor(RGB(255,0,0));
      }
      return hbr;
}

===========================================

I inserted this code in my dialog, and wrote the header file.
and replace IDC_FOO to IDC_POSITION (my control name)
what can I do next?
Avatar of Zoppo
Hi worker018,

take a look at http://www.codeguru.com/staticctrl/label_static.shtml for a extended CStatic derived class code and sample ...

hope that helps,

ZOPPO
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

 Thanks for your answer.