BOOL CMyDlg::OnEraseBkgnd(CDC* pDC) { // Set brush to desired background color CBrush backBrush(RGB(192, 192, 192));//can be a image // Save the old brush CBrush* pOldBrush = pDC->SelectObject(&backBrush); // Get the current clipping boundary CRect rect; pDC->GetClipBox(&rect); // Erase the area needed pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); pDC->SelectObject(pOldBrush); // Select the old brush back return TRUE; // message handled }
// TODO: Add your message handler code here HBITMAP hBitmap; hBitmap=(HBITMAP)LoadImage( ::AfxGetInstanceHandle(),"res\\background.bmp", IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION); ASSERT( hBitmap); HBITMAP hOldBitmap;
CDC MemDC; CRect rect; CDC *pDC=this->GetDC(); MemDC.CreateCompatibleDC(pDC); GetClientRect(rect); hOldBitmap=(HBITMAP)MemDC.SelectObject(hBitmap); pDC->BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY); MemDC.SelectObject(hOldBitmap); // Do not call CDialog::OnPaint() for painting messages }
BOOL CMyDlg::OnEraseBkgnd(CDC* pDC) { // Set brush to desired background color CBrush backBrush(RGB(192, 192, 192));//can be a image // Save the old brush CBrush* pOldBrush = pDC->SelectObject(&backBrush); // Get the current clipping boundary CRect rect; pDC->GetClipBox(&rect); // Erase the area needed pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); pDC->SelectObject(pOldBrush); // Select the old brush back return TRUE; // message handled }
Bitmap.LoadBitmap(IDB_1);
dcMemory.CreateCompatibleDC(pDC);
CBitmap* OldBitmap=dcMemory.SelectObject(&Bitmap);
pDC->BitBlt(0,0,495,495,&dcMemory,0,0,SRCCOPY);
Bitmap.DeleteObject();
dcMemory.SelectObject(OldBitmap);
dcMemory.DeleteDC();
ReleaseDC(pDC);
{
// Set brush to desired background color
CBrush backBrush(RGB(192, 192, 192));//can be a image // Save the old brush
CBrush* pOldBrush = pDC->SelectObject(&backBrush); // Get the current clipping boundary
CRect rect;
pDC->GetClipBox(&rect); // Erase the area needed
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),
PATCOPY); pDC->SelectObject(pOldBrush); // Select the old brush back
return TRUE; // message handled
}
{
CRect rect;
GetWindowRect(&rect); CDC memDC;
CBitmap m_bmpMain.LoadBitmap(IDB_BITMAP1);
CBitmap &cBitmap=m_bmpMain;
CBitmap* pOldMemBmp = NULL;
memDC.CreateCompatibleDC(pDC);
pOldMemBmp = memDC.SelectObject(&cBitmap);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
return TRUE;
}
则:
const char* lpszName = "c:\\test.bmp";
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL,
lpszName,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
CBitmap bmp;
bmp.Attach(hBitmap);
BITMAP bm;
bmp.GetBitmap(&bm);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap *pOldBmp = memDC.SelectObject(&bmp);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY); memDC.SelectObject(pOldBmp);
---------------------------------------
void CTestListDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
HBITMAP hBitmap; hBitmap=(HBITMAP)LoadImage(
::AfxGetInstanceHandle(),"res\\background.bmp",
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
ASSERT( hBitmap);
HBITMAP hOldBitmap;
CDC MemDC;
CRect rect;
CDC *pDC=this->GetDC();
MemDC.CreateCompatibleDC(pDC);
GetClientRect(rect);
hOldBitmap=(HBITMAP)MemDC.SelectObject(hBitmap);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(hOldBitmap); // Do not call CDialog::OnPaint() for painting messages
}
{
// Set brush to desired background color
CBrush backBrush(RGB(192, 192, 192));//can be a image // Save the old brush
CBrush* pOldBrush = pDC->SelectObject(&backBrush); // Get the current clipping boundary
CRect rect;
pDC->GetClipBox(&rect); // Erase the area needed
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),
PATCOPY); pDC->SelectObject(pOldBrush); // Select the old brush back
return TRUE; // message handled
}
HBITMAP bitmap1;
/////读取位图文件(转炉.bmp)
bitmap1=(HBITMAP)LoadImage(AfxGetInstanceHandle(),"转炉.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
ASSERT(bitmap1);
HBITMAP oldbitmap;
CDC memdc;CRect rect;
memdc.CreateCompatibleDC(pDC);
GetClientRect(rect);
oldbitmap=(HBITMAP)memdc.SelectObject(bitmap1);
///显示它
pDC->BitBlt(270,50,rect.Width()-20,rect.Height()-20,&memdc,0,0,SRCCOPY);
memdc.SelectObject(oldbitmap);
CBitmap m_MyBitmap;
m_MyBitmap.LoadBitmap (IDB_BITMAP1); BITMAP bmp;
m_MyBitmap.GetBitmap (&bmp); CRect rect;
pDC->GetClipBox(&rect); CPoint org(0,0);
pDC->DPtoLP(&org); CDC dcMem;
dcMem.CreateCompatibleDC (pDC);
CBitmap *pOldBitmap =dcMem.SelectObject (&m_MyBitmap);
dcMem.SetMapMode (pDC->GetMapMode());
pDC->StretchBlt (rect.left,rect.top,rect.Width(),rect.Height(),&dcMem,org.x,org.y,bmp.bmWidth, bmp.bmHeight, SRCCOPY);
pDC->SelectObject (pOldBitmap);