CListCtrl的Report(详细)视图基本用法

简单介绍

CListCtrl的报表视图,即LVS_REPORT,也可叫做详细视图,可以显示多列,而其它几个视图LVS_LIST、LVS_ICON、LVS_SMALLICON则没有多列显示功能。

基本用法

在对话框中可以通过VS的工具来拖拽一个CListCtrl到界面上,但在非对话框中只能用Create或CreateEx来创建。由此可看出,尽早学会用Create或CreateEx来创建一个CListCtrl是一个明智之举。以下使用后者来做一个演示:

  • 创建CListCtrl控件

    CRect rcClient;
    	GetClientRect(&rcClient);
    // 	m_ListCtrl.CreateEx(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES, 
    // 		WS_CHILD | WS_VISIBLE | LVS_REPORT,
    // 		rcClient,
    // 		this, IDC_LIST1);
    	m_ListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT,
    		rcClient, this, IDC_LIST1);
    	DWORD dwExtStyles = m_ListCtrl.GetExtendedStyle();
    	m_ListCtrl.SetExtendedStyle(dwExtStyles | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
    

    以上代码创建了一个带有网格线,选中时整行会高亮的CListCtrl控件,注意屏蔽的代码,本人之前以为CreateEx的第一个参数是可以传递CListCtrl控件的扩展样式的,查找MSDN才得知,第一个参数只能传递WS_EX_ 打头的扩展样式,要应用控件的扩展样式,要调用函数SetExtendedStyle来实现,以下是原文:

    Use CreateEx instead of Create to apply extended Windows styles, specified by the Windows extended style preface WS_EX_.
    CreateEx creates the control with the extended Windows styles specified by dwExStyle. To set extended styles specific to a control, call SetExtendedStyle. For example, use CreateEx to set such styles as WS_EX_CONTEXTHELP, but use SetExtendedStyle to set such styles as LVS_EX_FULLROWSELECT. For more information, see the styles described in the topic Extended List View Styles in the Windows SDK.

  • 创建列

    //插入两列,在Report视图下,首先插入列是必需的,是控件初始化的一部分
    m_ListCtrl.InsertColumn(0, _T("第一列") );
    m_ListCtrl.InsertColumn(1, _T("第二列") );
    
    //设置列宽,试过LVS_EX_AUTOSIZECOLUMNS,但效果没出来,故手动设置
    CRect rcListCtrl;
    m_ListCtrl.GetClientRect(&rcListCtrl);
    m_ListCtrl.SetColumnWidth(0, rcListCtrl.Width() / 2 );
    m_ListCtrl.SetColumnWidth(1, rcListCtrl.Width() / 2);
    
  • 初始化数据

    //初始化各行数据(一行有两列,先用InsertItem新增一行,再使用其返回的行索引设置剩余列值)
    int nRowIndex = -1;
    nRowIndex = m_ListCtrl.InsertItem(0, _T("aaaaaaa"));
    if (nRowIndex >= 0)
    {
    	m_ListCtrl.SetItemText(nRowIndex, 1, _T("111111"));
    }
    nRowIndex = m_ListCtrl.InsertItem(1, _T("bbbbbbbb"));
    if (nRowIndex >= 0)
    {
    	m_ListCtrl.SetItemText(nRowIndex, 1, _T("222222"));
    }
    nRowIndex = m_ListCtrl.InsertItem(2, _T("ccccccc"));
    if (nRowIndex >= 0)
    {
    	m_ListCtrl.SetItemText(nRowIndex, 1, _T("3333333"));
    }
    

截图演示

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值