MFC对话框用双缓冲之后子控件出现闪烁怎么解决?
//对话框类里面定义了下面这两个东西CDCm_cacheDC;//缓冲DCCBitmapm_cacheCBitmap;//缓冲位图//对话框onPaint函数voidCG...
//对话框类里面定义了下面这两个东西
CDC m_cacheDC; //缓冲DC
CBitmap m_cacheCBitmap; //缓冲位图
//对话框onPaint函数
void CGameDlg::OnPaint()
{
CDC *cDC=this->GetDC(); //获得当前窗口的DC
CDialog::OnPaint();
GetClientRect(&m_client);
//创建缓冲DC
m_cacheDC.CreateCompatibleDC(NULL);
m_cacheCBitmap.CreateCompatibleBitmap(cDC,m_client.Width(),m_client.Height());
// m_cacheCBitmap.CreateCompatibleBitmap();
m_cacheDC.SelectObject(&m_cacheCBitmap);
//绘制背景
m_back.Draw(m_cacheDC,m_client);
// ...... //接着绘制一堆东西
cDC->BitBlt(0,0,m_client.Width(),m_client.Height(),&m_cacheDC,0,0,SRCCOPY);
ValidateRect(&m_client); //即使在窗口被遮挡的情况下依然能够使得绘图区变得有效
m_cacheDC.DeleteDC();
//释放对象
m_cacheCBitmap.DeleteObject();
ReleaseDC(cDC); //释放DC
// TODO: 在此处添加消息处理程序代码
// ReleaseDC(pDC);
// 不为绘图消息调用 CDialogEx::OnPaint()
}
//下面是子按钮的DrawItem函数
void CMusicButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//从lpDrawItemStruct获取控件的相关信息
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
if (pDC->GetSafeHdc() == NULL)
return;
// int nSaveDC=pDC->SaveDC();
UINT state = lpDrawItemStruct->itemState;
//获取按钮的状态
m_bFocus = false;
if (state & ODS_FOCUS || state & ODS_SELECTED || state & ODS_DEFAULT)
{
m_bFocus = TRUE;
}
if (m_bFocus)
{
DrawImag(pDC, m_bmpPress, m_bmpMask);
}
else if (m_bPlay)
{
DrawImag(pDC, m_bmpPlay, m_bmpMask);
}
else
{ // 正常
DrawImag(pDC, m_bmpSilence, m_bmpMask);
}
}
我觉得应该是Onpaint里面用了缓冲DC而DrawItem里面没有用的问题,可是这该怎么解决呢? 展开
CDC m_cacheDC; //缓冲DC
CBitmap m_cacheCBitmap; //缓冲位图
//对话框onPaint函数
void CGameDlg::OnPaint()
{
CDC *cDC=this->GetDC(); //获得当前窗口的DC
CDialog::OnPaint();
GetClientRect(&m_client);
//创建缓冲DC
m_cacheDC.CreateCompatibleDC(NULL);
m_cacheCBitmap.CreateCompatibleBitmap(cDC,m_client.Width(),m_client.Height());
// m_cacheCBitmap.CreateCompatibleBitmap();
m_cacheDC.SelectObject(&m_cacheCBitmap);
//绘制背景
m_back.Draw(m_cacheDC,m_client);
// ...... //接着绘制一堆东西
cDC->BitBlt(0,0,m_client.Width(),m_client.Height(),&m_cacheDC,0,0,SRCCOPY);
ValidateRect(&m_client); //即使在窗口被遮挡的情况下依然能够使得绘图区变得有效
m_cacheDC.DeleteDC();
//释放对象
m_cacheCBitmap.DeleteObject();
ReleaseDC(cDC); //释放DC
// TODO: 在此处添加消息处理程序代码
// ReleaseDC(pDC);
// 不为绘图消息调用 CDialogEx::OnPaint()
}
//下面是子按钮的DrawItem函数
void CMusicButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//从lpDrawItemStruct获取控件的相关信息
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
if (pDC->GetSafeHdc() == NULL)
return;
// int nSaveDC=pDC->SaveDC();
UINT state = lpDrawItemStruct->itemState;
//获取按钮的状态
m_bFocus = false;
if (state & ODS_FOCUS || state & ODS_SELECTED || state & ODS_DEFAULT)
{
m_bFocus = TRUE;
}
if (m_bFocus)
{
DrawImag(pDC, m_bmpPress, m_bmpMask);
}
else if (m_bPlay)
{
DrawImag(pDC, m_bmpPlay, m_bmpMask);
}
else
{ // 正常
DrawImag(pDC, m_bmpSilence, m_bmpMask);
}
}
我觉得应该是Onpaint里面用了缓冲DC而DrawItem里面没有用的问题,可是这该怎么解决呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询