MFC:如何更改ListCtrl各行的颜色/粗体?
使用 MFC 和 Visual Studio 2010 C++。我需要一种方法来使 CListCtrl 的某些单独行脱颖而出(但是我不想使用内置选择功能来突出显示这些行)。它可以是行背景的颜色,或者字体粗细,甚至可能是图像(如果性能良好的话)。
理想情况下,我想知道如何使用库存列表控件来执行此操作。但是,如果这是不可能的,请告诉我使用第 3 方代码的方法。
更新
这是我最终使用的代码:
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,并使用布尔值来指示是否应突出显示该行。
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.
UPDATE
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;
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的关键信息是 NM_CUSTOMDRAW 消息发送到您的 CListCtrl(以及其他一些控件)。它允许您告诉 Windows 您想要自定义绘制 CListCtrl 的某些部分。这个想法是,该消息允许您告诉应该自定义绘制控件的哪一部分。因为自定义绘制整个 CListCtrl 只是为了更改单元格的文本颜色,这完全是多余的。
别担心,您不必自己处理自定义绘制:该消息允许为控件的一个特定行或单元格设置字体和/或文本/背景颜色。
这篇代码项目文章可能是一个很好的起点。
这是一个较短的代码示例[已存档] 设置 CListCtrl 中特定行的颜色。
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.
Here is a shorter code example[archived] to set the color of a specific line in your CListCtrl.
您可以使用以下代码来更改整个列表背景颜色,但我不确定是否支持更改每行颜色的功能。以下是代码:
我希望它有所帮助。
You can use the following code to alter the whole list backgroud colour but I am not sure there is supported functionality to change the colour per row. Following is the code:
I hope it helps.
这是我对我的程序所做的,
如果我想要突出显示的行是 cin 或 cout 或任何其他行,只需将此代码放在该行最后一部分的上方
更改背景和文本的颜色代码
,即 0x0F 让您在将其更改为所需颜色后 ,只需插入另一个位于您想要突出显示的行下方,即;
的颜色表及其代码示例;
这是黑色背景和蓝色文本
Here is what i did to my program,
if the row i want highlighted is a cin or a cout or any other just put this code above the that row
the last part i.e, 0x0F lets u change the colour code of the background and text
after changing that to your desired colour, just insert another below the row you want highlighted, i.e;
here is the table of colours and their codes
EXAMPLE for a black background and blue text;