-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlgdi.h
3445 lines (2903 loc) · 83.1 KB
/
atlgdi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Windows Template Library - WTL version 10.0
// Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Public License (http://opensource.org/licenses/MS-PL)
// which can be found in the file MS-PL.txt at the root folder.
#ifndef __ATLGDI_H__
#define __ATLGDI_H__
#pragma once
#ifndef __ATLAPP_H__
#error atlgdi.h requires atlapp.h to be included first
#endif
// protect template members from windowsx.h macros
#ifdef _INC_WINDOWSX
#undef CopyRgn
#undef CreateBrush
#undef CreatePen
#undef SelectBrush
#undef SelectPen
#undef SelectFont
#undef SelectBitmap
#endif // _INC_WINDOWSX
// required libraries
#pragma comment(lib, "msimg32.lib")
#if !defined(_ATL_NO_OPENGL)
#pragma comment(lib, "opengl32.lib")
#endif
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CPenT<t_bManaged>
// CBrushT<t_bManaged>
// CLogFont
// CFontT<t_bManaged>
// CBitmapT<t_bManaged>
// CPaletteT<t_bManaged>
// CRgnT<t_bManaged>
// CDCT<t_bManaged>
// CPaintDC
// CClientDC
// CWindowDC
// CMemoryDC
// CEnhMetaFileInfo
// CEnhMetaFileT<t_bManaged>
// CEnhMetaFileDC
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// Bitmap resource helpers to extract bitmap information for a bitmap resource
inline LPBITMAPINFOHEADER AtlGetBitmapResourceInfo(HMODULE hModule, ATL::_U_STRINGorID image)
{
HRSRC hResource = ::FindResource(hModule, image.m_lpstr, RT_BITMAP);
ATLASSERT(hResource != NULL);
HGLOBAL hGlobal = ::LoadResource(hModule, hResource);
ATLASSERT(hGlobal != NULL);
LPBITMAPINFOHEADER pBitmapInfoHeader = (LPBITMAPINFOHEADER)::LockResource(hGlobal);
ATLASSERT(pBitmapInfoHeader != NULL);
return pBitmapInfoHeader;
}
inline WORD AtlGetBitmapResourceBitsPerPixel(HMODULE hModule, ATL::_U_STRINGorID image)
{
LPBITMAPINFOHEADER pBitmapInfoHeader = AtlGetBitmapResourceInfo(hModule, image);
ATLASSERT(pBitmapInfoHeader != NULL);
return pBitmapInfoHeader->biBitCount;
}
inline WORD AtlGetBitmapResourceBitsPerPixel(ATL::_U_STRINGorID image)
{
return AtlGetBitmapResourceBitsPerPixel(ModuleHelper::GetResourceInstance(), image);
}
///////////////////////////////////////////////////////////////////////////////
// 32-bit (alpha channel) bitmap resource helper
// Note: 32-bit (alpha channel) images work only on Windows XP with Common Controls version 6.
// If you want your app to work on older version of Windows, load non-alpha images if Common
// Controls version is less than 6.
inline bool AtlIsAlphaBitmapResource(ATL::_U_STRINGorID image)
{
return (AtlGetBitmapResourceBitsPerPixel(image) == 32);
}
///////////////////////////////////////////////////////////////////////////////
// CPen
template <bool t_bManaged>
class CPenT
{
public:
// Data members
HPEN m_hPen;
// Constructor/destructor/operators
CPenT(HPEN hPen = NULL) : m_hPen(hPen)
{ }
~CPenT()
{
if(t_bManaged && (m_hPen != NULL))
DeleteObject();
}
CPenT<t_bManaged>& operator =(HPEN hPen)
{
Attach(hPen);
return *this;
}
void Attach(HPEN hPen)
{
if(t_bManaged && (m_hPen != NULL) && (m_hPen != hPen))
::DeleteObject(m_hPen);
m_hPen = hPen;
}
HPEN Detach()
{
HPEN hPen = m_hPen;
m_hPen = NULL;
return hPen;
}
operator HPEN() const { return m_hPen; }
bool IsNull() const { return (m_hPen == NULL); }
// Create methods
HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor)
{
ATLASSERT(m_hPen == NULL);
m_hPen = ::CreatePen(nPenStyle, nWidth, crColor);
return m_hPen;
}
HPEN CreatePen(int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL)
{
ATLASSERT(m_hPen == NULL);
m_hPen = ::ExtCreatePen(nPenStyle, nWidth, pLogBrush, nStyleCount, lpStyle);
return m_hPen;
}
HPEN CreatePenIndirect(LPLOGPEN lpLogPen)
{
ATLASSERT(m_hPen == NULL);
m_hPen = ::CreatePenIndirect(lpLogPen);
return m_hPen;
}
BOOL DeleteObject()
{
ATLASSERT(m_hPen != NULL);
BOOL bRet = ::DeleteObject(m_hPen);
if(bRet)
m_hPen = NULL;
return bRet;
}
// Attributes
int GetLogPen(LOGPEN* pLogPen) const
{
ATLASSERT(m_hPen != NULL);
return ::GetObject(m_hPen, sizeof(LOGPEN), pLogPen);
}
bool GetLogPen(LOGPEN& LogPen) const
{
ATLASSERT(m_hPen != NULL);
return (::GetObject(m_hPen, sizeof(LOGPEN), &LogPen) == sizeof(LOGPEN));
}
int GetExtLogPen(EXTLOGPEN* pLogPen, int nSize = sizeof(EXTLOGPEN)) const
{
ATLASSERT(m_hPen != NULL);
return ::GetObject(m_hPen, nSize, pLogPen);
}
bool GetExtLogPen(EXTLOGPEN& ExtLogPen, int nSize = sizeof(EXTLOGPEN)) const
{
ATLASSERT(m_hPen != NULL);
int nRet = ::GetObject(m_hPen, nSize, &ExtLogPen);
return ((nRet > 0) && (nRet <= nSize));
}
};
typedef CPenT<false> CPenHandle;
typedef CPenT<true> CPen;
///////////////////////////////////////////////////////////////////////////////
// CBrush
template <bool t_bManaged>
class CBrushT
{
public:
// Data members
HBRUSH m_hBrush;
// Constructor/destructor/operators
CBrushT(HBRUSH hBrush = NULL) : m_hBrush(hBrush)
{ }
~CBrushT()
{
if(t_bManaged && (m_hBrush != NULL))
DeleteObject();
}
CBrushT<t_bManaged>& operator =(HBRUSH hBrush)
{
Attach(hBrush);
return *this;
}
void Attach(HBRUSH hBrush)
{
if(t_bManaged && (m_hBrush != NULL) && (m_hBrush != hBrush))
::DeleteObject(m_hBrush);
m_hBrush = hBrush;
}
HBRUSH Detach()
{
HBRUSH hBrush = m_hBrush;
m_hBrush = NULL;
return hBrush;
}
operator HBRUSH() const { return m_hBrush; }
bool IsNull() const { return (m_hBrush == NULL); }
// Create methods
HBRUSH CreateSolidBrush(COLORREF crColor)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateSolidBrush(crColor);
return m_hBrush;
}
HBRUSH CreateHatchBrush(int nIndex, COLORREF crColor)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateHatchBrush(nIndex, crColor);
return m_hBrush;
}
HBRUSH CreateBrushIndirect(const LOGBRUSH* lpLogBrush)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateBrushIndirect(lpLogBrush);
return m_hBrush;
}
HBRUSH CreatePatternBrush(HBITMAP hBitmap)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreatePatternBrush(hBitmap);
return m_hBrush;
}
HBRUSH CreateDIBPatternBrush(HGLOBAL hPackedDIB, UINT nUsage)
{
ATLASSERT(hPackedDIB != NULL);
const void* lpPackedDIB = GlobalLock(hPackedDIB);
ATLASSERT(lpPackedDIB != NULL);
m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
GlobalUnlock(hPackedDIB);
return m_hBrush;
}
HBRUSH CreateDIBPatternBrush(const void* lpPackedDIB, UINT nUsage)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
return m_hBrush;
}
HBRUSH CreateSysColorBrush(int nIndex)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::GetSysColorBrush(nIndex);
return m_hBrush;
}
BOOL DeleteObject()
{
ATLASSERT(m_hBrush != NULL);
BOOL bRet = ::DeleteObject(m_hBrush);
if(bRet)
m_hBrush = NULL;
return bRet;
}
// Attributes
int GetLogBrush(LOGBRUSH* pLogBrush) const
{
ATLASSERT(m_hBrush != NULL);
return ::GetObject(m_hBrush, sizeof(LOGBRUSH), pLogBrush);
}
bool GetLogBrush(LOGBRUSH& LogBrush) const
{
ATLASSERT(m_hBrush != NULL);
return (::GetObject(m_hBrush, sizeof(LOGBRUSH), &LogBrush) == sizeof(LOGBRUSH));
}
};
typedef CBrushT<false> CBrushHandle;
typedef CBrushT<true> CBrush;
///////////////////////////////////////////////////////////////////////////////
// CFont
class CLogFont : public LOGFONT
{
public:
CLogFont()
{
memset(this, 0, sizeof(LOGFONT));
}
CLogFont(const LOGFONT& lf)
{
Copy(&lf);
}
CLogFont(HFONT hFont)
{
ATLASSERT(::GetObjectType(hFont) == OBJ_FONT);
::GetObject(hFont, sizeof(LOGFONT), (LOGFONT*)this);
}
HFONT CreateFontIndirect()
{
return ::CreateFontIndirect(this);
}
void SetBold()
{
lfWeight = FW_BOLD;
}
bool IsBold() const
{
return (lfWeight >= FW_BOLD);
}
void MakeBolder(int iScale = 1)
{
lfWeight += FW_BOLD * iScale;
}
void MakeLarger(int iScale)
{
if(lfHeight > 0)
lfHeight += iScale;
else
lfHeight -= iScale;
}
void SetHeight(LONG nPointSize, HDC hDC = NULL)
{
HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
// For MM_TEXT mapping mode
lfHeight = -::MulDiv(nPointSize, ::GetDeviceCaps(hDC1, LOGPIXELSY), 72);
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
}
LONG GetHeight(HDC hDC = NULL) const
{
HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
// For MM_TEXT mapping mode
LONG nPointSize = ::MulDiv(-lfHeight, 72, ::GetDeviceCaps(hDC1, LOGPIXELSY));
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
return nPointSize;
}
LONG GetDeciPointHeight(HDC hDC = NULL) const
{
HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC1, &ptOrg, 1);
POINT pt = { 0, 0 };
pt.y = abs(lfHeight) + ptOrg.y;
::LPtoDP(hDC1, &pt, 1);
LONG nDeciPoint = ::MulDiv(pt.y, 720, ::GetDeviceCaps(hDC1, LOGPIXELSY)); // 72 points/inch, 10 decipoints/point
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
return nDeciPoint;
}
void SetHeightFromDeciPoint(LONG nDeciPtHeight, HDC hDC = NULL)
{
HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
POINT pt = { 0, 0 };
pt.y = ::MulDiv(::GetDeviceCaps(hDC1, LOGPIXELSY), nDeciPtHeight, 720); // 72 points/inch, 10 decipoints/point
::DPtoLP(hDC1, &pt, 1);
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC1, &ptOrg, 1);
lfHeight = -abs(pt.y - ptOrg.y);
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
}
void SetCaptionFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfCaptionFont);
}
void SetMenuFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfMenuFont);
}
void SetStatusFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfStatusFont);
}
void SetMessageBoxFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfMessageFont);
}
void Copy(const LOGFONT* pLogFont)
{
ATLASSERT(pLogFont != NULL);
*(LOGFONT*)this = *pLogFont;
}
CLogFont& operator =(const CLogFont& src)
{
Copy(&src);
return *this;
}
CLogFont& operator =(const LOGFONT& src)
{
Copy(&src);
return *this;
}
CLogFont& operator =(HFONT hFont)
{
ATLASSERT(::GetObjectType(hFont) == OBJ_FONT);
::GetObject(hFont, sizeof(LOGFONT), (LOGFONT*)this);
return *this;
}
bool operator ==(const LOGFONT& logfont) const
{
return((logfont.lfHeight == lfHeight) &&
(logfont.lfWidth == lfWidth) &&
(logfont.lfEscapement == lfEscapement) &&
(logfont.lfOrientation == lfOrientation) &&
(logfont.lfWeight == lfWeight) &&
(logfont.lfItalic == lfItalic) &&
(logfont.lfUnderline == lfUnderline) &&
(logfont.lfStrikeOut == lfStrikeOut) &&
(logfont.lfCharSet == lfCharSet) &&
(logfont.lfOutPrecision == lfOutPrecision) &&
(logfont.lfClipPrecision == lfClipPrecision) &&
(logfont.lfQuality == lfQuality) &&
(logfont.lfPitchAndFamily == lfPitchAndFamily) &&
(lstrcmp(logfont.lfFaceName, lfFaceName) == 0));
}
};
template <bool t_bManaged>
class CFontT
{
public:
// Data members
HFONT m_hFont;
// Constructor/destructor/operators
CFontT(HFONT hFont = NULL) : m_hFont(hFont)
{ }
~CFontT()
{
if(t_bManaged && (m_hFont != NULL))
DeleteObject();
}
CFontT<t_bManaged>& operator =(HFONT hFont)
{
Attach(hFont);
return *this;
}
void Attach(HFONT hFont)
{
if(t_bManaged && (m_hFont != NULL) && (m_hFont != hFont))
::DeleteObject(m_hFont);
m_hFont = hFont;
}
HFONT Detach()
{
HFONT hFont = m_hFont;
m_hFont = NULL;
return hFont;
}
operator HFONT() const { return m_hFont; }
bool IsNull() const { return (m_hFont == NULL); }
// Create methods
HFONT CreateFontIndirect(const LOGFONT* lpLogFont)
{
ATLASSERT(m_hFont == NULL);
m_hFont = ::CreateFontIndirect(lpLogFont);
return m_hFont;
}
HFONT CreateFontIndirectEx(CONST ENUMLOGFONTEXDV* penumlfex)
{
ATLASSERT(m_hFont == NULL);
m_hFont = ::CreateFontIndirectEx(penumlfex);
return m_hFont;
}
HFONT CreateFont(int nHeight, int nWidth, int nEscapement,
int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline,
BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision,
BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily,
LPCTSTR lpszFacename)
{
ATLASSERT(m_hFont == NULL);
m_hFont = ::CreateFont(nHeight, nWidth, nEscapement,
nOrientation, nWeight, bItalic, bUnderline, cStrikeOut,
nCharSet, nOutPrecision, nClipPrecision, nQuality,
nPitchAndFamily, lpszFacename);
return m_hFont;
}
HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, HDC hDC = NULL, bool bBold = false, bool bItalic = false)
{
LOGFONT logFont = {};
logFont.lfCharSet = DEFAULT_CHARSET;
logFont.lfHeight = nPointSize;
ATL::Checked::tcsncpy_s(logFont.lfFaceName, _countof(logFont.lfFaceName), lpszFaceName, _TRUNCATE);
if(bBold)
logFont.lfWeight = FW_BOLD;
if(bItalic)
logFont.lfItalic = (BYTE)TRUE;
return CreatePointFontIndirect(&logFont, hDC);
}
HFONT CreatePointFontIndirect(const LOGFONT* lpLogFont, HDC hDC = NULL)
{
HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
// convert nPointSize to logical units based on hDC
LOGFONT logFont = *lpLogFont;
POINT pt = { 0, 0 };
pt.y = ::MulDiv(::GetDeviceCaps(hDC1, LOGPIXELSY), logFont.lfHeight, 720); // 72 points/inch, 10 decipoints/point
::DPtoLP(hDC1, &pt, 1);
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC1, &ptOrg, 1);
logFont.lfHeight = -abs(pt.y - ptOrg.y);
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
return CreateFontIndirect(&logFont);
}
BOOL DeleteObject()
{
ATLASSERT(m_hFont != NULL);
BOOL bRet = ::DeleteObject(m_hFont);
if(bRet)
m_hFont = NULL;
return bRet;
}
// Attributes
int GetLogFont(LOGFONT* pLogFont) const
{
ATLASSERT(m_hFont != NULL);
return ::GetObject(m_hFont, sizeof(LOGFONT), pLogFont);
}
bool GetLogFont(LOGFONT& LogFont) const
{
ATLASSERT(m_hFont != NULL);
return (::GetObject(m_hFont, sizeof(LOGFONT), &LogFont) == sizeof(LOGFONT));
}
};
typedef CFontT<false> CFontHandle;
typedef CFontT<true> CFont;
///////////////////////////////////////////////////////////////////////////////
// CBitmap
template <bool t_bManaged>
class CBitmapT
{
public:
// Data members
HBITMAP m_hBitmap;
// Constructor/destructor/operators
CBitmapT(HBITMAP hBitmap = NULL) : m_hBitmap(hBitmap)
{ }
~CBitmapT()
{
if(t_bManaged && (m_hBitmap != NULL))
DeleteObject();
}
CBitmapT<t_bManaged>& operator =(HBITMAP hBitmap)
{
Attach(hBitmap);
return *this;
}
void Attach(HBITMAP hBitmap)
{
if(t_bManaged && (m_hBitmap != NULL) && (m_hBitmap != hBitmap))
::DeleteObject(m_hBitmap);
m_hBitmap = hBitmap;
}
HBITMAP Detach()
{
HBITMAP hBitmap = m_hBitmap;
m_hBitmap = NULL;
return hBitmap;
}
operator HBITMAP() const { return m_hBitmap; }
bool IsNull() const { return (m_hBitmap == NULL); }
// Create and load methods
HBITMAP LoadBitmap(ATL::_U_STRINGorID bitmap)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::LoadBitmap(ModuleHelper::GetResourceInstance(), bitmap.m_lpstr);
return m_hBitmap;
}
HBITMAP LoadOEMBitmap(UINT nIDBitmap) // for OBM_/OCR_/OIC_
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::LoadBitmap(NULL, MAKEINTRESOURCE(nIDBitmap));
return m_hBitmap;
}
HBITMAP LoadMappedBitmap(UINT nIDBitmap, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateMappedBitmap(ModuleHelper::GetResourceInstance(), nIDBitmap, (WORD)nFlags, lpColorMap, nMapSize);
return m_hBitmap;
}
HBITMAP CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitsPerPixel, const void* lpBits)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateBitmap(nWidth, nHeight, nPlanes, nBitsPerPixel, lpBits);
return m_hBitmap;
}
HBITMAP CreateBitmapIndirect(LPBITMAP lpBitmap)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateBitmapIndirect(lpBitmap);
return m_hBitmap;
}
HBITMAP CreateCompatibleBitmap(HDC hDC, int nWidth, int nHeight)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
return m_hBitmap;
}
HBITMAP CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateDiscardableBitmap(hDC, nWidth, nHeight);
return m_hBitmap;
}
BOOL DeleteObject()
{
ATLASSERT(m_hBitmap != NULL);
BOOL bRet = ::DeleteObject(m_hBitmap);
if(bRet)
m_hBitmap = NULL;
return bRet;
}
// Attributes
int GetBitmap(BITMAP* pBitMap) const
{
ATLASSERT(m_hBitmap != NULL);
return ::GetObject(m_hBitmap, sizeof(BITMAP), pBitMap);
}
bool GetBitmap(BITMAP& bm) const
{
ATLASSERT(m_hBitmap != NULL);
return (::GetObject(m_hBitmap, sizeof(BITMAP), &bm) == sizeof(BITMAP));
}
bool GetSize(SIZE& size) const
{
ATLASSERT(m_hBitmap != NULL);
BITMAP bm = {};
if(!GetBitmap(&bm))
return false;
size.cx = bm.bmWidth;
size.cy = bm.bmHeight;
return true;
}
DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const
{
ATLASSERT(m_hBitmap != NULL);
return ::GetBitmapBits(m_hBitmap, dwCount, lpBits);
}
DWORD SetBitmapBits(DWORD dwCount, const void* lpBits)
{
ATLASSERT(m_hBitmap != NULL);
return ::SetBitmapBits(m_hBitmap, dwCount, lpBits);
}
BOOL GetBitmapDimension(LPSIZE lpSize) const
{
ATLASSERT(m_hBitmap != NULL);
return ::GetBitmapDimensionEx(m_hBitmap, lpSize);
}
BOOL SetBitmapDimension(int nWidth, int nHeight, LPSIZE lpSize = NULL)
{
ATLASSERT(m_hBitmap != NULL);
return ::SetBitmapDimensionEx(m_hBitmap, nWidth, nHeight, lpSize);
}
// DIB support
HBITMAP CreateDIBitmap(HDC hDC, CONST BITMAPINFOHEADER* lpbmih, DWORD dwInit, CONST VOID* lpbInit, CONST BITMAPINFO* lpbmi, UINT uColorUse)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateDIBitmap(hDC, lpbmih, dwInit, lpbInit, lpbmi, uColorUse);
return m_hBitmap;
}
HBITMAP CreateDIBSection(HDC hDC, CONST BITMAPINFO* lpbmi, UINT uColorUse, VOID** ppvBits, HANDLE hSection, DWORD dwOffset)
{
ATLASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateDIBSection(hDC, lpbmi, uColorUse, ppvBits, hSection, dwOffset);
return m_hBitmap;
}
int GetDIBits(HDC hDC, UINT uStartScan, UINT cScanLines, LPVOID lpvBits, LPBITMAPINFO lpbmi, UINT uColorUse) const
{
ATLASSERT(m_hBitmap != NULL);
return ::GetDIBits(hDC, m_hBitmap, uStartScan, cScanLines, lpvBits, lpbmi, uColorUse);
}
int SetDIBits(HDC hDC, UINT uStartScan, UINT cScanLines, CONST VOID* lpvBits, CONST BITMAPINFO* lpbmi, UINT uColorUse)
{
ATLASSERT(m_hBitmap != NULL);
return ::SetDIBits(hDC, m_hBitmap, uStartScan, cScanLines, lpvBits, lpbmi, uColorUse);
}
};
typedef CBitmapT<false> CBitmapHandle;
typedef CBitmapT<true> CBitmap;
///////////////////////////////////////////////////////////////////////////////
// CPalette
template <bool t_bManaged>
class CPaletteT
{
public:
// Data members
HPALETTE m_hPalette;
// Constructor/destructor/operators
CPaletteT(HPALETTE hPalette = NULL) : m_hPalette(hPalette)
{ }
~CPaletteT()
{
if(t_bManaged && (m_hPalette != NULL))
DeleteObject();
}
CPaletteT<t_bManaged>& operator =(HPALETTE hPalette)
{
Attach(hPalette);
return *this;
}
void Attach(HPALETTE hPalette)
{
if(t_bManaged && (m_hPalette != NULL) && (m_hPalette != hPalette))
::DeleteObject(m_hPalette);
m_hPalette = hPalette;
}
HPALETTE Detach()
{
HPALETTE hPalette = m_hPalette;
m_hPalette = NULL;
return hPalette;
}
operator HPALETTE() const { return m_hPalette; }
bool IsNull() const { return (m_hPalette == NULL); }
// Create methods
HPALETTE CreatePalette(LPLOGPALETTE lpLogPalette)
{
ATLASSERT(m_hPalette == NULL);
m_hPalette = ::CreatePalette(lpLogPalette);
return m_hPalette;
}
HPALETTE CreateHalftonePalette(HDC hDC)
{
ATLASSERT(m_hPalette == NULL);
ATLASSERT(hDC != NULL);
m_hPalette = ::CreateHalftonePalette(hDC);
return m_hPalette;
}
BOOL DeleteObject()
{
ATLASSERT(m_hPalette != NULL);
BOOL bRet = ::DeleteObject(m_hPalette);
if(bRet)
m_hPalette = NULL;
return bRet;
}
// Attributes
int GetEntryCount() const
{
ATLASSERT(m_hPalette != NULL);
WORD nEntries = 0;
::GetObject(m_hPalette, sizeof(WORD), &nEntries);
return (int)nEntries;
}
UINT GetPaletteEntries(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors) const
{
ATLASSERT(m_hPalette != NULL);
return ::GetPaletteEntries(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
}
UINT SetPaletteEntries(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors)
{
ATLASSERT(m_hPalette != NULL);
return ::SetPaletteEntries(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
}
// Operations
void AnimatePalette(UINT nStartIndex, UINT nNumEntries, LPPALETTEENTRY lpPaletteColors)
{
ATLASSERT(m_hPalette != NULL);
::AnimatePalette(m_hPalette, nStartIndex, nNumEntries, lpPaletteColors);
}
BOOL ResizePalette(UINT nNumEntries)
{
ATLASSERT(m_hPalette != NULL);
return ::ResizePalette(m_hPalette, nNumEntries);
}
UINT GetNearestPaletteIndex(COLORREF crColor) const
{
ATLASSERT(m_hPalette != NULL);
return ::GetNearestPaletteIndex(m_hPalette, crColor);
}
};
typedef CPaletteT<false> CPaletteHandle;
typedef CPaletteT<true> CPalette;
///////////////////////////////////////////////////////////////////////////////
// CRgn
template <bool t_bManaged>
class CRgnT
{
public:
// Data members
HRGN m_hRgn;
// Constructor/destructor/operators
CRgnT(HRGN hRgn = NULL) : m_hRgn(hRgn)
{ }
~CRgnT()
{
if(t_bManaged && (m_hRgn != NULL))
DeleteObject();
}
CRgnT<t_bManaged>& operator =(HRGN hRgn)
{
Attach(hRgn);
return *this;
}
void Attach(HRGN hRgn)
{
if(t_bManaged && (m_hRgn != NULL) && (m_hRgn != hRgn))
::DeleteObject(m_hRgn);
m_hRgn = hRgn;
}
HRGN Detach()
{
HRGN hRgn = m_hRgn;
m_hRgn = NULL;
return hRgn;
}
operator HRGN() const { return m_hRgn; }
bool IsNull() const { return (m_hRgn == NULL); }
// Create methods
HRGN CreateRectRgn(int x1, int y1, int x2, int y2)
{
ATLASSERT(m_hRgn == NULL);
m_hRgn = ::CreateRectRgn(x1, y1, x2, y2);
return m_hRgn;
}
HRGN CreateRectRgnIndirect(LPCRECT lpRect)
{
ATLASSERT(m_hRgn == NULL);
m_hRgn = ::CreateRectRgnIndirect(lpRect);
return m_hRgn;
}
HRGN CreateEllipticRgn(int x1, int y1, int x2, int y2)
{
ATLASSERT(m_hRgn == NULL);
m_hRgn = ::CreateEllipticRgn(x1, y1, x2, y2);
return m_hRgn;
}
HRGN CreateEllipticRgnIndirect(LPCRECT lpRect)
{
ATLASSERT(m_hRgn == NULL);
m_hRgn = ::CreateEllipticRgnIndirect(lpRect);
return m_hRgn;
}