您的位置:首页 > 其它

How to draw bitmap on the form - 如何在窗体上画(贴)位图

2009-02-14 11:37 513 查看
1、Code



bool CPrjWebFoxExampleDlg::DrawPictureOnForm()
{
	CPaintDC dc(this); // device context for painting
	CBitmap bitmap;
	CDC dcBackGround;
	CRect rect;
	BITMAP bm;

	bitmap.LoadBitmap(IDB_picMeInShenZhenAutoShow);
	
	dcBackGround.CreateCompatibleDC(&dc);

	dcBackGround.SelectObject(&bitmap);
	
	bitmap.GetBitmap(&bm);

	// Center bitmap in client rectangle
	GetClientRect(&rect);
	int x = (rect.Width() - bm.bmWidth) / 2;
	int y = (rect.Height() - bm.bmHeight) / 2;

	// Draw the bitmap
	dc.StretchBlt(x,y,bm.bmWidth,bm.bmHeight,&dcBackGround,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
		
	return true;
}




2、Sample in the msdn



Example

// This OnDraw() handler loads a bitmap from system resources,
// centers it in the view, and uses BitBlt() to paint the bitmap
// bits.

void CBlat2View::OnDraw(CDC* pDC)
{
   CBlat2Doc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);

   // load IDB_BITMAP1 from our resources
   CBitmap bmp;
   if (bmp.LoadBitmap(IDB_BITMAP1))
   {
      // Get the size of the bitmap
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);

      // Create an in-memory DC compatible with the
      // display DC we're using to paint
      CDC dcMemory;
      dcMemory.CreateCompatibleDC(pDC);

      // Select the bitmap into the in-memory DC
      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

      // Find a centerpoint for the bitmap in the client area
      CRect rect;
      GetClientRect(&rect);
      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

      // Copy the bits from the in-memory DC into the on-
      // screen DC to actually do the painting. Use the centerpoint
      // we computed for the target offset.
      pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
         0, 0, SRCCOPY);

      dcMemory.SelectObject(pOldBitmap);
   }
   else
      TRACE0("ERROR: Where's IDB_BITMAP1?/n");
}


3、MSDN Hyperlink



http://msdn.microsoft.com/en-us/library/aa293677(VS.60).aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐