CPaintDC dc(this);

CRect rect;
this->GetClientRect(&rect);
//第一种方法
CDC pDC;
if(!pDC.CreateCompatibleDC(NULL)){
MessageBox("出错1");
return;
}
pDC.SelectObject(pBitmap[current]);
CMemDC mDC(&dc);
if(direction==RIGHT)
mDC.StretchBlt(0,0,rect.Width(),rect.Height(),&pDC,0,0,rect.Width(),rect.Height(),SRCCOPY);
else
mDC.StretchBlt(0,0,rect.Width(),rect.Height(),&pDC,rect.Width(),0,-rect.Width(),rect.Height(),SRCCOPY);
第二种方法
CDC pDC;
if(!pDC.CreateCompatibleDC(NULL)){
MessageBox("出错1");
return;
}
pDC.SelectObject(pBitmap[current]); CDC mDC;
if(!mDC.CreateCompatibleDC(NULL)){
MessageBox("出错2");
return;
}
CBitmap tempBitmap;
if(!tempBitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height()))
{
MessageBox("出错3");
return;
}
mDC.SelectObject(&tempBitmap);
mDC.FillSolidRect(0,0,rect.Width(),rect.Height(),RGB(255,255,255));

if(direction==RIGHT)
mDC.StretchBlt(0,0,rect.Width(),rect.Height(),&pDC,0,0,rect.Width(),rect.Height(),SRCCOPY);
else
mDC.StretchBlt(0,0,rect.Width(),rect.Height(),&pDC,rect.Width(),0,-rect.Width(),rect.Height(),SRCCOPY); dc.TransparentBlt(0,0,rect.Width(),rect.Height(),&mDC,0,0,rect.Width(),rect.Height(),RGB(255,255,255)); pDC.DeleteDC();
mDC.DeleteDC();小弟在实现动画的过程中使用上面两种双缓冲,可是画出的小人还是在闪,不知道为什么呵。请高手指点。用的是。NET
CMemDC是网上下的一个类代码如下
class CMemDC : public CDC
{
public:    // 构造函数,设置内存DC
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);        m_pDC = pDC;
        m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
              
        if (m_bMemDC)    // 创建内存DC
        {
            pDC->GetClipBox(&m_rect);
            CreateCompatibleDC(pDC);
            m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
            m_pOldBitmap = SelectObject(&m_bitmap);
            SetWindowOrg(m_rect.left, m_rect.top);
        }
        else        // 设置当前DC的备份,用于打印
{
            m_bPrinting = pDC->m_bPrinting;
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
    }
    
    // 析构函数,备份内存DC的内容
    ~CMemDC()
    {
        if (m_bMemDC) 
        {    
            // 将位图拷贝到屏幕
m_pDC->TransparentBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                          this, m_rect.left, m_rect.top,m_rect.Width(), m_rect.Height(), RGB(255,255,255));            SelectObject(m_pOldBitmap);
        } else {
            // All we need to do is replace the DC with an illegal value,
            // this keeps us from accidently deleting the handles associated with
            // the CDC that was passed to the constructor.
            m_hDC = m_hAttribDC = NULL;
        }
    }    // 允许作为指针使用
    CMemDC* operator->() {return this;}
        
    operator CMemDC*() {return this;}private:
    CBitmap  m_bitmap;      
    CBitmap* m_pOldBitmap;  
    CDC*     m_pDC;         
    CRect    m_rect;        
    BOOL     m_bMemDC;      
};

解决方案 »

  1.   

    .NET中还在用类以C开头的习惯?
      

  2.   

    这问题还是存在。
    OnEraseBkgnd()返回TRUE后,再画图时,不需双缓冲,因为直接用BitBlt贴位图,就已经没有闪烁了。我的问题的出现在于,我已经用SetLayeredWindowAttributes将窗口的RGB(192,192,192)颜色弄没了,所以窗口是不可见的。我再用TransparentBlt画人,如果在OnEraseBkgnd()里面RETURN TRUE的话,窗口出现黑色背景,且小人会有残影。。如何去掉残影,实现透明背景呢。高手,你在哪