更改MFC中CListCtrl中项目中特定字符的颜色 [英] Changing color of a specific character in an item in CListCtrl in MFC

查看:80
本文介绍了更改MFC中CListCtrl中项目中特定字符的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CListCtrl,我需要从列表中每个单元格的文本中更改A SPECIFIC字符/字符集(我通过比较选择)的颜色。



我知道当我找到字符/字符集时(通过使用''strstr''命令)如何更改单元格整个文本的颜色,但我找不到一个显示示例如何更改字符/字符集。



以下是我的代码示例:



I have a CListCtrl and I need to change the color of A SPECIFIC character/set of characters (which I choose by comparison) from the text of every cell in the list.

I know how to change the color of the entire text of the cell when I find the character/set of characters (by using ''strstr'' command), but I can''t find an example which shows how to change ONLY the character/set of characters.

Here is a sample of my code:

void Agenda::OnCustomdrawMyList( NMHDR* pNMHDR, LRESULT* pResult )
    {
        NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)pNMHDR;

        *pResult = CDRF_DODEFAULT;

        if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
        {
            *pResult = CDRF_NOTIFYITEMDRAW;
            return;
        }else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
        {
            *pResult = CDRF_NOTIFYSUBITEMDRAW;
            return;
        }else if ( (CDDS_SUBITEM | CDDS_ITEMPREPAINT) == pLVCD->nmcd.dwDrawStage )
        {

            // So right now I am in the stage where a SUBITEM is PREPAINTED

            int nItem = pLVCD->nmcd.dwItemSpec;
            int nSubItem = pLVCD->iSubItem;

            char a[100];
            listControl.GetItemText(nItem,nSubItem,a,100);

            COLORREF textColorFound, textColorDefault;
            textColorDefault = RGB(0,0,0);
            pLVCD->clrText = textColorDefault;

            char* startingFrom;

            if( (startingFrom = strstr(a,filterText)) != NULL ) {
                    // Could I set a pointer here or something like that so
                    //   the coloring could start only from 'startingFrom'
                    //   and stop at 'strlen(filterText)' characters?

                textColorFound = RGB(205,92,92);
                pLVCD->clrText = textColorFound;
            }
            *pResult = CDRF_DODEFAULT;
        }
    }





* listControl是我的CListCtrl的变量

*其他的东西是不言自明的



* listControl is the variable for my CListCtrl
* the other things are pretty self-explanatory

推荐答案

具有这种功能的控件是丰富的编辑控件和浏览器控件等控件。

所以你需要弄清楚如何将这些控件放在列表控件的单元格中。



这里有一些关于代码项目的文章 -

在ListView中嵌入控件 [ ^ ]

具有HyperLink功能的简单CListCtrl [ ^ ]

TreeListViewEx树,列表和拖放 [ ^ ]



另一种选择是将所需文本创建为位图,然后显示位图列表控件。

这是一篇关于使用位图和列表控件的文章 -

使用虚拟CListCtrl显示文本和位图 [ ^ ]
The controls capable of such capability are controls like rich edit control and browser control.
So you need to figure out how to place such controls in a cell of a list control.

Here are some such articles on code project -
Embedding Controls in a ListView[^]
Simple CListCtrl with HyperLink Function[^]
TreeListViewEx Tree, List, and Drag and Drop[^]

Another option would be to create the required text as a bitmap and then show the bitmap in the list control.
Here is an article on using bitmaps with list controls -
Using a Virtual CListCtrl to Display Text and Bitmaps[^]


除此之外解决方案1中的选项,您可以在自定义绘制处理程序中执行此操作,方法是绘制符合条件的单元格文本并返回g CDRF_SKIPDEFAULT 或使用所有者绘图自己绘制所有单元格(实现 DrawItem())。



但这两种方法都需要代码以类似于Windows的方式绘制单元格内容。当支持所有可能的选项时,生成的代码会变得非常大:着色(选定,热,非活动,背景),文本对齐,图像(复选框,图标)和样式(经典或主题)。



对于简单的实现,请执行以下步骤:



  • 获取单元格
  • 通过使用背景颜色调用FillSolidRect()来清除单元格
  • 从左右减去6像素的单元格文本间距
  • 使用格式标记绘制文本 DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER
Besides the options from Solution 1, you can do it inside your custom draw handler by drawing the text of cells that match the criteria there and returning CDRF_SKIPDEFAULT or use owner drawing to draw all cells yourself (implementing DrawItem()).

But both methods require code to draw the cell content in a similar way like Windows does. The resulting code can become quite large when all possible options should be supported: Coloring (selected, hot, inactive, background), text alignment, images (check boxes, icons) and style (classic or themed).

For a simple implementation perform these steps:

  • Get the cell rect
  • Clear cell by calling FillSolidRect() with background color
  • Subtract cell text spacing of 6 pixels from left and right
  • Draw the text using format flags DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER


这篇关于更改MFC中CListCtrl中项目中特定字符的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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