CListCtrl的LVCOLUMN简单使用笔记

今天自己设计一个CListCtrlEx类,遇到一些问题。幸好在网上查了资料,问题都得以解决。为了感谢给我提供帮助的人,同时本着开源共享的原则,将今天所学内容在此做个笔记。

 

列表视图控件(List Control)
列表视图控件是一种非常常用的控件,在需要以报表形式显示数据时,列表控件通常是最好的选择。

列表视图控件(List Control)的数据类是CListCtrl,CListCtrl类有两个重要的数据结构LVCOLUMN和LVITEM。LVCOLUMN用于定义报表方式下的“列”的结构;LVITEM用于定义“项”的结构。此处我只说说LVCOLUMN结构。

typedef struct _LVCOLUMN { 
    UINT mask; 
    int fmt; 
    int cx; 
    LPTSTR pszText; 
    int cchTextMax; 
    int iSubItem; 
#if (_WIN32_IE >= 0x0300)
    int iImage;
    int iOrder;
#endif
} LVCOLUMN, FAR *LPLVCOLUMN; 


以下是MSDN的说明:

Contains information about a column in report view. This structure is used both for creating and manipulating columns. This structure supersedes the LV_COLUMN structure.

mask
Variable specifying which members contain valid information. This member can be zero, or one or more of the following values: LVCF_FMT  The fmt member is valid.  
LVCF_IMAGE  Version 4.70. The iImage member is valid.  
LVCF_ORDER  Version 4.70. The iOrder member is valid.  
LVCF_SUBITEM  The iSubItem member is valid.  
LVCF_TEXT  The pszText member is valid.  
LVCF_WIDTH  The cx member is valid.  

fmt
Alignment of the column heading and the subitem text in the column. This member can be one of the following values: LVCFMT_BITMAP_ON_RIGHT  Version 4.70. The bitmap appears to the right of text. This does not affect an image from an image list assigned to the header item.
LVCFMT_CENTER  Text is centered.
LVCFMT_COL_HAS_IMAGES  Version 4.70. The header item contains an image in the image list.
LVCFMT_IMAGE  Version 4.70. The item displays an image from an image list.  
LVCFMT_LEFT  Text is left-aligned.
LVCFMT_RIGHT  Text is right-aligned.

The leftmost column in a list view control must be left-aligned.

cx
Width of the column, in pixels.
pszText
If column information is being set, this member is the address of a null-terminated string that contains the column heading text. If the structure is receiving information about a column, this member specifies the address of the buffer that receives the column heading text.
cchTextMax
Size of the buffer pointed to by the pszText member. If the structure is not receiving information about a column, this member is ignored.
iSubItem
Index of subitem associated with the column.
iImage
Version 4.70. Zero-based index of an image within the image list. The specified image will appear within the column.
iOrder
Version 4.70. Zero-based column offset. Column offset is in left-to-right order. For example, zero indicates the leftmost column.

 

简单说明下:

typedef struct _LVCOLUMN {
UINT mask;            //说明此结构中哪些成员是有效的(枚举值见MSDN的列举)
int fmt;           //列的对齐方式
int cx;            //列的初始宽度
LPTSTR pszText; //列的标题
int cchTextMax;   //pszText所指向的缓冲区的大小
int iSubItem;      //与列关联的子项的索引值,从0开始,很多人都不用这个值,没关系
int iImage;           //与列关联的图像列表中指定图像的索引值
int iOrder;           //第几列,0代表最左一列
} LVCOLUMN, FAR *LPLVCOLUMN;


下面给出一个Example:

CListCtrl m_listPwrResults;
//插入一列int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1 )
m_listPwrResults.InsertColumn(0, _T("TEXT"),LVCFMT_LEFT);
//获得一列BOOL GetColumn(int nCol,LVCOLUMN* pColumn )const
LVCOLUMN colTest;
colTest.mask = LVCF_FMT | LVCF_WIDTH | LVCF_ORDER;

m_listPwrResults.GetColumn(0,&colTest);
//这样colTest里就可以读到对齐方式、宽度、第几列。
//(因为colTest.mask = LVCF_FMT | LVCF_WIDTH | LVCF_ORDER只设置了这3个值,其他内容都为空)。


特别指出,如果需要获得列标题pszText,仅仅colTest.mask   = LVCF_TEXT还不够,需要给pszText指针指定一块内存。

下面给出如何获得列标题的Example:

LVCOLUMN   lvColumn;   
TCHAR strChar[256];
lvColumn.pszText=strChar;   
lvColumn.cchTextMax=256 ;
lvColumn.mask   = LVCF_TEXT;

CListCtrl& theCtrl=pListView->GetListCtrl();
theCtrl.GetColumn(col,&lvColumn);
CString str=lvColumn.pszText;

 

 

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值