Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I have one dialog based application, wherein i've different static controls.I want to set different text color for different static controls.How can i use OnCtlColor() for this purpose.If i use CTLCOLOR_EDIT in OnCtlColor(),i can set only one color for all static controls. and how can i fill the static control with color(i mean the background color of static control, not only for the text which will display.for the whole static control.how can i move with these problems?

Please help me in this.

Thanks in Advance.
Posted
v6

for that you need to create some CBrush data as your dialog class membrs
like
MIDL
m_BlueBrush.CreateSolidBrush(RGB(0,0,255));
m_RedBrush.CreateSolidBrush(RGB(255,0,0));
m_GreenBrush.CreateSolidBrush(RGB(0,255,0));


afte that initilaize thses brushes in your OnInitDialog()

MIDL
m_BlueBrush.CreateSolidBrush(RGB(0,0,255));
m_RedBrush.CreateSolidBrush(RGB(255,0,0));
m_GreenBrush.CreateSolidBrush(RGB(0,255,0));



after this in each case of your OnCtlColor() function assign these brush object to return as shown below
C#
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

if( IDC_STATIC == pWnd->GetDlgCtrlID())
{
    pDC->SetTextColor(RGB(255,0,0));
    pDC->SetBkColor(RGB(0,0,255));
    hbr = (HBRUSH) m_GreenBrush;
}
else if( IDC_STATIC1 == pWnd->GetDlgCtrlID())
{
    pDC->SetTextColor(RGB(0,255,0));
    pDC->SetBkColor(RGB(0,0,255));
    hbr = (HBRUSH)m_RedBrush;
}
else if( IDC_STATIC2 == pWnd->GetDlgCtrlID())
{
    pDC->SetTextColor(RGB(0,0,255));
    pDC->SetBkColor(RGB(0,255,0));
    hbr = (HBRUSH)m_RedBrush;
}
return hbr;

take care of colour conflict with text bkgrnd...
 
Share this answer
 
Comments
Jijesh Balakrishnan 6-Jun-11 23:55pm    
this will change only the text background color
suppose you have 3 static controls
IDC_STATIC, IDC_STATIC1, IDC_STATIC2
write your OnCtlColor() like below
C#
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if( IDC_STATIC == pWnd->GetDlgCtrlID())
    {
        pDC->SetTextColor(RGB(255,0,0));
        pDC->SetBkColor(RGB(0,0,255));
    }
    else if( IDC_STATIC1 == pWnd->GetDlgCtrlID())
    {
        pDC->SetTextColor(RGB(0,255,0));
        pDC->SetBkColor(RGB(0,0,255));
    }
    else if( IDC_STATIC2 == pWnd->GetDlgCtrlID())
    {
        pDC->SetTextColor(RGB(0,0,255));
        pDC->SetBkColor(RGB(0,255,0));
    }
    return hbr;


all 3 gets different text colours/ diff bkgrond colours
 
Share this answer
 
Comments
Jijesh Balakrishnan 6-Jun-11 7:20am    
ya its working... thank you....
Jijesh Balakrishnan 6-Jun-11 7:21am    
how can i fill the static control with color
Jijesh Balakrishnan 6-Jun-11 7:30am    
not only the text's background color, entire control color

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900