CDC::CreateCompatibleDC

更新 : 2007 年 11 月

pDC により指定されるデバイスと互換性のあるメモリ デバイス コンテキストを作成します。

BOOL CreateCompatibleDC(
   CDC* pDC 
);

パラメータ

  • pDC
    デバイス コンテキストへのポインタ。pDC が NULL のときは、システム ディスプレイと互換性のあるメモリ デバイス コンテキストが作成されます。

戻り値

正常終了した場合は 0 以外を返します。それ以外の場合は 0 を返します。

解説

メモリ デバイス コンテキストは、ディスプレイ表面を表すメモリのブロックです。メモリ デバイス コンテキストは、デバイスの実際のディスプレイ表面にコピーするためのイメージをメモリ内に準備するために使います。

メモリ デバイス コンテキストが作成されると、GDI はそのメモリ デバイス コンテキストに対して 1 × 1 のモノクロのストック ビットマップを自動的に選択します。ビットマップが作成され、コンテキストに対して選択されているときだけ、メモリ デバイス コンテキストに、GDI 出力関数を使うことができます。

この関数は、ラスタ オペレーションをサポートするデバイスと互換性のあるデバイス コンテキストを作成するときにだけ使われます。デバイス コンテキスト間のビットのブロック転送に関する情報は、CDC::BitBlt メンバ関数を参照してください。デバイス コンテキストがラスタ オペレーションをサポートするかどうかを調べるには、CDC::GetDeviceCaps メンバ関数の RC_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");
   }
}

必要条件

ヘッダー : afxwin.h

参照

参照

CDC クラス

階層図

CDC::CDC

CDC::GetDeviceCaps

CreateCompatibleDC

CDC::BitBlt

CDC::CreateDC

CDC::CreateIC

CDC::DeleteDC

その他の技術情報

CDC のメンバ