使用MFC中的CDC,CBitmap来实现贴图

代码如下:

void CProView::OnDraw(CDC* pDC)
{
 CProDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////贴图
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);

CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);

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

 // Find a centerpoint for the bitmap in the client area
 RECT rect;
 GetClientRect(&rect);

 // 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(0, 0, rect.right, rect.bottom, &dcMemory,0, 0, SRCCOPY);
 dcMemory.SelectObject(pOldBitmap);
}

cdc的方法CreateCompatibleDC:

BOOL CreateCompatibleDC(
   CDC* pDC 
);

Creates a memory device context that is compatible with the device specified by pDC.

根据一个dc创建一个相兼容的内存dc。

cdc bitblt:Copies a bitmap from the source device context to this current device context.

BOOL BitBlt( int x,    目标X坐标 int y,   目标Y坐标 int nWidth, 操作范围宽度 int nHeight,  操作范围高度

CDC* pSrcDC,源内容 int xSrc,   源X坐标 int ySrc,   源Y坐标 DWORD dwRop     光栅操作方式);

光栅操作经常是SRCCOPY:将源矩形区域直接拷贝到目标矩形区域。

pDC->BitBlt(0, 0, rect.right, rect.bottom, &dcMemory,0, 0, SRCCOPY);

上面的是以左上角为原点操作。可以找到当前视图中心点进行操作。

 CBitmap bmp;
  bmp.LoadBitmap(IDB_BITMAP1))
  
 Get the size of the bitmap
  BITMAP bmpInfo;
  bmp.GetBitmap(&bmpInfo);

 // 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);
参考:http://msdn.microsoft.com/en-US/library/kwxzck32(v=vs.80).aspx
原文地址:https://www.cnblogs.com/youxin/p/2855541.html