MFC:如何更改ListCtrl的单个行的颜色/粗体? [英] MFC: How to change color/boldness of inidividual rows of ListCtrl?

查看:487
本文介绍了MFC:如何更改ListCtrl的单个行的颜色/粗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MFC和Visual Studio 2010 C ++.我需要一种使CListCtrl的某些单独行脱颖而出的方法(但是,我不想使用内置的选择功能来突出显示行).可能是行背景的颜色,字体粗细,甚至可能是图像(如果这样的话).

Using MFC and Visual Studio 2010 C++. I need a way to make certain individual rows of a CListCtrl stand out (however I do not want to use the built-in selection capability to highlight the rows). It could be the color of the row background, or font weight, or possibly even an image (if that is performant).

理想情况下,我想知道如何使用库存列表控件执行此操作.但是,如果这不可能,那么请让我知道使用第三方代码的方法.

Ideally I want to know how to do this using the stock list control. However, if this is not possible then let me know of a way using 3rd party code.

更新

这是我最终使用的代码:

Here's the code I ended up using:

void MyList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVCUSTOMDRAW* cd = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

    *pResult = CDRF_DODEFAULT;

    switch( cd->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT:
            *pResult = CDRF_NOTIFYITEMDRAW;
            break;

        case CDDS_ITEMPREPAINT:
            {
                int rowNumber = cd->nmcd.dwItemSpec;
                bool highlightRow = (bool)GetItemData(rowNumber);
                if (highlightRow)
                {
                    COLORREF backgroundColor;
                    backgroundColor = RGB(255, 0, 0);
                    cd->clrTextBk = backgroundColor;
                }
            }
            break;

        default:
            break;
    }
}

就我而言,我没有使用ItemData,所以我在其他地方调用了SetItemData并带有一个布尔值来指示是否应突出显示该行.

In my case, I wasn't using the ItemData for anything, so I called SetItemData elsewhere with a boolean value to indicate whether the row should be highlighted.

推荐答案

此处的关键消息是

The key message here is the NM_CUSTOMDRAW message sent to your CListCtrl (and some other controls). It allows you to tell Windows that you want to custom draw some part of the CListCtrl. The idea is that the message allows you tell which part of the control should be custom drawn. Because custom drawing the whole CListCtrl only to change the text color of a cell would be totally overkill.

请放心,您不必自己处理自定义绘图:该消息允许为控件的某一特定行或单元格设置字体和/或文本/背景色.

Don't worry, you don't have to handle custom draw yourself: The message allows to set the font and/or text/back color for one specific row or cell of the control.

此代码项目文章可能是一个很好的起点.

This codeproject article is probably a good starting point.

这里是一个较短的代码示例,用于设置CListCtrl中特定行的颜色.

Here is a shorter code example to set the color of a specific line in your CListCtrl.

这篇关于MFC:如何更改ListCtrl的单个行的颜色/粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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