MemDCEx.h 2.8 KB

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