neoWidgets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
DC.h
1 #ifndef NEO_DC_H
2 #define NEO_DC_H
3 
4 namespace neo
5 {
6 
14 {
15 public:
16  SaveRestoreDC(HDC pDC) { dc = pDC; saved = SaveDC(dc); }
17  ~SaveRestoreDC() { RestoreDC(dc, saved); }
18 private:
19  HDC dc;
20  int saved;
21 };
22 
27 {
28 public:
29  virtual ~DeviceContextIF() {}
30 
35  virtual HDC GetHDC() = 0;
36 
41  HGDIOBJ Select(HGDIOBJ object)
42  {
43  return SelectObject(GetHDC(), object);
44  }
45 
46  HBRUSH SetBrush(HBRUSH brush)
47  {
48  return reinterpret_cast<HBRUSH>(Select(brush));
49  }
50 
51  HPEN SetPen(HPEN pen)
52  {
53  return reinterpret_cast<HPEN>(Select(pen));
54  }
55 
56  HFONT SetFont(HFONT font)
57  {
58  return reinterpret_cast<HFONT>(Select(font));
59  }
60 
61  HRGN SetRegion(HRGN region)
62  {
63  return reinterpret_cast<HRGN>(Select(region));
64  }
65 
66  bool BitBlt(int x, int y, int width, int height, HDC sourceDC, int srcX, int srcY, DWORD rasterOp = SRCCOPY)
67  {
68  return ::BitBlt(GetHDC(), x, y, width, height,
69  sourceDC, srcX, srcY,
70  rasterOp) != FALSE;
71  }
72 
73  bool BitBlt(const RECT& rect, HDC sourceDC, const POINT& sourcePoint, DWORD rasterOp = SRCCOPY)
74  {
75  return ::BitBlt(GetHDC(), rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
76  sourceDC, sourcePoint.x, sourcePoint.y,
77  rasterOp) != FALSE;
78  }
79 
83  int SetStretchBltMode(int mode) { return ::SetStretchBltMode(GetHDC(), mode); }
84 
85  bool StretchBlt(int x, int y, int width, int height, HDC sourceDC, int srcX, int srcY, int srcWidth, int srcHeight, DWORD rasterOp = SRCCOPY)
86  {
87  return ::StretchBlt(GetHDC(), x, y, width, height,
88  sourceDC, srcX, srcY, srcWidth, srcHeight,
89  rasterOp) != FALSE;
90  }
91 
92  bool StretchBlt(const RECT& rect, HDC sourceDC, const RECT& sourceRect, DWORD rasterOp = SRCCOPY)
93  {
94  return ::StretchBlt(GetHDC(), rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
95  sourceDC, sourceRect.left, sourceRect.top, sourceRect.right - sourceRect.left, sourceRect.bottom - sourceRect.top,
96  rasterOp) != FALSE;
97  }
98 
99  void FillRect(const RECT& r, HBRUSH brush)
100  {
101  ::FillRect(GetHDC(), &r, brush);
102  }
103 
104  void FillRegion(HRGN region, HBRUSH brush)
105  {
106  ::FillRgn(GetHDC(), region, brush);
107  }
108 
113  {
114  SetBkMode(GetHDC(), OPAQUE);
115  }
116 
121  {
122  SetBkMode(GetHDC(), TRANSPARENT);
123  }
124 
125  void SetTextColor(COLORREF color)
126  {
127  ::SetTextColor(GetHDC(), color);
128  }
132  void SetBkColor(COLORREF color)
133  {
134  ::SetBkColor(GetHDC(), color);
135  }
136 
140  void DrawText(const tstring& text, RECT& boundingRect, UINT format = DT_LEFT|DT_TOP)
141  {
142  ::DrawText(GetHDC(), text.c_str(), -1, &boundingRect, format);
143  }
144 
145  void DrawText(const tstring& text, int x, int y)
146  {
147  Rect r(x, y, 1000000, 1000000);
148  DrawText(text, r);
149  }
150 
155  Size GetTextSize(const tstring& text, UINT format = DT_LEFT|DT_TOP, LONG maxWidth = 1000000)
156  {
157  // use DrawText to support multiline text
158  Rect r(0, 0, maxWidth, 0);
159  DrawText(text, r, format | DT_CALCRECT);
160  return r.GetSize();
161  }
162 };
163 
167 class DC : public DeviceContextIF
168 {
169 public:
170  DC(HDC pDC = 0) : dc(pDC) {}
171 
172  virtual HDC GetHDC() { return dc; }
173 
174  void operator=(HDC pDC) { dc = pDC; }
175  operator HDC() { return dc; }
176 
177  HDC dc;
178 };
179 
183 class PaintDC : public DeviceContextIF
184 {
185 public:
186  PaintDC(HWND pHwnd) : hwnd(pHwnd)
187  {
188  dc = BeginPaint(hwnd, &ps);
189  }
190  PaintDC(PaintDC&& rhs)
191  {
192  hwnd = rhs.hwnd;
193  dc = rhs.dc;
194  ps = rhs.ps;
195  rhs.hwnd = 0;
196  rhs.dc = 0;
197  ZeroMemory(&rhs.ps, sizeof(rhs.ps));
198  }
199  ~PaintDC()
200  {
201  EndPaint(hwnd, &ps);
202  }
203 
204  virtual HDC GetHDC() { return dc; }
205 
206  operator HDC() { return dc; }
207 private:
208  PaintDC(const PaintDC& rhs) {}
209 
210  HWND hwnd;
211  HDC dc;
212  PAINTSTRUCT ps;
213 };
214 
218 class ClientDC : public DeviceContextIF
219 {
220 public:
221  ClientDC(HWND win) : window(win) { dc = GetDC(window); }
222  ClientDC(ClientDC&& rhs)
223  {
224  window = rhs.window;
225  dc = rhs.dc;
226  rhs.window = 0;
227  rhs.dc = 0;
228  }
229  ~ClientDC() { ReleaseDC(window, dc); }
230 
231  virtual HDC GetHDC() { return dc; }
232 
233  operator HDC() { return dc; }
234 private:
235  ClientDC(const ClientDC& rhs) {}
236 
237  HWND window;
238  HDC dc;
239 };
240 
246 class MemoryDC : public DeviceContextIF
247 {
248 public:
250  MemoryDC(HBITMAP bitmap) { Init(ClientDC(0), bitmap); }
252  MemoryDC(HDC other, HBITMAP bitmap) { Init(other, bitmap); }
253  MemoryDC(MemoryDC&& rhs)
254  {
255  dc = rhs.dc;
256  attachedBitmap = rhs.attachedBitmap;
257  rhs.dc = 0;
258  rhs.attachedBitmap = 0;
259  }
260  ~MemoryDC() { if(dc) { RestoreDC(dc, -1); DeleteDC(dc); } }
261 
262  virtual HDC GetHDC() { return dc; }
263 
264  operator HDC() { return dc; }
265 
266  HBITMAP GetBitmap() { return attachedBitmap; }
267 private:
268  MemoryDC(const MemoryDC& rhs) {}
269  void Init(HDC other, HBITMAP bitmap);
270 
271  HDC dc;
272  HBITMAP attachedBitmap;
273 };
274 
275 inline void MemoryDC::Init(HDC other, HBITMAP bitmap)
276 {
277  attachedBitmap = bitmap;
278 
279  dc = CreateCompatibleDC(other);
280  assert(dc != 0);
281  if(dc)
282  {
283  SaveDC(dc);
284  SelectObject(dc, bitmap);
285  }
286 }
287 
288 } // namespace neo
289 
290 #endif
virtual HDC GetHDC()
Definition: DC.h:204
int SetStretchBltMode(int mode)
Definition: DC.h:83
virtual HDC GetHDC()
Definition: DC.h:262
Size GetTextSize(const tstring &text, UINT format=DT_LEFT|DT_TOP, LONG maxWidth=1000000)
Definition: DC.h:155
Wraps SaveDC in an object oriented way.
Definition: DC.h:13
void SetBkModeTransparent()
Definition: DC.h:120
A paint device context can only be called inside the WM_PAINT message handler.
Definition: DC.h:183
void DrawText(const tstring &text, RECT &boundingRect, UINT format=DT_LEFT|DT_TOP)
Definition: DC.h:140
MemoryDC(HBITMAP bitmap)
Definition: DC.h:250
MemoryDC(HDC other, HBITMAP bitmap)
Definition: DC.h:252
Interface for device context functions.
Definition: DC.h:26
Memory device contexts attach themselves to a bitmap.
Definition: DC.h:246
Unmanaged device context, to allow the user to call IDC functions on arbitrary DCs.
Definition: DC.h:167
Windows SIZE structure with constructor.
Definition: Rect.h:79
void SetBkModeOpaque()
Definition: DC.h:112
virtual HDC GetHDC()=0
HDC dc
Definition: DC.h:177
virtual HDC GetHDC()
Definition: DC.h:172
void SetBkColor(COLORREF color)
Definition: DC.h:132
virtual HDC GetHDC()
Definition: DC.h:231
Windows RECT structure with constructor.
Definition: Rect.h:108
A client device context allows drawing to the client area of a window.
Definition: DC.h:218
HGDIOBJ Select(HGDIOBJ object)
Definition: DC.h:41