<span style="font-size:14px;">绘图的过程放在OnDraw或者OnPaint函数中,OnPaint在进行屏幕显示时调用OnDraw。</span>
<span style="font-size:14px;">作图时发生闪烁的原因:</span>
<span style="font-size:14px;">当窗口需要重绘时,首先用背景色清除显示区,然后调用OnPaint,短时间内背景色与显示图像交替出现,使得显示窗口看起来在闪烁。</span>
<span style="font-size:14px;">将背景刷设置为NULL,重绘图形不会闪烁,但是窗口的显示会混乱。</span>
<span style="font-size:14px;">解决方案:</span>
<span style="font-size:14px;">MFC双缓冲:</span><span style="font-size:14px;"><span style="font-family:Tahoma;color:#555555;text-align: left; text-transform: none; line-height: 20px !important; text-indent: 0px; letter-spacing: normal; font-style: normal; font-variant: normal; font-weight: normal; word-spacing: 0px; white-space: normal; word-break: normal; word-wrap: normal; widows: 1; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;"><span style="line-height: 20px !important; word-break: normal; word-wrap: normal;">把要显示的图形先在内存中</span></span><span style="font-family:Tahoma;color:#555555;text-align: left; text-transform: none; line-height: 20px !important; text-indent: 0px; letter-spacing: normal; font-style: normal; font-variant: normal; font-weight: normal; word-spacing: 0px; white-space: normal; word-break: normal; word-wrap: normal; widows: 1; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;"><span style="line-height: 20px !important; word-break: normal; word-wrap: normal;">绘制好,然后再一次性的将内存中的图形按照一个点一个点地覆盖到屏幕上去</span></span></span>
void CDrawPicView::OnDraw(CDC* pDC)
{
CDrawPicDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CRect rect = 0;
GetClientRect(rect);
CDC MemDC; //首先定义一个内存显示设备对象
CBitmap MemBitmap; //定义一个位图对象
//建立与屏幕显示兼容的内存显示设备
MemDC.CreateCompatibleDC(NULL);
//建立一个与屏幕显示兼容的位图
MemBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
//只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
MemDC.SelectObject(&MemBitmap);
MemDC.FillSolidRect(0,0,rect.right,rect.bottom,RGB(255,255,255));
//绘图,添加你要的画图的代码,不过用MemDC画,而不是用pDC
/*...................*/
//将内存中的图拷贝到屏幕上进行显示
pDC->BitBlt(0,0,rect.right,rect.bottom,&MemDC,0,0,SRCCOPY);
//绘图完成后的清理
MemBitmap.DeleteObject();
MemDC.DeleteDC();
}