MFC: How to change the color / boldness of individual ListCtrl lines?

Using MFC and Visual Studio 2010 C ++. I need a way to highlight individual CListCtrl lines (however, I don't want to use the built-in highlight function to select lines). This may be the background color of the line or the weight of the font, or perhaps even an image (if so).

Ideally, I want to know how to do this using the stock list control. However, if this is not possible, tell me how to use third-party code.

UPDATE

Here 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 did not use ItemData for anything, so I called SetItemData elsewhere as a boolean to indicate whether to highlight the row.

+6
source share
3 answers

The key message here is the NM_CUSTOMDRAW message sent to your CListCtrl (and some other controls). This allows you to specify the Windows that you want to configure, draw the CListCtrl part. The idea is that the message allows you to specify which part of the control should be drawn by the user. Since the custom drawing of the entire CListCtrl just to change the color of the text of the cell will be completely full.

Do not worry, you do not need to process your own drawing yourself: the message allows you to set the font and / or text / background color for one specific row or cell of the control.

This code article is probably a good starting point.

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

+5
source

You can use the following code to change the entire color of the backgroud of the list, but I'm not sure if there is support for the functionality to change the color in the line. Below is the code:

 YourControl.SetBkColor(RGB(212,208,200)); 

Hope this helps.

-2
source

Here is what I did with my program, if the line I want to highlight is cin or cout or whatever, just put this code above that line

 SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x0F); 

the last part ie, 0x0F allows you to change the color code of the background and text

after you change it to the desired color, just insert another one below the line you want to highlight, for example,

  SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x0C); 

here is a table of colors and their codes

  0 = Black 8 = Gray 1 = Blue 9 = Light Blue 2 = Green A = Light Green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White 

EXAMPLE for black background and blue text;

 SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x01); 
-3
source

Source: https://habr.com/ru/post/905729/


All Articles