Menu

[r1075]: / trunk / src / memdc.h  Maximize  Restore  History

Download this file

181 lines (154 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once
//////////////////////////////////////////////////
// CMemDC - memory DC
//
// Author: Keith Rule
// Email: keithr@europa.com
// Copyright 1996-1997, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// History - 10/3/97 Fixed scrolling bug.
// Added print support.
// 25 feb 98 - fixed minor assertion bug
//
// This class implements a memory Device Context
#ifdef _MFC_VER
class CMemDC : public CDC
{
public:
// constructor sets up the memory DC
CMemDC(CDC* pDC, bool bTempOnly = false, int nOffset = 0) : CDC()
{
ASSERT(pDC != NULL);
m_pDC = pDC;
m_pOldBitmap = NULL;
m_bMemDC = ((!pDC->IsPrinting()) && (!GetSystemMetrics(SM_REMOTESESSION)));
m_bTempOnly = bTempOnly;
if (m_bMemDC) // Create a Memory DC
{
pDC->GetClipBox(&m_rect);
CreateCompatibleDC(pDC);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width() - nOffset, m_rect.Height());
m_pOldBitmap = SelectObject(&m_bitmap);
SetWindowOrg(m_rect.left, m_rect.top);
}
else // Make a copy of the relevant parts of the current DC for printing
{
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
FillSolidRect(m_rect, pDC->GetBkColor());
}
CMemDC(CDC* pDC, const CRect* pRect) : CDC()
{
ASSERT(pDC != NULL);
// Some initialization
m_pDC = pDC;
m_pOldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting();
m_bTempOnly = false;
// Get the rectangle to draw
if (pRect == NULL) {
pDC->GetClipBox(&m_rect);
} else {
m_rect = *pRect;
}
if (m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_pOldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
SetWindowExt(pDC->GetWindowExt());
SetViewportExt(pDC->GetViewportExt());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
} else {
// Make a copy of the relevant parts of the current DC for printing
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
// Fill background
FillSolidRect(m_rect, pDC->GetBkColor());
}
// Destructor copies the contents of the mem DC to the original DC
~CMemDC()
{
if (m_bMemDC) {
// Copy the off screen bitmap onto the screen.
if (!m_bTempOnly)
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_pOldBitmap);
} else {
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidentally deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
}
// Allow usage as a pointer
CMemDC* operator->() {return this;}
// Allow usage as a pointer
operator CMemDC*() {return this;}
private:
CBitmap m_bitmap; // Off screen bitmap
CBitmap* m_pOldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
BOOL m_bTempOnly; // Whether to copy the contents on the real DC on destroy
};
#else
class CMemDC
{
public:
// constructor sets up the memory DC
CMemDC(HDC hDC, bool bTempOnly = false, int nOffset = 0)
{
UNREFERENCED_PARAMETER(nOffset);
m_hDC = hDC;
m_hOldBitmap = NULL;
m_bTempOnly = bTempOnly;
GetClipBox(m_hDC, &m_rect);
m_hMemDC = ::CreateCompatibleDC(m_hDC);
m_hBitmap = CreateCompatibleBitmap(m_hDC, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top);
m_hOldBitmap = (HBITMAP)SelectObject(m_hMemDC, m_hBitmap);
SetWindowOrgEx(m_hMemDC, m_rect.left, m_rect.top, NULL);
}
// Destructor copies the contents of the mem DC to the original DC
~CMemDC()
{
if (m_hMemDC) {
// Copy the off screen bitmap onto the screen.
if (!m_bTempOnly)
BitBlt(m_hDC, m_rect.left, m_rect.top, m_rect.right-m_rect.left, m_rect.bottom-m_rect.top, m_hMemDC, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_hMemDC, m_hOldBitmap);
DeleteObject(m_hBitmap);
DeleteDC(m_hMemDC);
} else {
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidentally deleting the handles associated with
// the CDC that was passed to the constructor.
DeleteObject(m_hBitmap);
m_hMemDC = NULL;
}
}
// Allow usage as a pointer
operator HDC() {return m_hMemDC;}
private:
HBITMAP m_hBitmap; // Off screen bitmap
HBITMAP m_hOldBitmap; // bitmap originally found in CMemDC
HDC m_hDC; // Saves CDC passed in constructor
HDC m_hMemDC; // our own memory DC
RECT m_rect; // Rectangle of drawing area.
bool m_bTempOnly; // Whether to copy the contents on the real DC on destroy
};
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.