memdc.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // CMemDC - memory DC
  3. //
  4. // Author: Keith Rule, [email protected], Copyright 1996-1997, Keith Rule
  5. //
  6. // You may freely use or modify this code provided this copyright is included in all derived versions.
  7. //
  8. // History - 10/3/97 Fixed scrolling bug.
  9. // Added print support.
  10. // - 14/7/99 Added optional clip rect parameter [jgh]
  11. //
  12. // - 06/06/08 Added option to copy screen on construction
  13. //
  14. #if !defined(AFX_CMemDC_H__F666A491_3847_11D3_A58E_00805FC1DE10__INCLUDED_)
  15. #define AFX_CMemDC_H__F666A491_3847_11D3_A58E_00805FC1DE10__INCLUDED_
  16. class CMemDCEx : public CDC {
  17. private:
  18. CBitmap m_bitmap; // Offscreen bitmap
  19. CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
  20. CDC* m_pDC; // Saves CDC passed in constructor
  21. CRect m_rect; // Rectangle of drawing area.
  22. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
  23. public:
  24. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  25. CMemDCEx(CDC* pDC, CRect rect = CRect(0,0,0,0), BOOL bCopyFirst = FALSE) : CDC(), m_oldBitmap(NULL), m_pDC(pDC)
  26. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. {
  28. ASSERT(m_pDC != NULL); // If you asserted here, you passed in a NULL CDC.
  29. m_bMemDC = !pDC->IsPrinting();
  30. if (m_bMemDC){
  31. // Create a Memory DC
  32. CreateCompatibleDC(pDC);
  33. if ( rect == CRect(0,0,0,0) )
  34. pDC->GetClipBox(&m_rect);
  35. else
  36. m_rect = rect;
  37. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  38. m_oldBitmap = SelectObject(&m_bitmap);
  39. SetWindowOrg(m_rect.left, m_rect.top);
  40. if(bCopyFirst)
  41. {
  42. this->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  43. m_pDC, m_rect.left, m_rect.top, SRCCOPY);
  44. }
  45. } else {
  46. // Make a copy of the relevent parts of the current DC for printing
  47. m_bPrinting = pDC->m_bPrinting;
  48. m_hDC = pDC->m_hDC;
  49. m_hAttribDC = pDC->m_hAttribDC;
  50. }
  51. }
  52. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Function Header
  53. ~CMemDCEx()
  54. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. {
  56. if (m_bMemDC) {
  57. // Copy the offscreen bitmap onto the screen.
  58. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  59. this, m_rect.left, m_rect.top, SRCCOPY);
  60. //Swap back the original bitmap.
  61. SelectObject(m_oldBitmap);
  62. } else {
  63. // All we need to do is replace the DC with an illegal value,
  64. // this keeps us from accidently deleting the handles associated with
  65. // the CDC that was passed to the constructor.
  66. m_hDC = m_hAttribDC = NULL;
  67. }
  68. }
  69. // Allow usage as a pointer
  70. CMemDCEx* operator->() {return this;}
  71. // Allow usage as a pointer
  72. operator CMemDCEx*() {return this;}
  73. };
  74. #endif
  75. // End CMemDC
  76. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////