void CXXXButton::OnPaint()
{
CPaintDC dc(this);
CRect rect;
this->GetClientRect(&rect);
int imageW = m_pImage->GetWidth(); //m_pImage你自己的成員變量,自己去定義吧。
int imageH = m_pImage->GetHeight();
CImage memImg; //創(chuàng)建CImage緩存
memImg.Create(rect.Width(), rect.Height(), m_pImage->GetBPP());
HDC tmpdc = memImg.GetDC();
CDC memDC;
memDC.Attach(tmpdc);
//使用 memDC 開始
memDC.FillSolidRect(rect, RGB(255, 255, 255));
m_pImage->Draw(tmpdc, 0, 0, imageW/m_nStates, imageH, imageW*0/m_nStates, 0, imageW/m_nStates, imageH);
//使用 memDC 結(jié)束
memImg.Draw(dc.m_hDC, 0, 0, rect.Width(), rect.Height()); //將CImage緩存貼到dc上
memDC.Detach(); //釋放這兩個,不釋放crash
memImg.ReleaseDC();
}