• 让CtrlList的某一行自定义颜色


    参考文章:https://blog.csdn.net/weixin_43913330/article/details/90287250

    定义类继承CtrlList

    头文件中添加:

    CMap<DWORD, DWORD&, COLORREF, COLORREF&> MapItemColor;
    CMap<DWORD, DWORD&, COLORREF, COLORREF&> MapFontColor;

    用于存储自定义设置的列表项颜色、字体等


    需要添加专门用于自绘控件的事件响应:

    //sonne 2020-08-18
    void 类名::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
    {
        int col_num = get_col_num();
        
        NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
        // Take the default processing unless we set this to something else below.
        *pResult = CDRF_DODEFAULT;
        // First thing - check the draw stage. If it's the control's prepaint
        // stage, then tell Windows we want messages for every item.
        if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
        {
            *pResult = CDRF_NOTIFYITEMDRAW;
        }
        else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
        {
            // This is the notification message for an item. We'll request
            // notifications before each subitem's prepaint stage.
            *pResult = CDRF_NOTIFYSUBITEMDRAW;
        }
        else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage) 
        {                                                                         
            // This is the prepaint stage for a subitem. Here's where we set the
            // item's text and background colors. Our return value will tell 
            // Windows to draw the subitem itself, but it will use the new colors
            // we set here.         
            COLORREF ItemColor;
            DWORD dw = (pLVCD->iSubItem + col_num * pLVCD->nmcd.dwItemSpec);
    
            if (MapItemColor.Lookup(dw, ItemColor))    
            {
                pLVCD->clrTextBk = ItemColor;
                //SetFont(m_Font, false);
            }
            else
            {
                //如果不是选择的“行”和“列”就设置成系统默认的那种颜色
                pLVCD->clrTextBk = 16777215;
            }
            if (MapFontColor.Lookup(dw, ItemColor))
            {
                pLVCD->clrText = ItemColor;
                //SetFont(m_Font, false);
            }
            else
            {
                pLVCD->clrText = 0;
            }
            /*
            if (pLVCD->iSubItem == m_iRow)
            {
                if (m_FontFlag)
                {
                    m_FontFlag = FALSE;
                    SetFont(m_Font, false);
                }            
            }
            */
            *pResult = CDRF_DODEFAULT;
        }
    }

    需要将之注册到消息映射里:

    BEGIN_MESSAGE_MAP(COnlineDeviceListCtrl, CListCtrl)
      ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
    END_MESSAGE_MAP()

    对表格项进行自定义的一些函数,调用相关函数,自动会响应OnNMCustomdraw从而达到自绘的效果:

    //sonne 2020-08-18
    void 类名::SetCellColor(int iRow, int iCol, COLORREF color)
    {    
        int col_num = get_col_num();
        DWORD iItem = iRow * col_num + iCol;
        //设置某行的颜色
        MapItemColor.SetAt(iItem, color);
        //重新染色
        this->RedrawItems(iRow, iRow);
        //设置焦点
        this->SetFocus();    
        UpdateWindow();
    }
    
    
    /*
     *  sonne 
     *  2020-08-18
     *  为列表某一行自定义颜色
     */
    void 类名::set_ctrllist_row_color(int row, COLORREF color)
    {
        int col_num = get_col_num();
        for (int i=0; i<col_num; i++)
        {
            SetCellColor(row, i, color);
        }
    }
    
    
    /*
     *  sonne 
     *  2020-08-18
     *  获取表格的列数
     */
    int 类名::get_col_num()
    {
        CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
        int col_num = 0;
        if (pHeaderCtrl)
        {
            col_num = pHeaderCtrl->GetItemCount();
        }
        return col_num;
    }
    
    
    //sonne 2020-08-18
    void 类名::SetFontColor(int iRow, int iCol, COLORREF color)
    {
        int col_num = get_col_num();
        DWORD iItem = iRow * col_num + iCol;
        //设置单元格字体的颜色
        MapFontColor.SetAt(iItem, color);
        //重新染色
        this->RedrawItems(iRow, iRow);
        //设置焦点
        this->SetFocus();    
        UpdateWindow();
    }
  • 相关阅读:
    一周以来工作总结关于位图索引
    再学学表的分区
    PostgreSQL学习笔记
    通过vc助手设置快捷注释
    c语言中unsigned类型和普通类型间的转换
    LVS环境搭建入门
    java学习路线
    linux下删除当前文件夹中按时间排序的前N个文件夹
    RHEL下安装jdk和tomcat
    TDD 强迫你 Program to Interface
  • 原文地址:https://www.cnblogs.com/rixiang/p/13523814.html
Copyright © 2020-2023  润新知