CreateCompatibleDC

2.x

  HDC CreateCompatibleDC(hdc)    
  HDC hdc; /* handle of device context */

The CreateCompatibleDC function creates a memory device context that is compatible with the given device.

An application must select a bitmap into a memory device context to represent a screen surface. The device context can then be used to prepare images in memory before copying them to the screen surface of the compatible device.

Parameters

hdc

Identifies the device context. If this parameter is NULL, the function creates a memory device context that is compatible with the system screen.

Return Value

The return value is the handle of the new memory device context if the function is successful. Otherwise, it is NULL.

Comments

The CreateCompatibleDC function can be used only to create compatible device contexts for devices that support raster operations. To determine whether a device supports raster operations, an application can call the GetDeviceCaps function with the RC_BITBLT index.

GDI output functions can be used with a memory device context only if a bitmap has been created and selected into that context.

When it has finished using a device context created by CreateCompatibleDC, an application should free the device context by calling the DeleteDC function. All objects selected into the device context after it was created should be selected out and replaced with the original objects before the device context is removed.

Example

The following example loads a bitmap named Dog, uses the CreateCompatibleDC function to create a memory device context that is compatible with the screen, selects the bitmap into the memory device context, and then uses the BitBlt function to move the bitmap from the memory device context to the screen device context:

HDC hdc, hdcMemory;
HBITMAP hbmpMyBitmap, hbmpOld;
BITMAP bm;

hbmpMyBitmap = LoadBitmap(hinst, "MyBitmap");
GetObject(hbmpMyBitmap, sizeof(BITMAP), &bm);


hdc = GetDC(hwnd);
hdcMemory = CreateCompatibleDC(hdc);
hbmpOld = SelectObject(hdcMemory, hbmpMyBitmap);

BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCCOPY);
SelectObject(hdcMemory, hbmpOld);

DeleteDC(hdcMemory);
ReleaseDC(hwnd, hdc);

See Also

DeleteDC, GetDeviceCaps