-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIconBitmapUtils.cpp
309 lines (255 loc) · 9.13 KB
/
IconBitmapUtils.cpp
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
// sktoolslib - common files for SK tools
// Copyright (C) 2013, 2020 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "IconBitmapUtils.h"
#pragma comment(lib, "uxtheme.lib")
IconBitmapUtils::IconBitmapUtils()
{
}
IconBitmapUtils::~IconBitmapUtils()
{
std::map<UINT, HBITMAP>::iterator it;
for (it = bitmaps.begin(); it != bitmaps.end(); ++it)
{
::DeleteObject(it->second);
}
bitmaps.clear();
}
HBITMAP IconBitmapUtils::IconToBitmap(HINSTANCE hInst, UINT uIcon)
{
std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
return bitmap_it->second;
HICON hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 12, 12, LR_DEFAULTCOLOR);
if (!hIcon)
return NULL;
RECT rect;
rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);
rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);
rect.left = rect.top = 0;
HWND desktop = ::GetDesktopWindow();
if (desktop == NULL)
{
DestroyIcon(hIcon);
return NULL;
}
HDC screen_dev = ::GetDC(desktop);
if (screen_dev == NULL)
{
DestroyIcon(hIcon);
return NULL;
}
// Create a compatible DC
HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
if (dst_hdc == NULL)
{
DestroyIcon(hIcon);
::ReleaseDC(desktop, screen_dev);
return NULL;
}
// Create a new bitmap of icon size
HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
if (bmp == NULL)
{
DestroyIcon(hIcon);
::DeleteDC(dst_hdc);
::ReleaseDC(desktop, screen_dev);
return NULL;
}
// Select it into the compatible DC
HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
if (old_dst_bmp == NULL)
{
DestroyIcon(hIcon);
::DeleteDC(dst_hdc);
::ReleaseDC(desktop, screen_dev);
return NULL;
}
// Fill the background of the compatible DC with the white color
// that is taken by menu routines as transparent
::SetBkColor(dst_hdc, RGB(255, 255, 255));
::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
// Draw the icon into the compatible DC
::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);
// Restore settings
::SelectObject(dst_hdc, old_dst_bmp);
::DeleteDC(dst_hdc);
::ReleaseDC(desktop, screen_dev);
DestroyIcon(hIcon);
if (bmp)
bitmaps.insert(bitmap_it, std::make_pair(uIcon, bmp));
return bmp;
}
HBITMAP IconBitmapUtils::IconToBitmapPARGB32(HINSTANCE hInst, UINT uIcon)
{
std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
return bitmap_it->second;
HICON hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(uIcon), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
HBITMAP hBmp = IconToBitmapPARGB32(hIcon);
DestroyIcon(hIcon);
if (hBmp)
bitmaps.insert(bitmap_it, std::make_pair(uIcon, hBmp));
return hBmp;
}
HBITMAP IconBitmapUtils::IconToBitmapPARGB32(HICON hIcon)
{
if (!hIcon)
return NULL;
SIZE sizIcon;
sizIcon.cx = GetSystemMetrics(SM_CXSMICON);
sizIcon.cy = GetSystemMetrics(SM_CYSMICON);
RECT rcIcon;
SetRect(&rcIcon, 0, 0, sizIcon.cx, sizIcon.cy);
HBITMAP hBmp = NULL;
HDC hdcDest = CreateCompatibleDC(NULL);
if (hdcDest)
{
if (SUCCEEDED(Create32BitHBITMAP(hdcDest, &sizIcon, NULL, &hBmp)))
{
HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcDest, hBmp);
if (hbmpOld)
{
BLENDFUNCTION bfAlpha = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
BP_PAINTPARAMS paintParams = {0};
paintParams.cbSize = sizeof(paintParams);
paintParams.dwFlags = BPPF_ERASE;
paintParams.pBlendFunction = &bfAlpha;
HDC hdcBuffer;
HPAINTBUFFER hPaintBuffer = BeginBufferedPaint(hdcDest, &rcIcon, BPBF_DIB, &paintParams, &hdcBuffer);
if (hPaintBuffer)
{
if (DrawIconEx(hdcBuffer, 0, 0, hIcon, sizIcon.cx, sizIcon.cy, 0, NULL, DI_NORMAL))
{
// If icon did not have an alpha channel we need to convert buffer to PARGB
ConvertBufferToPARGB32(hPaintBuffer, hdcDest, hIcon, sizIcon);
}
// This will write the buffer contents to the destination bitmap
EndBufferedPaint(hPaintBuffer, TRUE);
}
SelectObject(hdcDest, hbmpOld);
}
}
DeleteDC(hdcDest);
}
return hBmp;
}
HRESULT IconBitmapUtils::Create32BitHBITMAP(HDC hdc, const SIZE *psize, __deref_opt_out void **ppvBits, __out HBITMAP *phBmp)
{
if (psize == 0)
return E_INVALIDARG;
if (phBmp == 0)
return E_POINTER;
*phBmp = NULL;
BITMAPINFO bmi;
SecureZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biWidth = psize->cx;
bmi.bmiHeader.biHeight = psize->cy;
bmi.bmiHeader.biBitCount = 32;
HDC hdcUsed = hdc ? hdc : GetDC(NULL);
if (hdcUsed)
{
*phBmp = CreateDIBSection(hdcUsed, &bmi, DIB_RGB_COLORS, ppvBits, NULL, 0);
if (hdc != hdcUsed)
{
ReleaseDC(NULL, hdcUsed);
}
}
return (NULL == *phBmp) ? E_OUTOFMEMORY : S_OK;
}
HRESULT IconBitmapUtils::ConvertBufferToPARGB32(HPAINTBUFFER hPaintBuffer, HDC hdc, HICON hicon, SIZE &sizIcon)
{
RGBQUAD *prgbQuad;
int cxRow;
HRESULT hr = GetBufferedPaintBits(hPaintBuffer, &prgbQuad, &cxRow);
if (SUCCEEDED(hr))
{
Gdiplus::ARGB *pargb = reinterpret_cast<Gdiplus::ARGB *>(prgbQuad);
if (!HasAlpha(pargb, sizIcon, cxRow))
{
ICONINFO info;
if (GetIconInfo(hicon, &info))
{
if (info.hbmMask)
{
hr = ConvertToPARGB32(hdc, pargb, info.hbmMask, sizIcon, cxRow);
}
DeleteObject(info.hbmColor);
DeleteObject(info.hbmMask);
}
}
}
return hr;
}
bool IconBitmapUtils::HasAlpha(__in Gdiplus::ARGB *pargb, SIZE &sizImage, int cxRow)
{
ULONG cxDelta = cxRow - sizImage.cx;
for (ULONG y = sizImage.cy; y; --y)
{
for (ULONG x = sizImage.cx; x; --x)
{
if (*pargb++ & 0xFF000000)
{
return true;
}
}
pargb += cxDelta;
}
return false;
}
HRESULT IconBitmapUtils::ConvertToPARGB32(HDC hdc, __inout Gdiplus::ARGB *pargb, HBITMAP hbmp, SIZE &sizImage, int cxRow)
{
BITMAPINFO bmi;
SecureZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biWidth = sizImage.cx;
bmi.bmiHeader.biHeight = sizImage.cy;
bmi.bmiHeader.biBitCount = 32;
HANDLE hHeap = GetProcessHeap();
void * pvBits = HeapAlloc(hHeap, 0, bmi.bmiHeader.biWidth * 4 * bmi.bmiHeader.biHeight);
if (pvBits == 0)
return E_OUTOFMEMORY;
HRESULT hr = E_UNEXPECTED;
if (GetDIBits(hdc, hbmp, 0, bmi.bmiHeader.biHeight, pvBits, &bmi, DIB_RGB_COLORS) == bmi.bmiHeader.biHeight)
{
ULONG cxDelta = cxRow - bmi.bmiHeader.biWidth;
Gdiplus::ARGB *pargbMask = static_cast<Gdiplus::ARGB *>(pvBits);
for (ULONG y = bmi.bmiHeader.biHeight; y; --y)
{
for (ULONG x = bmi.bmiHeader.biWidth; x; --x)
{
if (*pargbMask++)
{
// transparent pixel
*pargb++ = 0;
}
else
{
// opaque pixel
*pargb++ |= 0xFF000000;
}
}
pargb += cxDelta;
}
hr = S_OK;
}
HeapFree(hHeap, 0, pvBits);
return hr;
}