MemDC.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // MemDC.h : header file
  2. //
  3. #ifndef MEMDC_H
  4. #define MEMDC_H
  5. //////////////////////////////////////////////////
  6. // CMemDC - memory DC
  7. //
  8. // Author: Keith Rule
  9. // Email: keithr@europa.com
  10. // Copyright 1996-1997, Keith Rule
  11. //
  12. // You may freely use or modify this code provided this
  13. // Copyright is included in all derived versions.
  14. //
  15. // History - 10/3/97 Fixed scrolling bug.
  16. // Added print support.
  17. // 25 feb 98 - fixed minor assertion bug
  18. //
  19. // This class implements a memory Device Context
  20. class CMemDC : public CDC
  21. {
  22. public:
  23. // constructor sets up the memory DC
  24. CMemDC(CDC* pDC) : CDC()
  25. {
  26. ASSERT(pDC != NULL);
  27. m_pDC = pDC;
  28. m_pOldBitmap = NULL;
  29. m_bMemDC = !pDC->IsPrinting();
  30. if (m_bMemDC) // Create a Memory DC
  31. {
  32. pDC->GetClipBox(&m_rect);
  33. CreateCompatibleDC(pDC);
  34. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  35. m_pOldBitmap = SelectObject(&m_bitmap);
  36. SetWindowOrg(m_rect.left, m_rect.top);
  37. }
  38. else // Make a copy of the relevent parts of the current DC for printing
  39. {
  40. m_bPrinting = pDC->m_bPrinting;
  41. m_hDC = pDC->m_hDC;
  42. m_hAttribDC = pDC->m_hAttribDC;
  43. }
  44. }
  45. // Destructor copies the contents of the mem DC to the original DC
  46. ~CMemDC()
  47. {
  48. if (m_bMemDC)
  49. {
  50. // Copy the offscreen bitmap onto the screen.
  51. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  52. this, m_rect.left, m_rect.top, SRCCOPY);
  53. //Swap back the original bitmap.
  54. SelectObject(m_pOldBitmap);
  55. } else {
  56. // All we need to do is replace the DC with an illegal value,
  57. // this keeps us from accidently deleting the handles associated with
  58. // the CDC that was passed to the constructor.
  59. m_hDC = m_hAttribDC = NULL;
  60. }
  61. }
  62. // Allow usage as a pointer
  63. CMemDC* operator->() {return this;}
  64. // Allow usage as a pointer
  65. operator CMemDC*() {return this;}
  66. private:
  67. CBitmap m_bitmap; // Offscreen bitmap
  68. CBitmap* m_pOldBitmap; // bitmap originally found in CMemDC
  69. CDC* m_pDC; // Saves CDC passed in constructor
  70. CRect m_rect; // Rectangle of drawing area.
  71. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
  72. };
  73. /////////////////////////////////////////////////////////////////////////////
  74. //{{AFX_INSERT_LOCATION}}
  75. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
  76. #endif //MEMDC_H