Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWndDC.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cWndDC_H
7 #define _INC_cWndDC_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cWndGDI.h"
13 #include "../Image/cSurfaceInfo.h"
14 
15 #ifdef _WIN32
16 
17 namespace GrayLib
18 {
19  class GRAYLIB_LINK cWndDC
20  {
24 
25  public:
26  static const int k_HIMETRIC_PER_INCH = 2540;
27  static const GChar_t* k_pszDisplayDef;
28  HDC m_hDC;
29 
30  public:
31  cWndDC(HDC hDC = WINHANDLE_NULL)
32  : m_hDC(hDC)
33  {
34  }
35 
36  static bool GRAYCALL IsRectOnScreen(const cRectI& rect);
37 
38  void DPFromHimetric(POINT* pt, ITERATE_t nCount);
39  void HimetricFromDP(POINT* pt, ITERATE_t nCount);
40 
41  bool isValidDC() const
42  {
43  return(m_hDC != WINHANDLE_NULL);
44  }
45  operator HDC() const
46  {
47  return m_hDC;
48  }
49 
50  // Create objects from the DC
51 
52  HGDIOBJ SelectObject(IN HGDIOBJ hGDIObj) const
53  {
54  ASSERT(m_hDC != WINHANDLE_NULL);
55  return ::SelectObject(m_hDC, hGDIObj);
56  }
57  HGDIOBJ SelectObject(const cWndGDI& GDIObj) const
58  {
59  ASSERT(m_hDC != WINHANDLE_NULL);
60  return ::SelectObject(m_hDC, GDIObj.m_hObject);
61  }
62  BOOL GetTextMetricsX(OUT _GTN(TEXTMETRIC)* lptm) const
63  {
64  return _GTN(::GetTextMetrics)(m_hDC, lptm);
65  }
66  int GetDeviceCaps(int index) const
67  {
69  return ::GetDeviceCaps(m_hDC, index);
70  }
71  int GetClipBox(OUT RECT* pRect) const
72  {
73  return ::GetClipBox(m_hDC, pRect);
74  }
75 
76  HBITMAP CreateCompatibleBitmap(PIXELS_t x, PIXELS_t y)
77  {
78  ASSERT(m_hDC != WINHANDLE_NULL);
79  return ::CreateCompatibleBitmap(m_hDC, x, y);
80  }
81  bool DrawIconEx(int x, int y, HICON hIcon, int cx, int cy)
82  {
83  // DI_DEFAULTSIZE
84  return ::DrawIconEx(m_hDC, x, y, hIcon, cx, cy, 0, WINHANDLE_NULL, DI_NORMAL); // DI_MASK
85  }
86 
87  void FillRect(const RECT& rRect, HBRUSH hBrush)
88  {
89  ::FillRect(m_hDC, &rRect, hBrush);
90  }
91  void FillSolidRect(const RECT& rRect, COLORREF clr)
92  {
93  cWndGDI brush;
94  brush.CreateSolidBrush(clr);
95  FillRect(rRect, brush.get_HBrush());
96  }
97 
98  // Text
99 
100  int SetBkMode(int iMode)
101  {
102  // OPAQUE or TRANSPARENT
103  return ::SetBkMode(m_hDC, iMode);
104  }
105  int SelectClipRgn(HRGN hrgn)
106  {
107  return ::SelectClipRgn(m_hDC, hrgn);
108  }
109  COLORREF SetBkColor(COLORREF crColor)
110  {
111  return ::SetBkColor(m_hDC, crColor);
112  }
113  COLORREF SetTextColor(COLORREF crColor)
114  {
115  return ::SetTextColor(m_hDC, crColor);
116  }
117 
118  bool GetOutputTextExtent(const GChar_t* pszText, StrLen_t iLenMax, OUT SIZE& rSize)
119  {
120  // cWinSize
121  if (iLenMax < 0)
122  iLenMax = StrT::Len(pszText);
123  return _GTN(::GetTextExtentPoint32)(m_hDC, pszText, iLenMax, &rSize);
124  }
125  CSize GetTextExtent(const GChar_t* pszText, StrLen_t iLenMax = -1)
126  {
127  // MFC support.
128  CSize rSize;
129  if (!GetOutputTextExtent(pszText, iLenMax, rSize))
130  {
131  rSize.cx = 0;
132  rSize.cy = 0;
133  }
134  return rSize;
135  }
136 
137  BOOL ExtTextOutX(int x, int y, UINT options, RECT* pRect, const GChar_t* lpString, const int* lpDx)
138  {
139  // Use SetBkMode, SetBkColor, SetTextColor
140  UINT nCount = StrT::Len(lpString);
141  return _GTN(::ExtTextOut)(m_hDC, x, y, options, pRect, lpString, nCount, lpDx);
142  }
143 
144  HPALETTE SelectPalette(HPALETTE hPal, bool bForceBkgd = false)
145  {
147  return ::SelectPalette(m_hDC, hPal, bForceBkgd);
148  }
149  UINT RealizePalette()
150  {
151  return ::RealizePalette(m_hDC);
152  }
153 
154  COLORREF GetPixel(PIXELS_t x, PIXELS_t y) const
155  {
156  return ::GetPixel(m_hDC, x, y);
157  }
158  int GetClipBox(RECT& rect) const
159  {
161  return ::GetClipBox(m_hDC, &rect);
162  }
163 
164  void DrawFocusRect(const RECT* pRect)
165  {
167  ::DrawFocusRect(m_hDC, pRect);
168  }
169 
170  BOOL Rectangle(int x1, int y1, int x2, int y2)
171  {
172  ASSERT(m_hDC != WINHANDLE_NULL);
173  return ::Rectangle(m_hDC, x1, y1, x2, y2);
174  }
175  BOOL MoveTo(int x, int y)
176  {
177  ASSERT(m_hDC != WINHANDLE_NULL);
178  BOOL bRet = ::MoveToEx(m_hDC, x, y, nullptr);
179  return bRet;
180  }
181  BOOL MoveTo(POINT point)
182  {
183  return MoveTo(point.x, point.y);
184  }
185  BOOL LineTo(int x, int y)
186  {
188  ASSERT(m_hDC != WINHANDLE_NULL);
189  return ::LineTo(m_hDC, x, y);
190  }
191  BOOL LineTo(POINT point)
192  {
193  return LineTo(point.x, point.y);
194  }
195  BOOL Ellipse(int x1, int y1, int x2, int y2)
196  {
197  ASSERT(m_hDC != WINHANDLE_NULL);
198  return ::Ellipse(m_hDC, x1, y1, x2, y2);
199  }
200  BOOL Polygon(const POINT* lpPoints, int nCount)
201  {
202  ASSERT(m_hDC != WINHANDLE_NULL);
203  return ::Polygon(m_hDC, lpPoints, nCount);
204  }
205 
206 #ifndef UNDER_CE
207  int GetPixelFormat() const
208  {
210  return ::GetPixelFormat(m_hDC);
211  }
212  int SetPixelFormat(const PIXELFORMATDESCRIPTOR& pfd)
213  {
216  int iPixelFormat = ::ChoosePixelFormat(m_hDC, &pfd);
217  if (iPixelFormat <= 0)
218  return -1;
219  // make that the pixel format of the device context
220  BOOL bRet = ::SetPixelFormat(m_hDC, iPixelFormat, &pfd);
221  if (!bRet)
222  return -1;
223  return iPixelFormat;
224  }
225 #endif
226  };
227 
228  class GRAYLIB_LINK cWndDCOwn : public CObject, public cWndDC
229  {
234 
235  public:
236  HWND m_hWndOwner;
237 
238  public:
239  cWndDCOwn(HDC hDC = WINHANDLE_NULL, HWND hWndOwner = HWND_BROADCAST)
240  : cWndDC(hDC)
241  , m_hWndOwner(hWndOwner)
242  {
245  }
246  cWndDCOwn(HWND hWndOwner)
247  : cWndDC(::GetDC(hWndOwner))
248  , m_hWndOwner(hWndOwner)
249  {
250  // HWND_DESKTOP
251  }
252  ~cWndDCOwn()
253  {
254  ReleaseDC();
255  }
256 
257  bool ReleaseDC();
258  bool AttachDC(HWND hWndOwner)
259  {
260  ReleaseDC();
261  m_hWndOwner = hWndOwner;
262  m_hDC = ::GetDC(hWndOwner);
263  // ASSERT( hWndOwner == ::WindowFromDC(m_hDC));
264  return isValidDC();
265  }
266  bool AttachDCEx(HWND hWndOwner, DWORD dwFlags)
267  {
269  ReleaseDC();
270  m_hWndOwner = hWndOwner;
271  m_hDC = ::GetDCEx(hWndOwner, WINHANDLE_NULL, dwFlags);
272  // ASSERT( hWndOwner == ::WindowFromDC(m_hDC));
273  return isValidDC();
274  }
275  bool CreateCompatibleDC(HDC hDCScreen)
276  {
277  ReleaseDC();
278  ASSERT(m_hWndOwner == HWND_BROADCAST);
279  if (hDCScreen == WINHANDLE_NULL)
280  {
281  // NOTE: CreateCompatibleDC(WINHANDLE_NULL) would otherwise create a weird B&W hDC
282  m_hDC = _GTN(::CreateDC)(k_pszDisplayDef, nullptr, nullptr, nullptr);
283  }
284  else
285  {
286  m_hDC = ::CreateCompatibleDC(hDCScreen);
287  }
288  return isValidDC();
289  }
290  bool CreateDCX(const GChar_t* pszDriver = nullptr, const GChar_t* pszDevice = nullptr)
291  {
294  if (pszDriver == nullptr)
295  {
296  pszDriver = k_pszDisplayDef;
297  }
298  ReleaseDC();
299  ASSERT(m_hWndOwner == HWND_BROADCAST);
300  m_hDC = _GTN(::CreateDC)(pszDriver, pszDevice, nullptr, nullptr);
301  return isValidDC();
302  }
303  };
304 
305  class GRAYLIB_LINK cWndPaint : public cWndDCOwn
306  {
312 
313  private:
314  PAINTSTRUCT m_ps;
315  private:
316  HDC m_hDCAlt;
317 
318  public:
319  cWndPaint(HWND hWndOwner, HDC hDCAlt = WINHANDLE_NULL);
320  ~cWndPaint();
321 
322  HDC get_DCAlt() const noexcept
323  {
324  return m_hDCAlt;
325  }
326  bool get_PaintErase() const noexcept
327  {
328  return m_ps.fErase;
329  }
330  RECT get_PaintRect() const noexcept
331  {
332  return m_ps.rcPaint;
333  }
334  };
335 
336 #ifndef _MFC_VER
337  typedef cWndDC CDC; // alias
338  typedef cWndPaint CPaintDC;
339 #endif
340 }
341 
342 #endif
343 #endif
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define _GTN(c)
_WIN32 name has a A or W for UTF8 or UNICODE (like _FNF)
Definition: StrConst.h:28
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define WINHANDLE_NULL
HWND, HPEN, etc are NOT OS Handles. like HWND_DESKTOP. like HANDLEPTR_NULL. This is a WINAPI void* ha...
Definition: cOSHandle.h:23
UINT32 COLORREF
ABGR (high to low bits)
Definition: cVariantData.h:21
Definition: cMesh.h:22
interface const RECTQ_t & rect
Definition: cQuadtree.h:44
int PIXELS_t
Count of pixels in a dimension of some image/surface.
Definition: cSurfaceInfo.h:20
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
uint16 index
Definition: sample3.cpp:29
static StrLen_t Len(const TYPE *pszStr) noexcept