靠!我也来贴!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1stopping flicker during updates--------------------------------------------------------------------------------this article was contributed by roger onslow from australia. when making major changes to a list control (and other controls) it is a good idea to turn off updating of the control. to do this you would use setredraw(false) at the start of the change and setredraw(true) at the end, and then invalidate the control. however, setredraw simply turns redraw on or off, it does not let you know whether or not redraw was on or off before the call. and there is no function that can tell you this. for this reason, i have my own setredraw function that counts the number of times you have turned it on and turned it off. define a member variable: int m_redrawcount;in your contructor do: m_redrawcount = 0;and then define the following function (in this case for a list control): void cmylistctrl::setredraw( bool bredraw) { if (! bredraw) { if (m_redrawcount++ <= 0) { clistctrl::setredraw(false); } } else { if (--m_redrawcount <="0)" { clistctrl::setredraw(true); m_redrawcount="0;" invalidate(); } } } the first time you turn redraw off, the real setredraw function is called to turn redrawing off. subsequently the function increases a counter when you requested redraw be turned off, and decreases it when you request redraw to be turned on again. when the counter gets back down to zero, the real setredraw function is called to turn redrawing back on again and the control is invalidated (so that redrawing actually takes place). this means you can not put setredraw(false) at the start of, and setredraw(true) and the end of, any function that makes large changes to the lsit control (like loading, sorting, changing with column widths or order etc).
1、如何解决闪烁的问题:
使用内存DC,在内存中画完后,一次将内容拷贝到屏幕上。
2、减少刷屏次数:
LockWindowUpdate和UnlockWindowUpdate来控制,目的是尽量减少数据变化
而引起的屏幕更新。
写得很乱 CListCtrl &rListCtrl = GetListCtrl();
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem(lpDrawItemStruct->rcItem);
int nItem = lpDrawItemStruct->itemID;
int nId=lpDrawItemStruct->CtlID;
/ CImageList* pImageList;
CHeaderCtrl *pHeaderCtrl = (CHeaderCtrl*)rListCtrl.GetDlgItem(0);// Save dc state
int nSavedDC = pDC->SaveDC();// Get item image and state info
LV_ITEM lvi;
lvi.mask = LVIF_IMAGE | LVIF_STATE;
lvi.iItem = nItem;
lvi.iSubItem = pHeaderCtrl->OrderToIndex(0);
lvi.stateMask = 0xFFFF; // get all state flags
rListCtrl.GetItem(&lvi); // Should the item be highlighted
BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)
|| ( (lvi.state & LVIS_SELECTED)
&& ((rListCtrl.GetFocus() == this)
|| (rListCtrl.GetStyle() & LVS_SHOWSELALWAYS)
)
)
); CMarketWatcherDoc* pDoc = GetDocument();
// Get rectangles for drawing
CRect rcBounds, rcLabel, rcIcon,rHead;
pHeaderCtrl->GetItemRect(pHeaderCtrl->OrderToIndex(0),rHead);
rListCtrl.GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
rListCtrl.GetItemRect(nItem, rcLabel, LVIR_LABEL);
// rListCtrl.GetItemRect(nItem, rcIcon, LVIR_ICON);
CRect rcCol( rcBounds );
rcLabel.left=rHead.left;
rcLabel.right=rHead.right; CString sLabel = rListCtrl.GetItemText( nItem, pHeaderCtrl->OrderToIndex(0) ); // Labels are offset by a certain amount
// This offset is related to the width of a space character
int offset = pDC->GetTextExtent(_T(" "), 1 ).cx*2; CRect rcHighlight;
CRect rcWnd;
int nExt;
switch( m_nHighlight )
{
case 0:
nExt = pDC->GetOutputTextExtent(sLabel).cx + offset;
rcHighlight = rcLabel;
if( rcLabel.left + nExt < rcLabel.right )
rcHighlight.right = rcLabel.left + nExt;
break;
case 1:
rcHighlight = rcBounds;
rcHighlight.left = rcLabel.left;
break;
case 2:
GetClientRect(&rcWnd);
rcHighlight = rcBounds;
rcHighlight.left = rcLabel.left;
rcHighlight.right = rcWnd.right;
break;
default:
rcHighlight = rcLabel;
}
// Draw the background color
COLORREF corBack=WHITE;
if( bHighlight )
{
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
// pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
// pDC->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
pDC->SetBkColor(_LGRAY);
pDC->FillRect(rcHighlight, &CBrush(_LGRAY));
corBack=_LGRAY;
}
else
{
pDC->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_WINDOW)));
corBack=::GetSysColor(COLOR_WINDOW);
}
/* if( bHighlight )
{
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); pDC->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
}
else
{
CRect rcClient, rcRow = rcItem;
GetClientRect(&rcClient);
rcRow.right = rcClient.right; pDC->FillRect(rcRow, &CBrush(nItem%2 ? ::GetSysColor(COLOR_WINDOW) :
RGB(255,255,0)));
}*/
// Set clip region
rcCol.right = rcCol.left + rListCtrl.GetColumnWidth(pHeaderCtrl->OrderToIndex(0));
CRgn rgn;
rgn.CreateRectRgnIndirect(&rcCol);
pDC->SelectClipRgn(&rgn);
rgn.DeleteObject(); // Draw item label - Column 0
rcLabel.left += offset/2;
rcLabel.right -= offset;
char nTemp[50];
rListCtrl.GetItemText(nItem, 0,nTemp,50);
COLORREF corB=corBack;
COLORREF cor=pDC->GetTextColor();
int nColumn =0;
pDC->SetTextColor(_BLACK);
if(m_bPre && pHeaderCtrl->OrderToIndex(nColumn)==4)
{
CString s;
s.Format("%s",sLabel);
int k=s.Find("$",0);
s.Delete(k,1);
double f = atof(s);
if(f>0)
{
corB=LGREEN;
}
else if(f<0)
{
corB=LRED;
}
}
if(!m_bPre&& (m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==3
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==10))
{
char sh[60]={"1"},sl[60]={"2"},ps[60]={"3"};
pDoc->GetData(ps,3,nTemp);
pDoc->GetData(sh,1,nTemp);
pDoc->GetData(sl,2,nTemp);
if(strcmp(sh,ps)==0 && strcmp(sh,"")!=0)
corB=_YELLOW;
else if(strcmp(sl,ps)==0 && strcmp(sl,"")!=0)
corB=_YELLOW;
}
// this is color for possitve and negitivate
if(!m_bPre&& (m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==35
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==36
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==61
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==44
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==47
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==46
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==50
))
{
CString s;
s.Format("%s",sLabel);
int k=-1;
k=s.Find("$",0);
if(k!=-1)
s.Delete(k,1);
double f = atof(s);
if(f>0)
{
corB=_LGREEN;
}
else if(f<0)
{
corB=_LRED;
}
}
if(pHeaderCtrl->OrderToIndex(nColumn)==0)
{
pDC->DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE |
DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
else
{
pDC->FillSolidRect(rcCol, corB );
pDC->DrawText(sLabel, -1, rcLabel, DT_RIGHT | DT_SINGLELINE |
DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
// pDC->SetTextColor(cor);
// Draw labels for remaining columns
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH; if( m_nHighlight == 0 ) // Highlight only first column
{
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
}
rcBounds.right = rcHighlight.right > rcBounds.right ? rcHighlight.right :
rcBounds.right;
rgn.CreateRectRgnIndirect(&rcBounds);
pDC->SelectClipRgn(&rgn);
// lvc.mask=LVCF_TEXT;
int bkm=pDC->SetBkMode(OPAQUE);
int num=pHeaderCtrl->GetItemCount();
for(nColumn = 1;nColumn<num; nColumn++)
{
rListCtrl.GetColumn(pHeaderCtrl->OrderToIndex(nColumn), &lvc);
rcCol.left = rcCol.right;
rcCol.right += lvc.cx; // Draw the background if needed
/* if( m_nHighlight == HIGHLIGHT_NORMAL )
pDC->FillRect(rcCol, &CBrush(::GetSysColor(COLOR_WINDOW)));*/ sLabel = rListCtrl.GetItemText(nItem, pHeaderCtrl->OrderToIndex(nColumn));
if (sLabel.GetLength() == 0)
continue;
corB=corBack;
rcLabel = rcCol;
rcLabel.left += offset;
rcLabel.right -= offset;
cor=pDC->GetTextColor();
pDC->SetTextColor(_BLACK);
pDC->SetBkColor(corBack);
if(m_bPre && pHeaderCtrl->OrderToIndex(nColumn)==4)
{
CString s;
s.Format("%s",sLabel);
int k=s.Find("$",0);
s.Delete(k,1);
double f = atof(s);
if(f>0)
{
// cor=pDC->SetTextColor(_GREEN);
// corB=pDC->SetBkColor(_GREEN);
corB=LGREEN;
}
else if(f<0)
{
// cor=pDC->SetTextColor(_RED);
// corB=pDC->SetBkColor(_RED);
corB=LRED;
}
}
if(!m_bPre&& (m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==3
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==10))
{
char sh[60]={"1"},sl[60]={"2"},ps[60]={"3"};
pDoc->GetData(ps,3,nTemp);
pDoc->GetData(sh,1,nTemp);
pDoc->GetData(sl,2,nTemp);
if(strcmp(sh,ps)==0 && strcmp(sh,"")!=0)
corB=_YELLOW;
else if(strcmp(sl,ps)==0 && strcmp(sl,"")!=0)
corB=_YELLOW;
}
// this is color for possitve and negitivate
if(!m_bPre&& (m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==35
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==36
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==61
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==44
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==47
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==46
||m_intCol[pHeaderCtrl->OrderToIndex(nColumn)]==50
))
{
CString s;
s.Format("%s",sLabel);
int k=-1;
k=s.Find("$",0);
if(k!=-1)
s.Delete(k,1);
double f = atof(s);
if(f>0)
{
// cor=pDC->SetTextColor(_GREEN);
// corB=pDC->SetBkColor(_LGREEN);
corB=_LGREEN;
}
else if(f<0)
{
// cor=pDC->SetTextColor(_RED);
corB=_LRED;
}
}
if(pHeaderCtrl->OrderToIndex(nColumn)==0)
{
pDC->DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE |
DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
else
{
pDC->FillSolidRect(rcCol, corB );
pDC->DrawText(sLabel, -1, rcLabel, DT_RIGHT | DT_SINGLELINE |
DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
// pDC->SetTextColor(cor);
} // Draw focus rectangle if item has focus
/* if (lvi.state & LVIS_FOCUSED && (rListCtrl.GetFocus() == this))
pDC->DrawFocusRect(rcHighlight);*/
// Restore dc
pDC->SetBkMode(bkm);
pDC->RestoreDC( nSavedDC );
if (! bredraw) {
if (m_redrawcount++ <= 0) { clistctrl::setredraw(false); } } else { if (--m_redrawcount <="0)" { clistctrl::setredraw(true); m_redrawcount="0;" invalidate(); } } }
the first time you turn redraw off, the real setredraw function is called to turn redrawing off. subsequently the function increases a counter when you requested redraw be turned off, and decreases it when you request redraw to be turned on again. when the counter gets back down to zero, the real setredraw function is called to turn redrawing back on again and the control is invalidated (so that redrawing actually takes place). this means you can not put setredraw(false) at the start of, and setredraw(true) and the end of, any function that makes large changes to the lsit control (like loading, sorting, changing with column widths or order etc).