-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBitmap.h
165 lines (139 loc) · 3.95 KB
/
CBitmap.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
#pragma once
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); }
HBITMAP LoadOEMBitmap(UINT nIDBitmap) // for OBM_/OCR_/OIC_
{
SASSERT(m_hBitmap == NULL);
m_hBitmap = ::LoadBitmap(NULL, MAKEINTRESOURCE(nIDBitmap));
return m_hBitmap;
}
HBITMAP CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitsPerPixel, const void* lpBits)
{
SASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateBitmap(nWidth, nHeight, nPlanes, nBitsPerPixel, lpBits);
return m_hBitmap;
}
HBITMAP CreateBitmapIndirect(LPBITMAP lpBitmap)
{
SASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateBitmapIndirect(lpBitmap);
return m_hBitmap;
}
HBITMAP CreateCompatibleBitmap(HDC hDC, int nWidth, int nHeight)
{
SASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
return m_hBitmap;
}
HBITMAP CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
{
SASSERT(m_hBitmap == NULL);
m_hBitmap = ::CreateDiscardableBitmap(hDC, nWidth, nHeight);
return m_hBitmap;
}
BOOL DeleteObject()
{
SASSERT(m_hBitmap != NULL);
BOOL bRet = ::DeleteObject(m_hBitmap);
if (bRet)
m_hBitmap = NULL;
return bRet;
}
// Attributes
int GetBitmap(BITMAP* pBitMap) const
{
SASSERT(m_hBitmap != NULL);
return ::GetObject(m_hBitmap, sizeof(BITMAP), pBitMap);
}
bool GetBitmap(BITMAP& bm) const
{
SASSERT(m_hBitmap != NULL);
return (::GetObject(m_hBitmap, sizeof(BITMAP), &bm) == sizeof(BITMAP));
}
bool GetSize(SIZE& size) const
{
SASSERT(m_hBitmap != NULL);
BITMAP bm = { 0 };
if (!GetBitmap(&bm))
return false;
size.cx = bm.bmWidth;
size.cy = bm.bmHeight;
return true;
}
DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const
{
SASSERT(m_hBitmap != NULL);
return ::GetBitmapBits(m_hBitmap, dwCount, lpBits);
}
DWORD SetBitmapBits(DWORD dwCount, const void* lpBits)
{
SASSERT(m_hBitmap != NULL);
return ::SetBitmapBits(m_hBitmap, dwCount, lpBits);
}
BOOL GetBitmapDimension(LPSIZE lpSize) const
{
SASSERT(m_hBitmap != NULL);
return ::GetBitmapDimensionEx(m_hBitmap, lpSize);
}
BOOL SetBitmapDimension(int nWidth, int nHeight, LPSIZE lpSize = NULL)
{
SASSERT(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)
{
SASSERT(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)
{
SASSERT(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
{
SASSERT(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)
{
SASSERT(m_hBitmap != NULL);
return ::SetDIBits(hDC, m_hBitmap, uStartScan, cScanLines, lpvBits, lpbmi, uColorUse);
}
};
typedef CBitmapT<false> CBitmapHandle;
typedef CBitmapT<true> CBitmap;