VC++中的MFC控件ListBox颜色操作攻略,点击按钮对话框输出颜色不同的文本

本项目的目的是对mfc控件ListBox中输出的文本进行颜色进行颜色操作,即使控件对话框输出颜色不一样的文本。

  1. 新建一个mfc基于对话框的exe文件

  1. 新建一个基于CListBox的类,并命名为CColorListBox。新建这个类的原因是因为系统自带的CListBox类中的AddString函数中只有一个对文本进行操作的参数,并不能改变文本颜色,所以需要在CColorListBox中重写AddString函数,增加改变颜色的参数,使其同时也能对文本颜色进行操作。

  1. 在新建的CColorListBox类中加入以下三个函数,并记得在头文件中申明

int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF itemColor)
{
    // Add the string to the list box
    int nIndex = CListBox::AddString(lpszItem);
    // save color data
    if (nIndex >= 0)
    SetItemData(nIndex, itemColor);
    return nIndex;
}

void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
/// Losing focus ?
    if (lpDrawItemStruct->itemID == -1)
    {
        DrawFocusRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem);
        return;
    }
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

    COLORREF clrOld;
    CString sText;

    // get color info from item data
    COLORREF clrNew = (COLORREF)(lpDrawItemStruct->itemData);

    // item selected ?
    if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
         (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
    {
        CBrush brush(::GetSysColor(COLOR_HIGHLIGHT));
        pDC->FillRect(&lpDrawItemStruct->rcItem, &brush);
    }

    // item deselected ?
    if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&    
        (lpDrawItemStruct->itemAction & ODA_SELECT))
    {
        CBrush brush(::GetSysColor(COLOR_WINDOW));
        pDC->FillRect(&lpDrawItemStruct->rcItem, &brush);
    }         

    // item has focus ?
    if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && 
        (lpDrawItemStruct->itemState & ODS_FOCUS))
    {
        pDC->DrawFocusRect(&lpDrawItemStruct->rcItem); 
    }

    // lost focus ?
    if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&    
        !(lpDrawItemStruct->itemState & ODS_FOCUS))
    {
        pDC->DrawFocusRect(&lpDrawItemStruct->rcItem); 
    }

    // set the background mode to TRANSPARENT
    int nBkMode = pDC->SetBkMode(TRANSPARENT);

    if (lpDrawItemStruct->itemState & ODS_SELECTED)
        clrOld = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
    else 
    if (lpDrawItemStruct->itemState & ODS_DISABLED)
        clrOld = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
    else
        clrOld = pDC->SetTextColor(clrNew);

    // get item text
    GetText(lpDrawItemStruct->itemID, sText);
    CRect rect = lpDrawItemStruct->rcItem;

    // text format
    UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
    if (GetStyle() & LBS_USETABSTOPS)
        nFormat |= DT_EXPANDTABS;

    // draw the text
    pDC->DrawText(sText, -1, &rect, nFormat);

    // restore old values
    pDC->SetTextColor(clrOld); 
    pDC->SetBkMode(nBkMode);
}

void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
    lpMeasureItemStruct->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
}
  1. 对对话框进行布局后如下

  1. 在对话框头文件如下位置加入CColorListBoxm_MsgShow;头文件中加入#include "ColorListBox.h"

对话框源文件如下位置加入DDX_Control(pDX, IDC_TEST_LIST,m_MsgShow);

  1. 在对话框中加入系统自带的消息响应函数HBRUSH CListBoxTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor),可以通过与ID号的绑定来改变静态文本的颜色,如静态文本提示内容Tips

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

    if (pWnd->GetDlgCtrlID() == IDC_TIPS)
    {
        pDC->SetTextColor(RGB(160,32,240));
    }
//     if (pWnd->GetDlgCtrlID() == IDOK)
//     {
//         pDC->SetTextColor(RGB(160,32,240));
//     }
//     if (pWnd->GetDlgCtrlID() == IDCANCEL)
//     {
//         pDC->SetTextColor(RGB(160,32,240));
//     }
    return hbr;
}
  1. 确认按钮和退出按钮的函数如下,num定义在全局并初始化为0

void CListBoxTestDlg::OnOK() 
{
    // TODO: Add extra validation here
//     if (IDOK == IDYES)
//     {
//         m_MsgShow.AddString("这是测试消息1",RGB(255,0,0));
//     }
    if (num == 6)
    {
        num=0; 
    }
    if (num == 0)
    {
        m_MsgShow.AddString("缓缓掉落的落叶像思恋",RGB(255,0,0));
    } 
    else if(num == 1)
    {
        m_MsgShow.AddString("我点燃烛火温暖岁末的秋天",RGB(0,255,0));
    }
    else if(num == 2)
    {
        m_MsgShow.AddString("极光掠过天边",RGB(0,0,255));
    }
    else if(num == 3)
    {
        m_MsgShow.AddString("北风掠过想你的容颜",RGB(255,0,255));
    }
    else if(num == 4)
    {
        m_MsgShow.AddString("我把爱烧成了落叶",RGB(160,32,240));
    }
    else 
    {
        m_MsgShow.AddString("却换不回熟悉的那一张脸",RGB(0,255,255));
    }
    num++;
//     m_MsgShow.AddString("这是测试消息1",RGB(255,0,0));
//     m_MsgShow.AddString("这是测试消息2",RGB(0,255,0));
//     m_MsgShow.AddString("这是测试消息3",RGB(0,0,255));
//     m_MsgShow.AddString("这是测试消息4",RGB(255,0,255));
    
    /*CDialog::OnOK();*/
}
void CListBoxTestDlg::OnCancel() 
{
    // TODO: Add extra cleanup here
    if (AfxMessageBox("是否确定退出?",MB_YESNO)==IDYES)
    {
        CDialog::OnOK();
    }
    
}
  1. 运行程序,点一次确定输出一句文本,结果如下:

如果本文对你有所帮助,还希望能点一个赞,谢谢。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值