MemDC.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef _WIN32_WCE
  44. SetWindowOrg(m_rect.left, m_rect.top);
  45. #endif
  46. }
  47. else // Make a copy of the relevent parts of the current DC for printing
  48. {
  49. #ifndef WCE_NO_PRINTING
  50. m_bPrinting = pDC->m_bPrinting;
  51. #endif
  52. m_hDC = pDC->m_hDC;
  53. m_hAttribDC = pDC->m_hAttribDC;
  54. }
  55. }
  56. // Destructor copies the contents of the mem DC to the original DC
  57. ~CMemDC()
  58. {
  59. if (m_bMemDC)
  60. {
  61. // Copy the offscreen bitmap onto the screen.
  62. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  63. this, m_rect.left, m_rect.top, SRCCOPY);
  64. //Swap back the original bitmap.
  65. SelectObject(m_pOldBitmap);
  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. CMemDC* operator->() {return this;}
  75. // Allow usage as a pointer
  76. operator CMemDC*() {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. };
  84. /////////////////////////////////////////////////////////////////////////////
  85. //{{AFX_INSERT_LOCATION}}
  86. // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
  87. #endif // !defined(AFX_MEMDC_H__CA1D3541_7235_11D1_ABBA_00A0243D1382__INCLUDED_)