MemDC.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. private:
  26. CBitmap m_bitmap; // Offscreen bitmap
  27. CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
  28. CDC* m_pDC; // Saves CDC passed in constructor
  29. CRect m_rect; // Rectangle of drawing area.
  30. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
  31. public:
  32. CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
  33. {
  34. ASSERT(pDC != NULL);
  35. // Some initialization
  36. m_pDC = pDC;
  37. m_oldBitmap = NULL;
  38. m_bMemDC = !pDC->IsPrinting();
  39. // Get the rectangle to draw
  40. if (pRect == NULL) {
  41. pDC->GetClipBox(&m_rect);
  42. } else {
  43. m_rect = *pRect;
  44. }
  45. if (m_bMemDC) {
  46. // Create a Memory DC
  47. CreateCompatibleDC(pDC);
  48. pDC->LPtoDP(&m_rect);
  49. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  50. m_oldBitmap = SelectObject(&m_bitmap);
  51. SetMapMode(pDC->GetMapMode());
  52. pDC->DPtoLP(&m_rect);
  53. SetWindowOrg(m_rect.left, m_rect.top);
  54. } else {
  55. // Make a copy of the relevent parts of the current DC for printing
  56. m_bPrinting = pDC->m_bPrinting;
  57. m_hDC = pDC->m_hDC;
  58. m_hAttribDC = pDC->m_hAttribDC;
  59. }
  60. // Fill background
  61. FillSolidRect(m_rect, pDC->GetBkColor());
  62. }
  63. ~CMemDC()
  64. {
  65. if (m_bMemDC) {
  66. // Copy the offscreen bitmap onto the screen.
  67. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  68. this, m_rect.left, m_rect.top, SRCCOPY);
  69. //Swap back the original bitmap.
  70. SelectObject(m_oldBitmap);
  71. } else {
  72. // All we need to do is replace the DC with an illegal value,
  73. // this keeps us from accidently deleting the handles associated with
  74. // the CDC that was passed to the constructor.
  75. m_hDC = m_hAttribDC = NULL;
  76. }
  77. }
  78. // Allow usage as a pointer
  79. CMemDC* operator->()
  80. {
  81. return this;
  82. }
  83. // Allow usage as a pointer
  84. operator CMemDC*()
  85. {
  86. return this;
  87. }
  88. };
  89. #endif