how to set color for static control text in dialog?

27 08 2008


Colors convey meanings too. For instance, if something is written in red – that means something to be cautious. If its in green, its treated as safe. Well, usually in window dialogs, the static control texts are black in color. Is it possible to color them to convey more meaning?


Yes! You have to handle WM_CTLCOLOR message. For each control, this message will be triggered and you’re free to do modification on your control. Have a look at the code snippet in MFC framework.

// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
    ...
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

HBRUSH CDlgDlg::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
{
    // Call base class version at first. Or else it will override your changes.
    HBRUSH hbr = CDialog::OnCtlColor( pDC, pWnd, nCtlColor );

    // Check whether which static label its.
    if( pWnd->GetDlgCtrlID() == IDC_STATIC_OK )
    {
        // Set color as red.
        pDC->SetTextColor( RGB( 255, 0, 0 ));
        pDC->SetBkMode( TRANSPARENT );
    }

    return hbr;
}


Always call CDialog::OnCtlColor() at first. Or else it will override your modifications.


Targeted Audience – Intermediate.

Advertisements

Actions

Information

4 responses

10 09 2008
mehranhu

Hi, It really works, ive done it already, But im searching a messive way of the same thing to be done in simple WIN32, not MFC. Can u help me please
mehrangull@hotmail.com

10 09 2008
Jijo.Raj

If you seek plain win32, you could handle WM_CTLCOLORSTATIC and can change the static text color.

Have a look at this link – http://www.winprog.org/tutorial/dlgfaq.html. It contains code samples. Hope it helps.

Regards,
Jijo.

21 10 2008
pawan

Is it applicable only to static control or i can use it for other

21 10 2008
Jijo.Raj

You could change the color of other controls as well. Controls such as – Button, Dialog, edit, Listbox, scrollbars etc. For instance, see how to change the color of push button here – http://www.daniweb.com/forums/thread26182.html

Regards,
Jijo.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: