文档库 最新最全的文档下载
当前位置:文档库 › MFC列表控件

MFC列表控件

MFC列表控件
MFC列表控件

列表控件(CListCtrl)和列表框控件(CListBox)有一定的区别,大家不要混淆。列表控件(List Control)用来成列地显示数据,其表项通常包括图标(Icon)和标签(Lable)两部分,它们分别提供了对数据的形象和抽象描述。

图像列表控件(CImageList)是相同大小图像的一个集合,每个集合中均以0为图像的索引序号基数,(可以把这些图标看成是以数组方式存储的),图像列表通常由大图标或位图构成,其中包含透明位图模式(实际上,所有的再一个图像列表中的图标都被存储在一个屏幕设备的位图中)。可以利用windows API来绘制、建立和删除图像,并能实现增加、删除、替换和拖动图像邓操作。图像列表控件提供了控制图像列表额基本方法,这些方法在Windows 95及以后版本才能实现。该控件是不可见的,通常与其他如CListBox、CComboBox、CComboBoxEx 以及CTabCtrl一起使用,为他们提供图标资源。

CImageList& ImageList建立图像控件对象结构,Create初始化图像列表并绑定对象,图像控件的建立方法如下BOOL Create(int cx,int cy,UINT nFlags,int nInitial,int nGrow);

BOOL Create(UINT nBitmapID,int cx,int nGrow,COLORREF crMask);

BOOL Create(LPCTSTR lpszBitmapID,int cx,int nGrow,COLORREF crMask);

BOOL Create(CImageList& imageList1,int nImage1,CImageList& imagelist2,int nImage2,int dx,int dy);

其中各项参数的含义为:cx定义图像的宽度,单位为像素;cy定义图像的高度,单位为像素;nFlags确定建立图像列表的类型,可以是以下值的组合:ILC_COLOR、ILC_COLOR4、ILC_COLOR8、ILC_COLOR16、ILC_COLOR24、ILC_COLOR32、ILC_COLORDDB和ILS_MASK;nInitial用来确定图像列表包含的图像;nGrow 用来确定图像列表可控制的图像数量。

nBitmapID用来确定图像列表联系的位图标志值;crMask表示颜色屏蔽位;lpszBitmapID用来确定包含位图资源额标识串;imagellist1指向图像列表控件对象的一个指针;nImage1图像列表1中包含的图像数量;imagelist2指向图像列表控件对象的一个指针。

对于图像控件,同样不能单独使用,必须与列表控件、树控件和标签控件相互结合使用,下面分别介绍其具体应用技巧。

图像控件在列表控件中的应用技巧

设置图像控件CListCtrl::SetImageList的调用格式如下:

CImageList* SetImageList(CImageList* pImageList,int nImageList);

其返回值是指向前一个图像列表控件的一个指针,如果不存在前一个图像列表则为NULL;其中参数pImageList是指向图像列表的标识,nImageList是图像列表的类型,可以是如下值:LVSIL_NORMAL用大图标方式进行图像列表;LVSIL_SAMLL用小图标方式进行图像列表;LVSIL_STATE以图像状态进行图像列表。

使用图像列表

构造CImageList对象,调用其Create函数的一个重载来创建图像列表并将图像列表附加到CImageList对象;

如果在创建图像列表时没有添加图像,则调用Add或Read成员函数向图像列表添加图像;

通过调用控件的适当成员函数,将图像列表与此控件相关联,或者您自己使用图像列表的Draw成员函数绘制图像列表中的图像;

注意:如果图像列表是用new运算符创建的,则在操作完CImageList对象后必须销毁它。

CListCtrl::InsertColumn

This method inserts a new column in a list view control. The leftmost column in a list view control must be left-aligned.

The LVCOLUMN structure contains the attributes of a column in report view. It is also used to receive information about a column. This structure is described in the Platform SDK.

int InsertColumn( int nCol, const LVCOLUMN* pColumn );int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = –1, int nSubItem = –1 );

CListCtrl::InsertItem

This method inserts an item into the list view control.

int InsertItem( const LVITEM* pItem );int InsertItem( int nItem, LPCTSTR lpszItem );int InsertItem( int nItem, LPCTSTR lpszItem, int nImage );

Parameters

pItem

Specifies the pointer to an LVITEM structure that specifies the attributes of the item.

nItem

Specifies the index of the item to be inserted.

lpszItem

Specifies the address of a string containing the label of the item or LPSTR_TEXTCALLBACK if the item is

a callback item. For information on callback items, see CListCtrl::GetCallbackMask.

nImage

Specifies the index of an image for the item, or I_IMAGECALLBACK if the item is a callback item. For

information on callback items, see CListCtrl::GetCallbackMask.

nMask

Specifies which item attributes passed as parameters are valid. It can be one or more of the mask values. The

valid values can be combined with the bitwise OR operator.

nState

Indicates the state of the item, state image, and overlay image.

nStateMask

Indicates which bits of the state member will be retrieved or modified. See LVITEM.

nImage

Specifies the index of an image for the item within the image list.

lParam

Specifies a 32-bit application-specific value associated with the item. If this parameter is specified, you must

set the nMask attribute LVIF_PARAM.

CListCtrl::SetItemText

This method changes the text of a list view item or subitem.

BOOL SetItemText( int nItem, int nSubItem, LPCTSTR lpszText );

Parameters

nItem

Specifies the index of the item whose text is to be set.

nSubItem

Specifies the index of the subitem, or zero to set the item label.

lpszText

Specifies the pointer to a string that contains the new item text.

Return Value

Nonzero if it is successful; otherwise, it is zero.

CListCtrl::SetImageList

This method assigns an image list to a list view control. CImageList* SetImageList( CImageList* pImageList, int nImageListType);

Parameters

pImageList

Specifies the pointer to the image list to assign.

nImageListType

Specifies the type of image list. It can be one of these values:

LVSIL_NORMAL Image list with large icons.

?LVSIL_SMALL Image list with small icons.

?LVSIL_STATE Image list with state images.

Return Value

A pointer to the previous image list.

CListCtrl::SetItemState

BOOL SetItemState(int nItem,LVITEM*pItem);

BOOL SetItemState(int nItem,UINT nState,UINT nMask); CListCtrl::GetItemCount

This method retrieves the number of items in a list view control.

int GetItemCount( ) const;

CListCtrl::GetFirstSelectedItemPositio n

This method gets the position of the first selected item in the list view control.

POSITION GetFirstSelectedItemPosition( )const; CListCtrl::GetNextSelectedItem

This method retrieves the index of the list item identified by pos, then sets pos to the POSITION value. You can use GetNextSelectedItem in a forward iteration loop if you establish the initial position with a call to GetFirstSelectedItemPosition.

You must ensure that your POSITION value is valid. If it is invalid, then the Debug version of the MFC for Windows CE asserts.

int GetNextSelectedItem(POSITION&pos)const; CListCtrl::DeleteItem

This method deletes an item from a list view control.

BOOL DeleteItem( int nItem );

相关文档