资源描述:
《ListControl控件的使用方法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、ListControl控件的使用方法[录入者:NCRE100
2、 时间:2009-02-1418:14:11
3、作者:
4、来源:NCRE100.com
5、浏览:308次] 列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值。MFC中使用CListCtrl类来封装列表控件的各种操作。通过调用BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );创建一个窗口,dwStyle中可以使用以下一些列表控件的专用风格: LVS_ICO
6、N ,LVS_SMALLICON ,LVS_LIST, LVS_REPORT 这四种风格决定控件的外观,同时只可以选择其中一种,分别对应:大图标显示,小图标显示,列表显示,详细报表显示 LVS_EDITLABELS 结点的显示字符可以被编辑,对于报表风格来讲可编辑的只为第一列。 LVS_SHOWSELALWAYS 在失去焦点时也显示当前选中的结点 LVS_SINGLESEL 同时只能选中列表中一项 首先你需要设置列表控件所使用的ImageList,如果你使用大图标显示风格,你就需要以如下形式调用: CImageList* SetImage
7、List( CImageList* pImageList, LVSIL_NORMAL); 如果使用其它三种风格显示而不想显示图标你可以不进行任何设置,否则需要以如下形式调用: CImageList* SetImageList( CImageList* pImageList, LVSIL_SMALL); int InsertItem( int nItem, LPCTSTR lpszItem ); 插入行 nItem:指明插入位置 lpszItem:为显示字符。 除LVS_REPORT风格外其他三种风格都只需要直接调用InsertItem就可
8、以了,但如果使用报表风格就必须先设置列表控件中的列信息。 int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat , int nWidth, int nSubItem); 插入列iCol:为列的位置,从零开始lpszColumnHeading:为显示的列名nFormat:为显示对齐方式nWidth:为显示宽度nSubItem:为分配给该列的列索引。 BOOL SetItemText( int nItem, int nSubItem, LPTSTR lpszText );设
9、置每列的显示字符 nItem:为行位置 nSubItem:为列位置 lpszText:为显示字符 下面的代码演示了如何设置多列并插入数据: m_list.SetImageList(&m_listSmall,LVSIL_SMALL);//设置ImageList m_list.InsertColumn(0,"Col 1",LVCFMT_LEFT,300,0);//设置列 m_list.InsertColumn(1,"Col 2",LVCFMT_LEFT,300,1); m_list.InsertColumn(2,"Col 3",LVCFMT_L
10、EFT,300,2); m_list.InsertItem(0,"Item 1_1");//插入行 m_list.SetItemText(0,1,"Item 1_2");//设置该行的不同列的显示字符 m_list.SetItemText(0,2,"Item 1_3") COLORREF GetTextColor( )/BOOL SetTextColor( COLORREF cr ):用于得到/设置显示的字符颜色。 COLORREF GetTextBkColor( )/BOOL SetTextBkColor( COLORREF cr ):用于
11、得到/设置显示的背景颜色。 void SetItemCount( int iCount ):用于得到添加进列表中项的数量。 BOOL DeleteItem(int nItem):用于删除某一项 BOOL DeleteAllItems( ):将删除所有项。 BOOL SetBkImage(HBITMAP hbm, BOOL fTile , int xOffsetPercent, int yOffsetPercent):用于设置背景位图。 CString GetItemText( int nItem, int nSubItem ):用于得到某项
12、的显示字符。 列表控件的消息映射同样使用ON