为不同的静态控件设置不同的颜色 [英] Set Different Colors For Different Static Controls

查看:55
本文介绍了为不同的静态控件设置不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个基于对话框的应用程序,其中我有不同的静态控件.我想为不同的静态控件设置不同的文本颜色.如何为此目的使用OnCtlColor().如果我在OnCtlColor()中使用CTLCOLOR_EDIT,我只能为所有静态控件设置一种颜色.以及如何用颜色填充静态控件(我的意思是静态控件的背景色,不仅是要显示的文本.对于整个静态控件.如何解决这些问题?

请帮助我.

在此先感谢.

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.

推荐答案

为此,您需要创建一些CBrush数据作为对话框类成员
就像
for that you need to create some CBrush data as your dialog class membrs
like
m_BlueBrush.CreateSolidBrush(RGB(0,0,255));
m_RedBrush.CreateSolidBrush(RGB(255,0,0));
m_GreenBrush.CreateSolidBrush(RGB(0,255,0));



之后在OnInitDialog()中初始化这些画笔



afte that initilaize thses brushes in your OnInitDialog()

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




之后,在每种情况下,对于OnCtlColor()函数,请分配这些画笔对象以返回,如下所示




after this in each case of your OnCtlColor() function assign these brush object to return as shown below

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;


注意与文本bkgrnd的颜色冲突...


take care of colour conflict with text bkgrnd...


假设您有3个静态控件
IDC_STATIC,IDC_STATIC1,IDC_STATIC2
如下所示编写您的OnCtlColor()
suppose you have 3 static controls
IDC_STATIC, IDC_STATIC1, IDC_STATIC2
write your OnCtlColor() like below
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;



全部3种文字颜色/差异bkgrond颜色



all 3 gets different text colours/ diff bkgrond colours


这篇关于为不同的静态控件设置不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆