// Listing2: DrawBitmap() procedure

BOOL FAR PASCAL DrawBitmap(HBITMAP hBitmap, HPALETTE hPal,
                                 HDC hDC, int x, int y)
//hBitmap = handle of bitmap to be drawn
//hPal = palette of bitmap, if necessary (may be NULL)
//hDC = device context bitmap to be drawn on
//x, y = location bitmap to be drawn on device context
{
  BITMAP bitmap;        //structure to hold bitmap statistics
  HDC    hDCMem;        //memory DC to place bitmap in
  POINT  bSize, origin;

  if (!hBitmap || !hDC) {
    //ERROR: invalid parameter
    return FALSE;
  }

  //create a compatible memory dc to place bitmap in
  hDCMem = CreateCompatibleDC(hDC);

  //select bitmap into memory dc
  SelectObject(hDCMem, hBitmap);

  //set mapping modes to be equivalent
  SetMapMode(hDCMem, GetMapMode(hDC));

  //get statistics of bitmap
  GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bitmap);

  //convert size and origin of bitmap to logical coordinates
  bSize.x = bitmap.bmWidth;
  bSize.y = bitmap.bmHeight;
  DPtoLP(hDC, &bSize,1);

  origin.x = origin.y = 0;
  DPtoLP(hDCMem,&origin,1);

  //if a palette is provided, set dc to use it
  if (hPal) {
    SelectPalette(hDC, hPal, FALSE);
    RealizePalette(hDC);
  }

  //draw bitmap onto passed in device context
  BitBlt(hDC, x, y, bSize.x, bSize.y, hDCMem,
                    origin.x, origin.y, SRCCOPY);

  //cleanup
  DeleteDC(hDCMem);

  return TRUE;
}

