3 //////////////////////////////////////////////////
\r
4 // CMyMemDC - memory DC
\r
6 // Author: Keith Rule
\r
7 // Email: keithr@europa.com
\r
8 // Copyright 1996-1997, Keith Rule
\r
10 // You may freely use or modify this code provided this
\r
11 // Copyright is included in all derived versions.
\r
13 // History - 10/3/97 Fixed scrolling bug.
\r
14 // Added print support.
\r
15 // 25 feb 98 - fixed minor assertion bug
\r
17 // This class implements a memory Device Context
\r
20 class CMyMemDC : public CDC
\r
23 // constructor sets up the memory DC
\r
24 CMyMemDC(CDC* pDC, bool bTempOnly = false, int nOffset = 0) : CDC()
\r
26 ASSERT(pDC != NULL);
\r
29 m_pOldBitmap = NULL;
\r
30 m_bMyMemDC = ((!pDC->IsPrinting()) && (!GetSystemMetrics(SM_REMOTESESSION)));
\r
31 m_bTempOnly = bTempOnly;
\r
33 if (m_bMyMemDC) // Create a Memory DC
\r
35 pDC->GetClipBox(&m_rect);
\r
36 CreateCompatibleDC(pDC);
\r
37 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width() - nOffset, m_rect.Height());
\r
38 m_pOldBitmap = SelectObject(&m_bitmap);
\r
39 SetWindowOrg(m_rect.left, m_rect.top);
\r
41 else // Make a copy of the relevant parts of the current DC for printing
\r
43 m_bPrinting = pDC->m_bPrinting;
\r
45 m_hAttribDC = pDC->m_hAttribDC;
\r
48 FillSolidRect(m_rect, pDC->GetBkColor());
\r
51 CMyMemDC(CDC* pDC, const CRect* pRect) : CDC()
\r
53 ASSERT(pDC != NULL);
\r
55 // Some initialization
\r
57 m_pOldBitmap = NULL;
\r
58 m_bMyMemDC = !pDC->IsPrinting();
\r
59 m_bTempOnly = false;
\r
61 // Get the rectangle to draw
\r
62 if (pRect == NULL) {
\r
63 pDC->GetClipBox(&m_rect);
\r
69 // Create a Memory DC
\r
70 CreateCompatibleDC(pDC);
\r
71 pDC->LPtoDP(&m_rect);
\r
73 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
\r
74 m_pOldBitmap = SelectObject(&m_bitmap);
\r
76 SetMapMode(pDC->GetMapMode());
\r
78 SetWindowExt(pDC->GetWindowExt());
\r
79 SetViewportExt(pDC->GetViewportExt());
\r
81 pDC->DPtoLP(&m_rect);
\r
82 SetWindowOrg(m_rect.left, m_rect.top);
\r
84 // Make a copy of the relevant parts of the current DC for printing
\r
85 m_bPrinting = pDC->m_bPrinting;
\r
87 m_hAttribDC = pDC->m_hAttribDC;
\r
91 FillSolidRect(m_rect, pDC->GetBkColor());
\r
94 // Destructor copies the contents of the mem DC to the original DC
\r
98 // Copy the off screen bitmap onto the screen.
\r
100 m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
\r
101 this, m_rect.left, m_rect.top, SRCCOPY);
\r
103 //Swap back the original bitmap.
\r
104 SelectObject(m_pOldBitmap);
\r
106 // All we need to do is replace the DC with an illegal value,
\r
107 // this keeps us from accidentally deleting the handles associated with
\r
108 // the CDC that was passed to the constructor.
\r
109 m_hDC = m_hAttribDC = NULL;
\r
113 // Allow usage as a pointer
\r
114 CMyMemDC* operator->() {return this;}
\r
116 // Allow usage as a pointer
\r
117 operator CMyMemDC*() {return this;}
\r
120 CBitmap m_bitmap; // Off screen bitmap
\r
121 CBitmap* m_pOldBitmap; // bitmap originally found in CMyMemDC
\r
122 CDC* m_pDC; // Saves CDC passed in constructor
\r
123 CRect m_rect; // Rectangle of drawing area.
\r
124 BOOL m_bMyMemDC; // TRUE if CDC really is a Memory DC.
\r
125 BOOL m_bTempOnly; // Whether to copy the contents on the real DC on destroy
\r
132 // constructor sets up the memory DC
\r
133 CMyMemDC(HDC hDC, bool bTempOnly = false, int nOffset = 0)
\r
136 m_hOldBitmap = NULL;
\r
137 m_bTempOnly = bTempOnly;
\r
139 GetClipBox(m_hDC, &m_rect);
\r
140 m_hMyMemDC = ::CreateCompatibleDC(m_hDC);
\r
141 m_hBitmap = CreateCompatibleBitmap(m_hDC, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top);
\r
142 m_hOldBitmap = (HBITMAP)SelectObject(m_hMyMemDC, m_hBitmap);
\r
143 SetWindowOrgEx(m_hMyMemDC, m_rect.left, m_rect.top, NULL);
\r
146 // Destructor copies the contents of the mem DC to the original DC
\r
150 // Copy the off screen bitmap onto the screen.
\r
152 BitBlt(m_hDC, m_rect.left, m_rect.top, m_rect.right-m_rect.left, m_rect.bottom-m_rect.top, m_hMyMemDC, m_rect.left, m_rect.top, SRCCOPY);
\r
154 //Swap back the original bitmap.
\r
155 SelectObject(m_hMyMemDC, m_hOldBitmap);
\r
156 DeleteObject(m_hBitmap);
\r
157 DeleteDC(m_hMyMemDC);
\r
159 // All we need to do is replace the DC with an illegal value,
\r
160 // this keeps us from accidentally deleting the handles associated with
\r
161 // the CDC that was passed to the constructor.
\r
162 DeleteObject(m_hBitmap);
\r
163 DeleteDC(m_hMyMemDC);
\r
168 // Allow usage as a pointer
\r
169 operator HDC() {return m_hMyMemDC;}
\r
171 HBITMAP m_hBitmap; // Off screen bitmap
\r
172 HBITMAP m_hOldBitmap; // bitmap originally found in CMyMemDC
\r
173 HDC m_hDC; // Saves CDC passed in constructor
\r
174 HDC m_hMyMemDC; // our own memory DC
\r
175 RECT m_rect; // Rectangle of drawing area.
\r
176 bool m_bTempOnly; // Whether to copy the contents on the real DC on destroy
\r