各位大神们。我在网上找了个重绘list的代码,如果说不调用窗口刷新函数,那么进度条画布上去,但是调用窗口刷新函数,又一直闪,特别是数据比较多的时候,闪的吓死人,有没有什么办法可以避免闪烁这一点。下面是代码:
void CProcessList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
//LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
//// TODO: 在此添加控件通知处理程序代码
//*pResult = 0;
//draw each item.set txt color,bkcolor....
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
{
// This is the notification message for an item.  We'll request
// notifications before each subitem's prepaint stage.
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
{
// This is the prepaint stage for a subitem. Here's where we set the
// item's text and background colors. Our return value will tell
// Windows to draw the subitem itself, but it will use the new colors
// we set here.
int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
int nSubItem = pLVCD->iSubItem;
if(nSubItem != 8)//这里我只重绘第3列
return;
COLORREF crText  = ::GetSysColor(COLOR_WINDOWFRAME);
COLORREF crBkgnd = ::GetSysColor(COLOR_WINDOW);
CDC* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);
CRect rect;
GetSubItemRect(nItem, nSubItem, LVIR_BOUNDS, rect);
CString cstrTemp;
cstrTemp = GetItemText(nItem,nSubItem);

if (LVIS_SELECTED == GetItemState(nItem,LVIS_SELECTED))
{
DrawText(nItem, nSubItem, pDC, ::GetSysColor(COLOR_HIGHLIGHT), ::GetSysColor(COLOR_HIGHLIGHT), rect);
}
else
{
DrawText(nItem, nSubItem, pDC, crText, crBkgnd, rect);
}
*pResult = CDRF_SKIPDEFAULT; // We've painted everything.
}
}void CProcessList::DrawText(int nItem, int nSubItem, CDC *pDC, COLORREF crText, COLORREF crBkgnd, CRect &rect)
{
ASSERT(pDC);
pDC->FillSolidRect(&rect, crBkgnd);
GROUP_NODE* pNode = (GROUP_NODE*)GetItemData(nItem);
int nPercent = pNode->nProcess;
TRACE(_T("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%d\n"),nPercent);
CRect procRect = rect;
CPen pen;
CPen * Oldpen;
pen.CreatePen(PS_NULL,0,RGB(0,0,0));
Oldpen = pDC->SelectObject(&pen);
procRect.right += 1;
pDC->Rectangle(procRect);
procRect.left += 1;
procRect.bottom -= 1;
procRect.top += 0;
procRect.right = procRect.left + rect.Width() * nPercent / 100;
CString str;
CBrush brush;
brush.CreateSolidBrush((RGB(0,255,0)));
str.Format("%d%%", nPercent);
if (-1 == nPercent)
{
brush.DeleteObject();
brush.CreateSolidBrush((RGB(255,0,0)));
procRect.right = procRect.left + rect.Width();
str = _T("升级失败");
}
if (-2 == nPercent)
{
brush.DeleteObject();
brush.CreateSolidBrush((RGB(255,0,0)));
procRect.right = procRect.left + rect.Width();
str = _T("重启失败");
}
if(101 == nPercent)
{
str = _T("正在重启");
}
if (102 == nPercent)
{
str = _T("重启成功");
}
pDC->FillRect(&procRect, &brush);
if (!str.IsEmpty())
{
UINT nFormat = DT_VCENTER | DT_SINGLELINE | DT_CENTER |DT_WORDBREAK|DT_EDITCONTROL;
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crText);
pDC->SetBkColor(crBkgnd);
pDC->DrawText(str, &rect, nFormat);
TRACE(_T("chonghui............\n"));
} pDC->SelectObject(Oldpen);
}控件、列表MFCListCtrl