OSDN Git Service

Show Ignore Sub Menu
[tortoisegit/TortoiseGitJp.git] / src / Utils / MiscUI / MyMemDC.h
1 #pragma once\r
2 \r
3 //////////////////////////////////////////////////\r
4 // CMyMemDC - memory DC\r
5 //\r
6 // Author: Keith Rule\r
7 // Email:  keithr@europa.com\r
8 // Copyright 1996-1997, Keith Rule\r
9 //\r
10 // You may freely use or modify this code provided this\r
11 // Copyright is included in all derived versions.\r
12 //\r
13 // History - 10/3/97 Fixed scrolling bug.\r
14 //                   Added print support.\r
15 //           25 feb 98 - fixed minor assertion bug\r
16 //\r
17 // This class implements a memory Device Context\r
18 \r
19 #ifdef _MFC_VER\r
20 class CMyMemDC : public CDC\r
21 {\r
22 public:\r
23         // constructor sets up the memory DC\r
24         CMyMemDC(CDC* pDC, bool bTempOnly = false, int nOffset = 0) : CDC()\r
25     {\r
26                 ASSERT(pDC != NULL);\r
27                 \r
28                 m_pDC = pDC;\r
29                 m_pOldBitmap = NULL;\r
30         m_bMyMemDC = ((!pDC->IsPrinting()) && (!GetSystemMetrics(SM_REMOTESESSION)));\r
31                 m_bTempOnly = bTempOnly;\r
32                 \r
33         if (m_bMyMemDC) // Create a Memory DC\r
34                 {\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
40         }\r
41                 else            // Make a copy of the relevant parts of the current DC for printing\r
42                 {\r
43             m_bPrinting = pDC->m_bPrinting;\r
44             m_hDC               = pDC->m_hDC;\r
45             m_hAttribDC = pDC->m_hAttribDC;\r
46         }\r
47 \r
48                 FillSolidRect(m_rect, pDC->GetBkColor());\r
49         }\r
50         \r
51         CMyMemDC(CDC* pDC, const CRect* pRect) : CDC()\r
52         {\r
53                 ASSERT(pDC != NULL); \r
54 \r
55                 // Some initialization\r
56                 m_pDC = pDC;\r
57                 m_pOldBitmap = NULL;\r
58                 m_bMyMemDC = !pDC->IsPrinting();\r
59                 m_bTempOnly = false;\r
60 \r
61                 // Get the rectangle to draw\r
62                 if (pRect == NULL) {\r
63                         pDC->GetClipBox(&m_rect);\r
64                 } else {\r
65                         m_rect = *pRect;\r
66                 }\r
67 \r
68                 if (m_bMyMemDC) {\r
69                         // Create a Memory DC\r
70                         CreateCompatibleDC(pDC);\r
71                         pDC->LPtoDP(&m_rect);\r
72 \r
73                         m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());\r
74                         m_pOldBitmap = SelectObject(&m_bitmap);\r
75 \r
76                         SetMapMode(pDC->GetMapMode());\r
77 \r
78                         SetWindowExt(pDC->GetWindowExt());\r
79                         SetViewportExt(pDC->GetViewportExt());\r
80 \r
81                         pDC->DPtoLP(&m_rect);\r
82                         SetWindowOrg(m_rect.left, m_rect.top);\r
83                 } else {\r
84                         // Make a copy of the relevant parts of the current DC for printing\r
85                         m_bPrinting = pDC->m_bPrinting;\r
86                         m_hDC       = pDC->m_hDC;\r
87                         m_hAttribDC = pDC->m_hAttribDC;\r
88                 }\r
89 \r
90                 // Fill background \r
91                 FillSolidRect(m_rect, pDC->GetBkColor());\r
92         }\r
93         \r
94         // Destructor copies the contents of the mem DC to the original DC\r
95         ~CMyMemDC()\r
96     {\r
97                 if (m_bMyMemDC) {       \r
98                         // Copy the off screen bitmap onto the screen.\r
99                         if (!m_bTempOnly)\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
102                         \r
103             //Swap back the original bitmap.\r
104             SelectObject(m_pOldBitmap);\r
105                 } else {\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
110                 }\r
111         }\r
112         \r
113         // Allow usage as a pointer\r
114     CMyMemDC* operator->() {return this;}\r
115         \r
116     // Allow usage as a pointer\r
117     operator CMyMemDC*() {return this;}\r
118 \r
119 private:\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
126 };\r
127 #else\r
128 class CMyMemDC\r
129 {\r
130 public:\r
131 \r
132         // constructor sets up the memory DC\r
133         CMyMemDC(HDC hDC, bool bTempOnly = false, int nOffset = 0) \r
134         {       \r
135                 m_hDC = hDC;\r
136                 m_hOldBitmap = NULL;\r
137                 m_bTempOnly = bTempOnly;\r
138 \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
144         }\r
145 \r
146         // Destructor copies the contents of the mem DC to the original DC\r
147         ~CMyMemDC()\r
148         {\r
149                 if (m_hMyMemDC) {       \r
150                         // Copy the off screen bitmap onto the screen.\r
151                         if (!m_bTempOnly)\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
153 \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
158                 } else {\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
164                         m_hMyMemDC = NULL;\r
165                 }\r
166         }\r
167 \r
168         // Allow usage as a pointer\r
169         operator HDC() {return m_hMyMemDC;}\r
170 private:\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
177 };\r
178 \r
179 #endif\r
180 \r