CDC::CreateCompatibleDC

创建与 pDC指定的设备兼容的一个单元设备上下文。

BOOL CreateCompatibleDC(
   CDC* pDC 
);

参数

  • pDC
    与设备上下文的指针。 如果 pDC 是 NULL,函数创建与系统显示兼容的一个单元设备上下文。

返回值

非零,如果函数运行成功;否则为0。

备注

存储设备上下文是表示显示产生内存块。 它可用于在复制它们。准备内存中的图像到物理计算机图面的计算机。

在存储设备上下文之后,GDI为其自动选择1 x 1纯色库存位图的。 仅当位图已创建并选中到该上下文,GDI输出函数可用于存储设备上下文。

此功能只能用于创建支持光栅操作的设备兼容的设备上下文。 请参见 CDC::BitBlt 成员函数有关的信息位块在设备上下文之间的调用。 若要确定设备上下文是否支持光栅操作,请参见在成员函数 CDC::GetDeviceCapsRC_BITBLT 光栅功能。

示例

// This handler loads a bitmap from system resources,
// centers it in the view, and uses BitBlt() to paint the bitmap
// bits.
void CDCView::DrawBitmap(CDC* pDC)
{
   // 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");
   }
}

要求

Header: afxwin.h

请参见

参考

CDC 类

层次结构图

CDC::CDC

CDC::GetDeviceCaps

CreateCompatibleDC

CDC::BitBlt

CDC::CreateDC

CDC::CreateIC

CDC::DeleteDC