Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWndGDI.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cWndGDI_H
7 #define _INC_cWndGDI_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
13 
14 #ifdef _WIN32
15 #include "../Geometry/cRectI.h"
22 
23 namespace Gray
24 {
25  template <> inline void cHandlePtr<HMENU>::CloseHandle(HMENU h) // static
26  {
28  ::DestroyMenu(h);
29  }
30  template <> inline void cHandlePtr<HICON>::CloseHandle(HICON h) // static
31  {
34  ::DestroyIcon(h);
35  }
36 }
37 
38 namespace GrayLib
39 {
40  class cWndDC;
41 
42  class GRAYLIB_LINK cWndGDI : public CObject
43  {
48 
49  public:
50  HGDIOBJ m_hObject; // cHandlePtr<HGDIOBJ>
51 
52  protected:
53  void DeleteObjectLast() noexcept
54  {
55  if (m_hObject != WINHANDLE_NULL)
56  {
57  ::DeleteObject(m_hObject);
58  }
59  }
60 
61  public:
62  cWndGDI(HGDIOBJ hGDIObj = WINHANDLE_NULL) noexcept
63  : m_hObject(hGDIObj)
64  {
65  }
66 #ifndef STRICT
67  cWndGDI(HBITMAP h) noexcept
68  : m_hObject((HGDIOBJ)h)
69  {
70  }
71 #endif
72  ~cWndGDI() noexcept
73  {
74  DeleteObjectLast();
75  }
76 
77  void Attach(HGDIOBJ hObject) noexcept
78  {
79  if (hObject != m_hObject)
80  {
81  DeleteObjectLast();
82  m_hObject = hObject;
83  }
84  }
85  void Detach() noexcept
86  {
87  m_hObject = WINHANDLE_NULL;
88  }
89  HGDIOBJ DetachRet() noexcept
90  {
91  HGDIOBJ hObject = m_hObject;
92  m_hObject = WINHANDLE_NULL;
93  return hObject;
94  }
95 
96  void DeleteObject() noexcept
97  {
98  if (m_hObject != WINHANDLE_NULL)
99  {
100  ::DeleteObject(m_hObject);
101  m_hObject = WINHANDLE_NULL;
102  }
103  }
104 
105  DWORD GetObjectType() const noexcept
106  {
108  return ::GetObjectType(m_hObject);
109  }
110  int GetObjectX(size_t iSizeMax, void* pData) const noexcept
111  {
116  return _GTN(::GetObject)(m_hObject, (int)iSizeMax, pData);
117  }
118  bool GetBitmap(BITMAP* pBitmap) const noexcept
119  {
122  return GetObjectX(sizeof(BITMAP), (char*)pBitmap) == sizeof(BITMAP) ;
123  }
124 
125  HGDIOBJ get_Handle() const noexcept
126  {
128  return m_hObject;
129  }
130  operator HGDIOBJ() const noexcept
131  {
132  return m_hObject;
133  }
134  bool isValidHandle() const noexcept
135  {
136  return m_hObject != WINHANDLE_NULL ;
137  }
138 
139  HBRUSH get_HBrush() const noexcept
140  {
141  return (HBRUSH)m_hObject;
142  }
143  HPEN get_HPen() const noexcept
144  {
145  return (HPEN)m_hObject;
146  }
147  HRGN get_HRgn() const noexcept
148  {
149  return (HRGN)m_hObject;
150  }
151  HBITMAP get_HBitmap() const noexcept
152  {
153  return (HBITMAP)m_hObject;
154  }
155  HFONT get_HFont() const noexcept
156  {
157  return (HFONT)m_hObject;
158  }
159 
160  HBRUSH CreateSolidBrush(COLORREF color) noexcept
161  {
162  Attach((HGDIOBJ) ::CreateSolidBrush(color));
163  return get_HBrush();
164  }
165  HPEN CreatePen(int iStyle, int cWidth, COLORREF color) noexcept
166  {
168  Attach((HGDIOBJ) ::CreatePen(iStyle, cWidth, color));
169  return get_HPen();
170  }
171 
172 #if 0
173  // use cWndBitmap
174  HBITMAP CreateCompatibleBitmap(HDC hDC, int cx, int cy)
175  {
176  Attach((HGDIOBJ) ::CreateCompatibleBitmap(hDC, cx, cy));
177  return get_HBitmap();
178  }
179 #endif
180  };
181 
182  template <class _TYPE_HANDLE = HGDIOBJ>
183  class cWndGDIT : public cWndGDI
184  {
188 
189  typedef cWndGDI SUPER_t;
190 
191  public:
192  cWndGDIT(_TYPE_HANDLE h = WINHANDLE_NULL) noexcept
193  : cWndGDI((HGDIOBJ)h)
194  {
195  }
196  _TYPE_HANDLE get_Handle() const
197  {
199  return (_TYPE_HANDLE)m_hObject;
200  }
201  operator _TYPE_HANDLE() const
202  {
203  return (_TYPE_HANDLE)m_hObject;
204  }
205  };
206 
207  class GRAYLIB_LINK cWndBrush : public cWndGDIT<HBRUSH>
208  {
211 
212  typedef cWndGDIT<HBRUSH> SUPER_t;
213 
214  public:
215  cWndBrush(HBRUSH h = WINHANDLE_NULL) noexcept
216  : cWndGDIT<HBRUSH>(h)
217  {
218  }
219  cWndBrush(COLORREF color)
220  : cWndGDIT<HBRUSH>(::CreateSolidBrush(color))
221  {
222  }
223  };
224 
225  class GRAYLIB_LINK cWndPen : public cWndGDIT<HPEN>
226  {
229 
230  typedef cWndGDIT<HPEN> SUPER_t;
231 
232  public:
233  cWndPen(HPEN h = WINHANDLE_NULL) noexcept
234  : cWndGDIT<HPEN>(h)
235  {
236  }
237  cWndPen(int iStyle, int cWidth, COLORREF color)
238  : cWndGDIT<HPEN>(::CreatePen(iStyle, cWidth, color))
239  {
240  }
241  };
242 
243  class GRAYLIB_LINK cWndFont : public cWndGDIT<HFONT>
244  {
248 
249  typedef cWndGDIT<HFONT> SUPER_t;
250 
251  public:
252  cWndFont(HFONT h = WINHANDLE_NULL) noexcept
253  : cWndGDIT<HFONT>(h)
254  {
255  }
256  int GetLogFont(_GTN(LOGFONT)& lf) const noexcept
257  {
260  return GetObjectX(sizeof(_GTN(LOGFONT)), &lf);
261  }
262  HFONT CreateFontIndirectX(const _GTN(LOGFONT)* pLogFont) noexcept
263  {
265  Attach((HGDIOBJ)_GTN(::CreateFontIndirect)(pLogFont));
266  return get_HFont();
267  }
268  };
269 
270  class GRAYLIB_LINK cWndRgn : public cWndGDIT<HRGN>
271  {
274  typedef cWndGDIT<HRGN> SUPER_t;
275  public:
276  cWndRgn(HRGN h = WINHANDLE_NULL) noexcept
277  : cWndGDIT<HRGN>(h)
278  {
279  }
280  cWndRgn(int nLeft, int nTop, int nRight, int nBottom)
281  : cWndGDIT<HRGN>(::CreateRectRgn(nLeft, nTop, nRight, nBottom))
282  {
283  }
284  cWndRgn(const RECT& new_rect)
285  : cWndGDIT<HRGN>(::CreateRectRgnIndirect(&new_rect))
286  {
287  }
288  void ClearRgn()
289  {
291  DeleteObject();
292  }
293  HRGN CreateRectRgn(int nLeft, int nTop, int nRight, int nBottom)
294  {
295  Attach((HGDIOBJ) ::CreateRectRgn(nLeft, nTop, nRight, nBottom));
296  return get_HRgn();
297  }
298  HRGN CreateRectRgn(const RECT& new_rect)
299  {
300  Attach((HGDIOBJ) ::CreateRectRgnIndirect(&new_rect));
301  return get_HRgn();
302  }
303  bool SetRectRgn(int nLeft, int nTop, int nRight, int nBottom)
304  {
306  return ::SetRectRgn(get_HRgn(), nLeft, nTop, nRight, nBottom);
307  }
308  int CombineRgn(HRGN hRgn1, HRGN hRgn2, int iMode)
309  {
313  return ::CombineRgn(get_HRgn(), hRgn1, hRgn2, iMode);
314  }
315  bool IsEqualRgn(HRGN hRgn) const
316  {
317  return ::EqualRgn(get_HRgn(), hRgn);
318  }
319  int GetRgnBox(RECT* pRectRet) const
320  {
321  return ::GetRgnBox(get_HRgn(), pRectRet);
322  }
323  int OffsetRgn(int x, int y)
324  {
325  return ::OffsetRgn(get_HRgn(), x, y);
326  }
327  bool PtInRegion(int x, int y) const
328  {
329  return ::PtInRegion(get_HRgn(), x, y);
330  }
331  bool RectInRegion(const RECT* pRect) const
332  {
333  return ::RectInRegion(get_HRgn(), pRect);
334  }
335 
336  int AddRect(const RECT& new_rect);
337  int SubtractRect(const RECT& new_rect);
338 
339  void Combine(const cWndRgn &rgn);
340  void Intersect(const cWndRgn &rgn);
341  void Subtract(const cWndRgn &rgn);
342 
343  int GetRects(cArrayStruct<RECT>& rects) const;
344  bool SetRects(const RECT* pRects, int nCount);
345  };
346 
347  class GRAYLIB_LINK cWndGDISelect
348  {
352  private:
353  HDC m_hDC;
354  HGDIOBJ m_hGDIObjPrv; // Keep previous copy to restore when this is destroyed.
355  public:
356  cWndGDISelect(HDC hDC, HGDIOBJ hGDIObj)
357  : m_hDC(hDC)
358  {
359  ASSERT(hGDIObj != WINHANDLE_NULL);
360  m_hGDIObjPrv = ::SelectObject(hDC, hGDIObj);
361  }
362  ~cWndGDISelect()
363  {
364  ::SelectObject(m_hDC, m_hGDIObjPrv);
365  }
366  };
367 
368  class GRAYLIB_LINK cWndGDICur : public cWndGDI, public cWndGDISelect
369  {
373  public:
374  cWndGDICur(HDC hDC, HGDIOBJ hObj)
375  : cWndGDI(hObj)
376  , cWndGDISelect(hDC, hObj)
377  {
378  }
379  };
380 
381  class GRAYLIB_LINK cWndIconInfo : public ::ICONINFO
382  {
385  public:
386  cWndGDIT<HBITMAP> m_hbmMask;
387  cWndGDIT<HBITMAP> m_hbmColor;
388 
389  public:
390  bool InitIconInfo(HICON hIcon)
391  {
395  if (!::GetIconInfo(hIcon, static_cast<::ICONINFO*>(this)))
396  return false;
397  m_hbmMask.Attach(this->hbmMask); // Alpha mask for the bitmap.
398  m_hbmColor.Attach(this->hbmColor); // color mask. (if color)
399  return true;
400  }
401  bool InitCursorInfo(HCURSOR hCursor)
402  {
403  return InitIconInfo((HCURSOR)hCursor);
404  }
405  };
406 
407  typedef cWndGDIT<HCURSOR> cWndCursor;
408  typedef cWndGDIT<HICON> cWndIcon;
409 
410 #ifndef _MFC_VER
411  typedef cWinSize CSize; // Alias for MFC
412  typedef cWndGDI CGdiObject;
413  typedef cWndFont CFont;
414  typedef cWndRgn CRgn;
415  typedef cWndPen CPen;
416  typedef cWndBrush CBrush;
417 #endif
418 };
419 #endif // _WIN32
420 #endif // _INC_cWndGDI_H
#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
void CloseHandle()
Definition: cHandlePtr.h:79
Definition: cMesh.h:22
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14