neoWidgets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
GdiObject.h
1 #ifndef NEO_GDI_OBJECT_H
2 #define NEO_GDI_OBJECT_H
3 
4 namespace neo
5 {
6 
10 template<typename HANDLE_T>
11 class GdiObject
12 {
13 public:
14  GdiObject(HANDLE_T pHandle = 0) : handle(pHandle) {}
15  ~GdiObject()
16  {
17  if(handle)
18  DeleteObject(handle);
19  }
20  void SetHandle(HANDLE_T pHandle)
21  {
22  if(handle)
23  DeleteObject(handle);
24  handle = pHandle;
25  }
26  operator HANDLE_T() { return handle; }
27  HANDLE_T handle;
28 };
29 
33 class SolidBrush : public GdiObject<HBRUSH>
34 {
35 public:
39  SolidBrush(COLORREF color) { Create(color); }
40 
42  void Create(COLORREF color) { SetHandle(CreateSolidBrush(color)); }
43 };
44 
48 class HatchBrush : public GdiObject<HBRUSH>
49 {
50 public:
54  HatchBrush(int hatch, COLORREF color) { Create(hatch, color); }
55 
57  void Create(int hatch, COLORREF color) { SetHandle(CreateHatchBrush(hatch, color)); }
58 };
59 
63 class PatternBrush : public GdiObject<HBRUSH>
64 {
65 public:
69  PatternBrush(HBITMAP bitmap) { Create(bitmap); }
70 
72  void Create(HBITMAP bitmap) { SetHandle(CreatePatternBrush(bitmap)); }
73 };
74 
75 // there are also GetSysColorBrush() and GetStockObject(), but these don't need to be managed
76 
80 class Pen : public GdiObject<HPEN>
81 {
82 public:
84  Pen() {}
86  Pen(COLORREF color, int width = 1, int penStyle = PS_SOLID) { Create(color, width, penStyle); }
87 
89  void Create(COLORREF color, int width = 1, int penStyle = PS_SOLID) { SetHandle(CreatePen(penStyle, width, color)); }
90 };
91 
95 class Bitmap : public GdiObject<HBITMAP>
96 {
97 public:
99  Bitmap() {}
101  Bitmap(int width, int height, UINT planes, UINT bpp, const void* data) { CreateBitmap(width, height, planes, bpp, data); }
103  Bitmap(HDC compatibleDC, int width, int height) { CreateCompatibleBitmap(compatibleDC, width, height); }
105  Bitmap(const tstring& filename) { LoadBitmap(filename); }
106 
108  void CreateBitmap(int width, int height, UINT planes, UINT bpp, const void* data) { SetHandle(::CreateBitmap(width, height, planes, bpp, data)); }
110  void CreateCompatibleBitmap(HDC compatibleDC, int width, int height) { SetHandle(::CreateCompatibleBitmap(compatibleDC, width, height)); }
112  void LoadBitmap(const tstring& filename) { DeleteObject(handle); SetHandle((HBITMAP)LoadImage(0, filename.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)); }
113 };
114 
118 class BitmapData : public BITMAP
119 {
120 public:
121  BitmapData(HBITMAP bitmap)
122  {
123  ZeroMemory(this, sizeof(BitmapData));
124  GetObject(bitmap, sizeof(BitmapData), this);
125  }
126 };
127 
131 class DibSection : public GdiObject<HBITMAP>
132 {
133 public:
135  DibSection();
137  DibSection(int width, int height, int bpp, bool bottomup = true);
139  DibSection(HBITMAP bitmap, int bpp, bool bottomup);
141  DibSection(HBITMAP bitmap);
143  DibSection& operator= (const DibSection& other);
144 
146  static int BPP(HBITMAP bitmap);
148  static bool IsDibSection(HBITMAP bitmap);
150  static bool IsBottomUp(HBITMAP bitmap);
152  static void FillBitmapInfo(BITMAPINFO& bmi, int w, int h, int bpp, bool bottomup = true);
153 
155  bool Create(int width, int height, int bpp, bool bottomup = true);
157  bool CopyCreate(HBITMAP bitmap, int bpp, bool bottomup);
159  bool CopyCreate(HBITMAP bitmap) { return CopyCreate(bitmap, BPP(bitmap), IsBottomUp(bitmap)); }
160 
162  bool Attach(HBITMAP bitmap);
164  HBITMAP Detach();
166  void Clear();
168  bool CopyFromBitmap(HBITMAP bitmap);
170  bool CopyToBitmap(HBITMAP bitmap);
171 
173  int GetWidth() { return dibData.bmWidth; }
175  int GetHeight() { return dibData.bmHeight; }
177  int GetBPP() { return dibData.bmBitsPixel; }
179  void* GetBits() { return dibData.bmBits; }
181  void* GetPixelAddress(int x, int y) { return (BYTE*)dibData.bmBits + y*dibData.bmWidthBytes + x*dibData.bmBitsPixel/8; }
183  bool IsBottomUp() { return dibInfo.bmiHeader.biHeight > 0; }
185  int FlippedY(int y) { return IsBottomUp() ? dibData.bmHeight - 1 - y : y; }
186 
187  BITMAP dibData;
188  BITMAPINFO dibInfo;
189 private:
190  template<typename T>
191  static bool GetObject(HANDLE handle, T& obj) { return ::GetObject(handle, sizeof(obj), &obj) == sizeof(obj); }
192 };
193 
200 {
201 public:
202  FontParams()
203  : width(0)
204  , escapement(0)
205  , orientation(0)
206  , weight(FW_DONTCARE)
207  , italic(FALSE)
208  , underline(FALSE)
209  , strikeout(FALSE)
210  , charset(ANSI_CHARSET)
211  , outPrecision(OUT_DEFAULT_PRECIS)
212  , clipPrecision(CLIP_DEFAULT_PRECIS)
213  , quality(PROOF_QUALITY)
214  , pitchAndFamily(VARIABLE_PITCH | FF_DONTCARE)
215  {}
216 
218  FontParams& SetWidth(int pWidth) { width = pWidth; return *this; }
220  FontParams& SetEscapment(int pEscapement) { escapement = pEscapement; return *this; }
222  FontParams& SetOrientation(int pOrientation) { orientation = pOrientation; return *this; }
224  FontParams& SetWeight(int pWeight) { weight = pWeight; return *this; }
226  FontParams& SetItalic(BOOL set) { italic = set; return *this; }
228  FontParams& SetUnderline(BOOL set) { underline = set; return *this; }
230  FontParams& SetStrikeout(BOOL set) { strikeout = set; return *this; }
232  FontParams& SetCharSet(BYTE pCharset) { charset = pCharset; return *this; }
234  FontParams& SetOutPrecision(BYTE pOutPrecision) { outPrecision = pOutPrecision; return *this; }
236  FontParams& SetClipPrecision(BYTE pClipPrecision) { clipPrecision = pClipPrecision; return *this; }
238  FontParams& SetQuality(BYTE pQuality) { quality = pQuality; return *this; }
240  FontParams& SetPitchAndFamily(BYTE pPitchAndFamily) { pitchAndFamily = pPitchAndFamily; return *this; }
242  FontParams& SetFaceName(const tstring& pFacename) { facename = pFacename; return *this; }
243 
245  HFONT CreateFont(int height) const
246  {
247  return ::CreateFont(height,
248  width,
249  escapement,
250  orientation,
251  weight,
252  italic,
253  underline,
254  strikeout,
255  charset,
256  outPrecision,
257  clipPrecision,
258  quality,
259  pitchAndFamily,
260  facename.c_str());
261  }
262 
263  int width;
264  int escapement;
265  int orientation;
266  int weight;
267  DWORD italic;
268  DWORD underline;
269  DWORD strikeout;
270  DWORD charset;
271  DWORD outPrecision;
272  DWORD clipPrecision;
273  DWORD quality;
274  DWORD pitchAndFamily;
275  tstring facename;
276 };
277 
281 class Font : public GdiObject<HFONT>
282 {
283 public:
284  Font(HFONT font = 0) : GdiObject<HFONT>(font) {}
285  Font(int height, const FontParams& params = FontParams()) { Create(height, params); }
286 
287  void Create(int height, const FontParams& params = FontParams()) { SetHandle(params.CreateFont(height)); }
288 
298  {
299  // get message box font
300  NONCLIENTMETRICS ncm;
301  ncm.cbSize = sizeof(ncm);
302 
303  // If we're compiling with the Vista SDK or later, the NONCLIENTMETRICS struct
304  // will be the wrong size for previous versions, so we need to adjust it.
305 #if(WINVER >= 0x0600)
306  OSVERSIONINFO os;
307  os.dwOSVersionInfoSize = sizeof(os);
308  GetVersionEx(&os);
309 
310  if(os.dwMajorVersion < 6)
311  // In versions of Windows prior to Vista, the iPaddedBorderWidth member
312  // is not present, so we need to subtract its size from cbSize.
313  ncm.cbSize -= sizeof(ncm.iPaddedBorderWidth);
314 #endif
315  SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
316 
317  // create a font that matches the message box font
318  SetHandle(CreateFontIndirect(&ncm.lfMessageFont));
319  }
320 };
321 
322 //-----------------------------------------------------------------------------
323 
325 {
326  handle = 0;
327  Clear();
328 }
329 
330 inline DibSection::DibSection(int width, int height, int bpp, bool bottomup)
331 {
332  handle = 0;
333  Create(width, height, bpp, bottomup);
334 }
335 
336 inline DibSection::DibSection(HBITMAP bitmap, int bpp, bool bottomup)
337 {
338  handle = 0;
339  CopyCreate(bitmap, bpp, bottomup);
340 }
341 
342 inline DibSection::DibSection(HBITMAP bitmap)
343 {
344  handle = 0;
345  CopyCreate(bitmap);
346 }
347 
349 {
350  handle = 0;
351  CopyCreate(other.handle);
352  return *this;
353 }
354 
355 inline int DibSection::BPP(HBITMAP bitmap)
356 {
357  BITMAP bm;
358  if(GetObject(bitmap, bm))
359  return bm.bmBitsPixel;
360  return 0;
361 }
362 
363 inline bool DibSection::IsDibSection(HBITMAP bitmap)
364 {
365  DIBSECTION ds;
366  return GetObject(bitmap, ds);
367 }
368 
369 inline bool DibSection::IsBottomUp(HBITMAP bitmap)
370 {
371  DIBSECTION ds;
372  if(GetObject(bitmap, ds))
373  return ds.dsBmih.biHeight > 0;
374  return true;
375 }
376 
377 inline void DibSection::FillBitmapInfo(BITMAPINFO& bmi, int w, int h, int bpp, bool bottomup)
378 {
379  ZeroMemory(&bmi, sizeof(bmi));
380  bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
381  bmi.bmiHeader.biWidth = w;
382  bmi.bmiHeader.biHeight = h;
383  if(!bottomup)
384  bmi.bmiHeader.biHeight = -h;
385  bmi.bmiHeader.biBitCount = bpp;
386  bmi.bmiHeader.biPlanes = 1;
387  bmi.bmiHeader.biCompression = BI_RGB;
388  bmi.bmiHeader.biXPelsPerMeter = 0x0ec4; // this is a standard value
389  bmi.bmiHeader.biYPelsPerMeter = 0x0ec4;
390 }
391 
392 inline bool DibSection::Create(int width, int height, int bpp, bool bottomup)
393 {
394  DeleteObject(handle);
395 
396  FillBitmapInfo(dibInfo, width, height, bpp, bottomup);
397  handle = CreateDIBSection(ClientDC(0), &dibInfo, DIB_RGB_COLORS, 0, 0, 0);
398  if(handle)
399  if(GetObject(handle, dibData))
400  if(dibData.bmBits)
401  return true;
402 
403  Clear();
404  return false;
405 }
406 
407 inline bool DibSection::CopyCreate(HBITMAP bitmap, int bpp, bool bottomup)
408 {
409  BITMAP bm;
410  if(GetObject(bitmap, bm))
411  if(Create(bm.bmWidth, bm.bmHeight, bpp, bottomup))
412  if(CopyFromBitmap(bitmap))
413  return true;
414 
415  Clear();
416  return false;
417 }
418 
419 inline bool DibSection::Attach(HBITMAP bitmap)
420 {
421  DIBSECTION ds;
422  if(GetObject(bitmap, ds)){
423  DeleteObject(handle);
424  handle = bitmap;
425  dibData = ds.dsBm;
426  dibInfo.bmiHeader = ds.dsBmih;
427  return true;
428  }
429 
430  Clear();
431  return false;
432 }
433 
434 inline HBITMAP DibSection::Detach()
435 {
436  HBITMAP result = handle;
437  handle = 0;
438  Clear();
439  return result;
440 }
441 
442 inline void DibSection::Clear()
443 {
444  DeleteObject(handle);
445  handle = 0;
446  ZeroMemory(&dibData, sizeof(dibData));
447  ZeroMemory(&dibInfo, sizeof(dibInfo));
448 }
449 
450 inline bool DibSection::CopyFromBitmap(HBITMAP bitmap)
451 {
452  BITMAP bm;
453  if(GetObject(bitmap, bm))
454  if(bm.bmWidth == GetWidth() && bm.bmHeight == GetHeight())
455  if(GetDIBits(ClientDC(0), bitmap, 0, GetHeight(), dibData.bmBits, &dibInfo, DIB_RGB_COLORS) == GetHeight())
456  return true;
457 
458  return false;
459 }
460 
461 inline bool DibSection::CopyToBitmap(HBITMAP bitmap)
462 {
463  BITMAP bm;
464  if(GetObject(bitmap, bm))
465  if(bm.bmWidth == GetWidth() && bm.bmHeight == GetHeight())
466  if(SetDIBits(ClientDC(0), bitmap, 0, GetHeight(), dibData.bmBits, &dibInfo, DIB_RGB_COLORS) == GetHeight())
467  return true;
468 
469  return false;
470 }
471 
472 } // namespace neo
473 
474 #endif
HBITMAP Detach()
Definition: GdiObject.h:434
FontParams & SetOrientation(int pOrientation)
Definition: GdiObject.h:222
void Create(COLORREF color)
Definition: GdiObject.h:42
void Create(int hatch, COLORREF color)
Definition: GdiObject.h:57
void LoadBitmap(const tstring &filename)
Definition: GdiObject.h:112
PatternBrush(HBITMAP bitmap)
Definition: GdiObject.h:69
bool CopyToBitmap(HBITMAP bitmap)
Definition: GdiObject.h:461
static void FillBitmapInfo(BITMAPINFO &bmi, int w, int h, int bpp, bool bottomup=true)
Definition: GdiObject.h:377
Font parameters with everything set to default.
Definition: GdiObject.h:199
Bitmap()
Definition: GdiObject.h:99
Brush handle
Definition: GdiObject.h:63
FontParams & SetItalic(BOOL set)
Definition: GdiObject.h:226
FontParams & SetUnderline(BOOL set)
Definition: GdiObject.h:228
static bool IsDibSection(HBITMAP bitmap)
Definition: GdiObject.h:363
FontParams & SetOutPrecision(BYTE pOutPrecision)
Definition: GdiObject.h:234
bool IsBottomUp()
Definition: GdiObject.h:183
int GetWidth()
Definition: GdiObject.h:173
void * GetBits()
Definition: GdiObject.h:179
Basic GDI class that automatically deletes the handle.
Definition: GdiObject.h:11
int GetHeight()
Definition: GdiObject.h:175
bool CopyCreate(HBITMAP bitmap)
Definition: GdiObject.h:159
PatternBrush()
Definition: GdiObject.h:67
void * GetPixelAddress(int x, int y)
Definition: GdiObject.h:181
Brush handle
Definition: GdiObject.h:48
BITMAP dibData
Definition: GdiObject.h:187
bool Create(int width, int height, int bpp, bool bottomup=true)
Definition: GdiObject.h:392
void Create(COLORREF color, int width=1, int penStyle=PS_SOLID)
Definition: GdiObject.h:89
Pen(COLORREF color, int width=1, int penStyle=PS_SOLID)
Definition: GdiObject.h:86
Wraps the creation of a BITMAP struct from a HBITMAP.
Definition: GdiObject.h:118
void CreateBitmap(int width, int height, UINT planes, UINT bpp, const void *data)
Definition: GdiObject.h:108
void CreateDefaultFont()
Definition: GdiObject.h:297
BITMAPINFO dibInfo
Definition: GdiObject.h:188
Bitmap(int width, int height, UINT planes, UINT bpp, const void *data)
Definition: GdiObject.h:101
Font handle
Definition: GdiObject.h:281
Bitmap(const tstring &filename)
Definition: GdiObject.h:105
FontParams & SetClipPrecision(BYTE pClipPrecision)
Definition: GdiObject.h:236
Pen handle
Definition: GdiObject.h:80
int GetBPP()
Definition: GdiObject.h:177
void Create(HBITMAP bitmap)
Definition: GdiObject.h:72
HatchBrush()
Definition: GdiObject.h:52
Device independent bitmap.
Definition: GdiObject.h:131
Bitmap(HDC compatibleDC, int width, int height)
Definition: GdiObject.h:103
DibSection()
Definition: GdiObject.h:324
FontParams & SetEscapment(int pEscapement)
Definition: GdiObject.h:220
HatchBrush(int hatch, COLORREF color)
Definition: GdiObject.h:54
FontParams & SetPitchAndFamily(BYTE pPitchAndFamily)
Definition: GdiObject.h:240
bool Attach(HBITMAP bitmap)
Definition: GdiObject.h:419
HFONT CreateFont(int height) const
Definition: GdiObject.h:245
SolidBrush()
Definition: GdiObject.h:37
FontParams & SetWeight(int pWeight)
Definition: GdiObject.h:224
Brush handle
Definition: GdiObject.h:33
A client device context allows drawing to the client area of a window.
Definition: DC.h:218
bool CopyCreate(HBITMAP bitmap, int bpp, bool bottomup)
Definition: GdiObject.h:407
bool CopyFromBitmap(HBITMAP bitmap)
Definition: GdiObject.h:450
FontParams & SetFaceName(const tstring &pFacename)
Definition: GdiObject.h:242
FontParams & SetCharSet(BYTE pCharset)
Definition: GdiObject.h:232
FontParams & SetWidth(int pWidth)
Definition: GdiObject.h:218
DibSection & operator=(const DibSection &other)
Definition: GdiObject.h:348
void CreateCompatibleBitmap(HDC compatibleDC, int width, int height)
Definition: GdiObject.h:110
Pen()
Definition: GdiObject.h:84
FontParams & SetQuality(BYTE pQuality)
Definition: GdiObject.h:238
void Clear()
Definition: GdiObject.h:442
FontParams & SetStrikeout(BOOL set)
Definition: GdiObject.h:230
static int BPP(HBITMAP bitmap)
Definition: GdiObject.h:355
Bitmap handle
Definition: GdiObject.h:95
int FlippedY(int y)
Definition: GdiObject.h:185
SolidBrush(COLORREF color)
Definition: GdiObject.h:39