MemDC.h
上传用户:tonybian
上传日期:2007-01-18
资源大小:328k
文件大小:3k
源码类别:

图形图象

开发平台:

WINDOWS

  1. #if !defined(AFX_CMemDC_H__F666A491_3847_11D3_A58E_00805FC1DE10__INCLUDED_)
  2. #define AFX_CMemDC_H__F666A491_3847_11D3_A58E_00805FC1DE10__INCLUDED_
  3. class CMemDC : public CDC {
  4. private:
  5. CBitmap m_bitmap; // Offscreen bitmap
  6. CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
  7. CDC* m_pDC; // Saves CDC passed in constructor
  8. CRect m_rect; // Rectangle of drawing area.
  9. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
  10. public:
  11. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  12. CMemDC(CDC* pDC, CRect rect = CRect(0,0,0,0)) : CDC(), m_oldBitmap(NULL), m_pDC(pDC)
  13. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. {
  15. ASSERT(m_pDC != NULL); // If you asserted here, you passed in a NULL CDC.
  16. m_bMemDC = !pDC->IsPrinting();
  17. if (m_bMemDC){
  18. // Create a Memory DC
  19. CreateCompatibleDC(pDC);
  20. if ( rect == CRect(0,0,0,0) )
  21. pDC->GetClipBox(&m_rect);
  22. else
  23. m_rect = rect;
  24. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  25. m_oldBitmap = SelectObject(&m_bitmap);
  26. SetWindowOrg(m_rect.left, m_rect.top);
  27. } else {
  28. // Make a copy of the relevent parts of the current DC for printing
  29. m_bPrinting = pDC->m_bPrinting;
  30. m_hDC = pDC->m_hDC;
  31. m_hAttribDC = pDC->m_hAttribDC;
  32. }
  33. }
  34. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  35. ~CMemDC()
  36. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. {
  38. if (m_bMemDC) {
  39. // Copy the offscreen bitmap onto the screen.
  40. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  41. this, m_rect.left, m_rect.top, SRCCOPY);
  42. //Swap back the original bitmap.
  43. SelectObject(m_oldBitmap);
  44. } else {
  45. // All we need to do is replace the DC with an illegal value,
  46. // this keeps us from accidently deleting the handles associated with
  47. // the CDC that was passed to the constructor.
  48. m_hDC = m_hAttribDC = NULL;
  49. }
  50. }
  51. // Allow usage as a pointer
  52. CMemDC* operator->() {return this;}
  53. // Allow usage as a pointer
  54. operator CMemDC*() {return this;}
  55. };
  56. #endif
  57. // End CMemDC
  58. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////