CListCtrl 设置行高、选中行颜色、间隔行颜色(完整代码,亲测有效)

目录

1.效果图

2.CMyListCtrlEx类(继承自CListCtrl)

2.1 CMyListCtrlEx.h 

2.2 CMyListCtrlEx.cpp

3.使用CMyListCtrlEx创建表格 


1.效果图

2.CMyListCtrlEx类(继承自CListCtrl)

2.1 CMyListCtrlEx.h 

#pragma once

// CMyListCtrlEx 窗体视图

class CMyListCtrlEx : public CListCtrl
{

private:
    unsigned int m_uRowHeight;
    CRect m_listRect;

public:
	CMyListCtrlEx();
	virtual ~CMyListCtrlEx();    

protected:
	virtual void DoDataExchange(CDataExchange* pDX);

	DECLARE_MESSAGE_MAP()
public:
    void SetRowHeigt(int nHeight);

    afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    
};


2.2 CMyListCtrlEx.cpp

#include "stdafx.h"
#include "CMyListCtrlEx.h"


CMyListCtrlEx::CMyListCtrlEx()
{
    m_uRowHeight = 60;
}

CMyListCtrlEx::~CMyListCtrlEx()
{
}

void CMyListCtrlEx::DoDataExchange(CDataExchange* pDX)
{
    CListCtrl::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMyListCtrlEx, CListCtrl)
    ON_WM_MEASUREITEM_REFLECT()
    ON_WM_MEASUREITEM()
    ON_WM_DRAWITEM()
END_MESSAGE_MAP()

void CMyListCtrlEx::SetRowHeigt(int nHeight)
{
    m_uRowHeight = nHeight;

    CRect rcWin;
    GetWindowRect(&rcWin);
    WINDOWPOS wp;
    wp.hwnd = m_hWnd;
    wp.cx = rcWin.Width();
    wp.cy = rcWin.Height();
    wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
    SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}

void CMyListCtrlEx::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
    CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

void CMyListCtrlEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
    lpMeasureItemStruct->itemHeight = m_uRowHeight;
}

void CMyListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    LVITEM lvi = { 0 };
    lvi.mask = LVIF_STATE;//|LVIF_IMAGE; 
    lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
    lvi.iItem = lpDrawItemStruct->itemID;
    BOOL bGet = GetItem(&lvi);

    BOOL bSelect = ((lvi.state & LVIS_DROPHILITED) || ((lvi.state & LVIS_SELECTED)
        && ((GetFocus() == this) || (GetStyle() & LVS_SHOWSELALWAYS))));

    //画文本背景 
    CRect bgRect = lpDrawItemStruct->rcItem;
    if (bSelect)//设置选中颜色
    {
        pDC->SetTextColor(RGB(255, 255, 255));//白色文本
        pDC->FillRect(bgRect, &CBrush(RGB(88, 88, 88)));//深灰色背景
    }
    else
    {
        int iRow = lvi.iItem;
        if (iRow % 2 == 0)//设置偶数行文字颜色和背景颜色
        {
            pDC->SetTextColor(RGB(0, 0, 0));//黑色文本
            pDC->FillRect(bgRect, &CBrush(RGB(128, 128, 128))); //灰色背景
        }
        else//设置奇数行文字颜色和背景颜色
        {
            pDC->SetTextColor(RGB(0, 0, 0));//黑色文本
            pDC->FillRect(bgRect, &CBrush(RGB(255, 255, 255))); //白色背景
        }
    }

    //绘制文本
    if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
    {
        //得到列数
        //int nCollumn = GetHeaderCtrl()->GetItemCount();

        //循环处理
        CString szText;
        for (int i = 0; i < GetHeaderCtrl()->GetItemCount(); i++)
        {
            CRect rcItem;
            if (!GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem))
            {
                continue;
            }

            szText = GetItemText(lpDrawItemStruct->itemID, i);

            rcItem.left += 5; rcItem.right -= 1;
            pDC->DrawText(szText, lstrlen(szText), &rcItem,
                DT_LEFT | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE);
        }
    }
}

3.使用CMyListCtrlEx创建表格 

    CRect rect;
    auto parent = this->GetParent();
    if (parent)
    {
        parent->GetClientRect(&rect);
        parent->ClientToScreen(&rect);//647*450
    }


    m_listCtrl.Create(WS_BORDER | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS
        | LVS_NOCOLUMNHEADER | LVS_OWNERDRAWFIXED, rect, this, 101);
    m_listCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | WS_VSCROLL);
    m_listCtrl.InsertColumn(0, _T(""), LVCFMT_LEFT, 0);//第0列无法居中显示,一般将其隐藏
    m_listCtrl.InsertColumn(1, _T("No"), LVCFMT_LEFT, 30); // 插入第2列的列名
    m_listCtrl.InsertColumn(2, _T("Name"), LVCFMT_LEFT, 450); // 插入第3列的列名
    m_listCtrl.InsertColumn(3, _T("State"), LVCFMT_LEFT, 70); // 插入第4列的列名
    m_listCtrl.InsertColumn(4, _T("Operation"), LVCFMT_LEFT, 90); // 插入第4列的列名
    m_listCtrl.SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER);

    CString strNo;
    CString strName;
    CString strState;
    CString strOpt;
    for (int i = 0; i <= 5; i++) {
        strNo.Format(_T("%d"), i);
        strName.Format(_T("Ecu%d"), i);
        strState.Format(_T("未扫描"), 20 + i);
        strOpt = _T("进入系统");
        m_listCtrl.InsertItem(i, _T("")); // 插入行
        m_listCtrl.SetItemText(i, 1, strNo);
        m_listCtrl.SetItemText(i, 2, strName);
        m_listCtrl.SetItemText(i, 3, strState);
        m_listCtrl.SetItemText(i, 4, strOpt);
    }

如有问题,可以留言咨询

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen Roson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值