MemDC.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef _MEMDC_H_
  2. #define _MEMDC_H_
  3. //////////////////////////////////////////////////
  4. // CMemDC - memory DC
  5. //
  6. // Author: Keith Rule
  7. // Email: keithr@europa.com
  8. // Copyright 1996-1999, Keith Rule
  9. //
  10. // You may freely use or modify this code provided this
  11. // Copyright is included in all derived versions.
  12. //
  13. // History - 10/3/97 Fixed scrolling bug.
  14. // Added print support. - KR
  15. //
  16. // 11/3/99 Fixed most common complaint. Added
  17. // background color fill. - KR
  18. //
  19. // 11/3/99 Added support for mapping modes other than
  20. // MM_TEXT as suggested by Lee Sang Hun. - KR
  21. //
  22. // This class implements a memory Device Context which allows
  23. // flicker free drawing.
  24. class CMemDC : public CDC
  25. {
  26. private:
  27. CBitmap m_bitmap; // Offscreen bitmap
  28. CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
  29. CDC* m_pDC; // Saves CDC passed in constructor
  30. CRect m_rect; // Rectangle of drawing area.
  31. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
  32. public:
  33. CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
  34. {
  35. ASSERT(pDC != NULL);
  36. // Some initialization
  37. m_pDC = pDC;
  38. m_oldBitmap = NULL;
  39. m_bMemDC = !pDC->IsPrinting();
  40. // Get the rectangle to draw
  41. if (pRect == NULL)
  42. {
  43. pDC->GetClipBox(&m_rect);
  44. } else
  45. {
  46. m_rect = *pRect;
  47. }
  48. if (m_bMemDC)
  49. {
  50. // Create a Memory DC
  51. CreateCompatibleDC(pDC);
  52. pDC->LPtoDP(&m_rect);
  53. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  54. m_oldBitmap = SelectObject(&m_bitmap);
  55. SetMapMode(pDC->GetMapMode());
  56. pDC->DPtoLP(&m_rect);
  57. SetWindowOrg(m_rect.left, m_rect.top);
  58. }
  59. else
  60. {
  61. // Make a copy of the relevent parts of the current DC for printing
  62. m_bPrinting = pDC->m_bPrinting;
  63. m_hDC = pDC->m_hDC;
  64. m_hAttribDC = pDC->m_hAttribDC;
  65. }
  66. // Fill background
  67. FillSolidRect(m_rect, pDC->GetBkColor());
  68. }
  69. ~CMemDC()
  70. {
  71. if (m_bMemDC)
  72. {
  73. // Copy the offscreen bitmap onto the screen.
  74. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  75. this, m_rect.left, m_rect.top, SRCCOPY);
  76. //Swap back the original bitmap.
  77. SelectObject(m_oldBitmap);
  78. }
  79. else
  80. {
  81. // All we need to do is replace the DC with an illegal value,
  82. // this keeps us from accidently deleting the handles associated with
  83. // the CDC that was passed to the constructor.
  84. m_hDC = m_hAttribDC = NULL;
  85. }
  86. }
  87. // Allow usage as a pointer
  88. CMemDC* operator->()
  89. {
  90. return this;
  91. }
  92. // Allow usage as a pointer
  93. operator CMemDC*()
  94. {
  95. return this;
  96. }
  97. };
  98. #endif