65.9K
CodeProject is changing. Read more.
Home

CBitmapDC - An automatic memory DC wrapper

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (12 votes)

Nov 25, 1999

viewsIcon

142705

downloadIcon

3676

A handy class that provides a memory bitmap device context

  • Download demo project - 18 Kb
  • Download source - 5 Kb
  • CBitmapDC is a subclass of CDC. It is a handy little class that provides a memory bitmap device context without having to reinvent the wheel every time you need one.

    Example 1

    CBitmapDC can be used to make a CBitmap on the fly
    1. make a memory bitmap DC by calling CBitmapDC's constructor with the desired bitmap width and height
    2. draw in this device context with normal CDC drawing functions
    3. call CBitmapDC's Close function, this returns a pointer to the finished CBitmap
    4. do whatever you want to do with the CBitmap
    5. delete the CBitmap
    void CMyView::OnDraw(CDC* pDC)
    {
      CBitmapDC bitmapDC(50, 50, pDC);
      bitmapDC.Rectangle(0, 0, 50, 50);
      bitmapDC.MoveTo(0,50);
      bitmapDC.LineTo(50,0);
      CBitmap *pbmp = bitmapDC.Close();
      DrawBitmap(pbmp, pDC, CPoint(10, 10));
      delete pbmp;
    }
    

    Example 2

    CBitmapDC can be used as a temporary scratchpad
    1. make a memory bitmap DC by calling CBitmapDC's constructor with the desired bitmap width and height
    2. draw in this device context with normal CDC drawing functions
    3. do whatever you want to do e.g. blit the memory DC to screen
    4. use the automatic cleanup of CBitmapDC's destructor
    void CMyView::OnDraw(CDC* pDC)
    {
      CBitmapDC bitmapDC_2(50, 50, pDC);
      bitmapDC_2.Rectangle(0, 0, 50, 50);
      bitmapDC_2.MoveTo(0, 0);
      bitmapDC_2.LineTo(50, 50);
      pDC->BitBlt(200, 10, 50, 50, &bitmapDC_2, 0, 0, SRCCOPY);
    }