VC6.0给ListView(CListCtrl)添加数据

VC6.0给ListView(CListCtrl)添加数据

CListCtrl* theCtrl = GetListCtrl(); 
theCtrl.InsertColumn(0,"姓名",LVCFMT_CENTER,50); 
theCtrl.InsertColumn(0,"班级",LVCFMT_CENTER,50); 
如何将 “张三”加入到姓名列中;如何将“初三四班”加入到班级列中?

 

theCtrl.InsertItem(0, "张三"); 
第一个参数是索引即行数,写0就是插到第一行,第二个参数是内容 

theCtrl.SetItemText(0, 1, "初三四班"); 
第一个参数同样是行索引,第二个参数是列索引,第三个不说了 

首先使用theCtrl.GetItemCount();获得列表中的总行数。比如n=theCtrl.GetItemCount(); 
然后使用theCtrl.InsertItem(n, "张三");新加一行,但新增的行只有第一列有数据,就是"张三",后面的列就可以用theCtrl.SetItemText(n, 1, "初二六班");的方法进行设置或修改了。 
当然不用获得总行数,直接用theCtrl.InsertItem(0, "张三");会把新行插到第一行的位置,类似于头插法,上面说的那个相当于尾插法。

VC6 CListCtrl继承类例子 oid CListCtrlCl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: 添加您的代码以绘制指定项 TCHAR lpBuffer[256]; LV_ITEM lvi; lvi.mask = LVIF_TEXT | LVIF_PARAM ; lvi.iItem = lpDrawItemStruct->itemID ; lvi.iSubItem = 0; lvi.pszText = lpBuffer ; lvi.cchTextMax = sizeof(lpBuffer); VERIFY(GetItem(&lvi)); LV_COLUMN lvc, lvcprev ; ::ZeroMemory(&lvc, sizeof(lvc)); ::ZeroMemory(&lvcprev, sizeof(lvcprev)); lvc.mask = LVCF_WIDTH | LVCF_FMT; lvcprev.mask = LVCF_WIDTH | LVCF_FMT; CDC* pDC; pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rtClient; GetClientRect(&rtClient); for ( int nCol=0; GetColumn(nCol, &lvc); nCol++) { if ( nCol > 0 ) { // Get Previous Column Width in order to move the next display item GetColumn(nCol-1, &lvcprev) ; lpDrawItemStruct->rcItem.left += lvcprev.cx ; lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left; } CRect rcItem; if (!GetSubItemRect(lpDrawItemStruct->itemID,nCol,LVIR_LABEL,rcItem)) continue; ::ZeroMemory(&lvi, sizeof(lvi)); lvi.iItem = lpDrawItemStruct->itemID; lvi.mask = LVIF_TEXT | LVIF_PARAM; lvi.iSubItem = nCol; lvi.pszText = lpBuffer; lvi.cchTextMax = sizeof(lpBuffer); VERIFY(GetItem(&lvi)); CRect rcTemp; rcTemp = rcItem; if (nCol==0) { rcTemp.left -=2; } if ( lpDrawItemStruct->itemState & ODS_SELECTED ) { pDC->FillSolidRect(&rcTemp, GetSysColor(COLOR_HIGHLIGHT)) ; pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ; } else { COLORREF color; color = GetBkColor(); pDC->FillSolidRect(rcTemp,color); if (FindColColor(nCol,color)) { pDC->FillSolidRect(rcTemp,color); } if (FindItemColor(nCol,lpDrawItemStruct->itemID,color)) { pDC->FillSolidRect(rcTemp,color); } //pDC->SetTextColor(m_color); } pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT)); UINT uFormat = DT_CENTER ; if (m_Header.m_Format[nCol]=='0') { uFormat = DT_LEFT; } else if (m_Header.m_Format[nCol]=='1') { uFormat = DT_CENTER; } else if (m_Header.m_Format[nCol]=='2') { uFormat = DT_RIGHT; } TEXTMETRIC metric; pDC->GetTextMetrics(&metric); int ofst; ofst = rcItem.Height() - metric.tmHeight; rcItem.OffsetRect(0,ofst/2); pDC->SetTextColor(m_color); COLORREF color; if (FindColTextColor(nCol,color)) { pDC->SetTextColor(color); } if (FindItemTextColor(nCol,lpDrawItemStruct->itemID,color)) { pDC->SetTextColor(color); } CFont nFont ,* nOldFont; nFont.CreateFont(m_fontHeight,m_fontWith,0,0,0,FALSE,FALSE,0,0,0,0,0,0,_TEXT("宋体"));//创建字体 nOldFont = pDC->SelectObject(&nFont); DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer), &rcItem, uFormat) ; pDC->SelectStockObject(SYSTEM_FONT) ; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值