VC绘制控件如何防止闪烁
理论上不管什么控件都适用,方法如下:
新建一个MFC类,继承原来的控件类型,对于VC自动生成的类进行如下改动:
首先覆盖 OnEraseBkgnd() 防止擦除时填涂背景:
BOOL CXXXCtrl::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
{
return TRUE;
}
然后覆盖 OnPaint(),原理是先在内存DC中绘图,之后再一股脑将画出的东东填涂到窗体上。
代码
void CXXXCtrl::OnPaint()
{
CPaintDC dc(this);
// TODO: 在此处添加消息处理程序代码
CRect rect;
CRect headerRect;
CDC dcMem; //内存DC
CBitmap bmpMem;
GetClientRect(&rect);
dcMem.CreateCompatibleDC(&dc);
bmpMem.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
dcMem.SelectObject(&bmpMem);
dcMem.FillSolidRect(&rect, RGB(255,255,255));
//这一句是调用默认的OnPaint(),把图形画在内存DC上
DefWindowProc(WM_PAINT, (WPARAM)dcMem.m_hDC, (LPARAM)0);
//输出
dc.BitBlt(0,
0,
rect.Width(),
rect.Height(),
&dcMem,
0,
0,
SRCCOPY);
dcMem.DeleteDC();
bmpMem.DeleteObject();
}
{
CPaintDC dc(this);
// TODO: 在此处添加消息处理程序代码
CRect rect;
CRect headerRect;
CDC dcMem; //内存DC
CBitmap bmpMem;
GetClientRect(&rect);
dcMem.CreateCompatibleDC(&dc);
bmpMem.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
dcMem.SelectObject(&bmpMem);
dcMem.FillSolidRect(&rect, RGB(255,255,255));
//这一句是调用默认的OnPaint(),把图形画在内存DC上
DefWindowProc(WM_PAINT, (WPARAM)dcMem.m_hDC, (LPARAM)0);
//输出
dc.BitBlt(0,
0,
rect.Width(),
rect.Height(),
&dcMem,
0,
0,
SRCCOPY);
dcMem.DeleteDC();
bmpMem.DeleteObject();
}