MemDC.h 3.1 KB

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