犯了一个小错,水平滚动时出现文字重影,所以改了下:
错误在于,在遍历列时,我用
CRect rcColumn; GetHeaderCtrl()->GetItemRect(i, &rcColumn);
这是错误的,因为一旦水平滚动时,这样得到的rcColumn就会不确定,因为rcColumn是得到绘制区域left,right,这两个值不停变化,从而产生文字的重影
所以改了下,改用GetColumn,因为它得到的是width,在水平滚动时,width是不会变化的。
代码如下:
CDC dc; CRect rc; dc.Attach(lpDIS->hDC); rc = lpDIS->rcItem; CRect rcItem(rc); //--draw bkgrand ------------------------------- if (lpDIS->itemState & ODS_SELECTED) { dc.FillSolidRect(&rcItem,RGB(235, 235, 235)); } else { dc.FillSolidRect(&rcItem,RGB(255, 255, 255)); } TCHAR szBuf[1024]; LVITEM lvi; lvi.iItem =lpDIS->itemID; lvi.iSubItem = 0; lvi.mask = LVIF_TEXT | LVIF_IMAGE; lvi.pszText = szBuf; lvi.cchTextMax = 1024; GetItem(&lvi); CRect rcIcon; rcIcon.SetRectEmpty(); //-----draw image -------------------------------- CImageList* pImgList = GetImageList(LVSIL_NORMAL); if(!pImgList) pImgList = GetImageList(LVSIL_SMALL); if(!pImgList) pImgList = GetImageList(LVSIL_STATE); if (pImgList&&lvi.iImage >=0) { IMAGEINFO ii = {0}; pImgList->GetImageInfo(lvi.iImage, &ii); rcIcon = ii.rcImage ; rcIcon.OffsetRect(rcItem.TopLeft()) ; CPoint ptIcon; ptIcon.x=rcIcon.left; ptIcon.y=rcIcon.top; pImgList->Draw(&dc, lvi.iImage, ptIcon, ILD_TRANSPARENT); } //---draw text ------------------------------------ dc.ExcludeClipRect(rcIcon) ; CRect rcText(rc); if (!rcIcon.IsRectEmpty()) { rcText.left = rcIcon.right; } LVCOLUMN lvc={0}; lvc.mask = LVCF_WIDTH; LVCOLUMN lvcPre={0}; lvcPre.mask = LVCF_WIDTH; int nColumn = GetHeaderCtrl()->GetItemCount(); for (int i=0; i<nColumn; i++) { //CRect rcColumn; //GetHeaderCtrl()->GetItemRect(i, &rcColumn); GetColumn(i, &lvc); if (i>0) { GetColumn(i-1, &lvcPre); rcText.left += lvcPre.cx; // } rcText.right = rcText.left + lvc.cx; dc.SetBkMode(TRANSPARENT); if (lpDIS->itemState & ODS_SELECTED) { dc.SetTextColor(RGB(255, 0, 0)); } else { dc.SetTextColor(RGB(128, 128, 128)); } CString szItem; szItem = GetItemText(lpDIS->itemID, i); dc.DrawText(szItem, &rcText, DT_LEFT|DT_SINGLELINE|DT_VCENTER); } dc.Detach();