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