VC 背景填充位图的几种方法
IDB_BG 是位图资源的ID
方法一
OnEraseBkgnd(CDC* pDC) { // 设置画刷为希望的背景 CDC backBmp; backBmp.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.LoadBitmap(IDB_BG); CBrush m_brushBackground; m_brushBackground.CreatePatternBrush(&bitmap); ///创建位图画刷 CBrush* pOldMemDCBrush=pDC->SelectObject(&m_brushBackground); pDC->PatBlt(0,0,m_ScrollRect.Width(),m_ScrollRect.Height(),PATCOPY); pDC->SelectObject(pOldMemDCBrush); return TRUE; }
方法二:
OnEraseBkgnd(CDC* pDC) { CBitmap m_pMap; m_pMap.LoadBitmap(IDB_BG); CRect rect; GetClientRect(&rect); BITMAP bm; CDC dcMem; m_pMap.GetObject(sizeof(BITMAP),&bm); dcMem.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = dcMem.SelectObject(&m_pMap); for (register int nX=0;nX<rect.right-rect.left;nX+=bm.bmWidth) for(int nY=0;nY<rect.bottom-rect.top;nY+=bm.bmHeight) pDC->BitBlt(nX,nY,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY); dcMem.SelectObject(pOldBitmap); return TRUE; }
方法三:
OnEraseBkgnd(CDC* pDC) { CBitmap bmp; bmp.LoadBitmap(IDB_BG); ///加载位图 CBrush m_brushBackground; m_brushBackground.CreatePatternBrush(&bmp); ///创建位图画刷 CRect rect; GetClientRect(&rect); pDC->FillRect(rect,&m_brushBackground); //用背景画填充区域 return TRUE; }
参考资料:
http://blog.163.com/tiger_zl2005/blog/static/5632972006631231250/