Skip to content

Commit 84509bb

Browse files
committed
Clean up Bitmap
1 parent 3cf8d80 commit 84509bb

File tree

3 files changed

+28
-87
lines changed

3 files changed

+28
-87
lines changed

src/Bitmap.cpp

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
// Standard library
2222
#include <vector>
2323

24-
HBITMAP getResourceBitmap(unsigned int id)
24+
namespace Bitmap
25+
{
26+
27+
HBITMAP getResource(unsigned int id)
2528
{
2629
HINSTANCE hinstance = (HINSTANCE)GetModuleHandle(nullptr);
2730
HBITMAP bitmap = (HBITMAP)LoadImageA(hinstance, MAKEINTRESOURCEA(id), IMAGE_BITMAP, 0, 0, 0);
@@ -35,17 +38,17 @@ HBITMAP getResourceBitmap(unsigned int id)
3538
return bitmap;
3639
}
3740

38-
bool replaceBitmapColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor)
41+
bool replaceColor(HBITMAP hbitmap, COLORREF oldColor, COLORREF newColor)
3942
{
40-
if (!hbmp) {
43+
if (!hbitmap) {
4144
return false;
4245
}
4346

4447
DeviceContextHandleWrapper hdc(GetDC(HWND_DESKTOP), DeviceContextHandleWrapper::Referenced);
4548

4649
BITMAP bitmap;
4750
memset(&bitmap, 0, sizeof(bitmap));
48-
if (!GetObject(hbmp, sizeof(bitmap), &bitmap)) {
51+
if (!GetObject(hbitmap, sizeof(bitmap), &bitmap)) {
4952
WARNING_PRINTF("failed to get bitmap object, GetObject() failed: %s\n", StringUtility::lastErrorString().c_str());
5053
return false;
5154
}
@@ -59,7 +62,7 @@ bool replaceBitmapColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor)
5962
bitmapInfo.bmiHeader.biBitCount = 32;
6063

6164
std::vector<COLORREF> pixels(bitmap.bmWidth * bitmap.bmHeight);
62-
if (!GetDIBits(hdc, hbmp, 0, bitmap.bmHeight, &pixels[0], &bitmapInfo, DIB_RGB_COLORS)) {
65+
if (!GetDIBits(hdc, hbitmap, 0, bitmap.bmHeight, &pixels[0], &bitmapInfo, DIB_RGB_COLORS)) {
6366
WARNING_PRINTF("failed to get bitmap bits, GetDIBits() failed: %s\n", StringUtility::lastErrorString().c_str());
6467
return false;
6568
}
@@ -71,76 +74,11 @@ bool replaceBitmapColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor)
7174
}
7275
}
7376

74-
if (!SetDIBits(hdc, hbmp, 0, bitmap.bmHeight, &pixels[0], &bitmapInfo, DIB_RGB_COLORS)) {
77+
if (!SetDIBits(hdc, hbitmap, 0, bitmap.bmHeight, &pixels[0], &bitmapInfo, DIB_RGB_COLORS)) {
7578
WARNING_PRINTF("failed to set bitmap bits, SetDIBits() failed: %s\n", StringUtility::lastErrorString().c_str());
7679
}
7780

7881
return replaced;
7982
}
8083

81-
bool replaceBitmapMaskColor(HBITMAP hbmp, HBITMAP hmask, COLORREF newColor)
82-
{
83-
if (!hbmp || !hmask) {
84-
return false;
85-
}
86-
87-
BITMAP maskBitmap;
88-
if (!GetObjectA(hmask, sizeof(BITMAP), &maskBitmap)) {
89-
WARNING_PRINTF(
90-
"failed to get mask bitmap object, GetObject() failed: %s\n",
91-
StringUtility::lastErrorString().c_str());
92-
return false;
93-
}
94-
95-
// BITMAP colorBitmap;
96-
// if (!GetObjectA(hbmp, sizeof(BITMAP), &colorBitmap)) {
97-
// WARNING_PRINTF(
98-
// "failed to get color bitmap object, GetObject() failed: %s\n",
99-
// StringUtility::lastErrorString().c_str());
100-
// return false;
101-
// }
102-
103-
DeviceContextHandleWrapper desktopDC(GetDC(HWND_DESKTOP), DeviceContextHandleWrapper::Referenced);
104-
if (!desktopDC) {
105-
WARNING_PRINTF(
106-
"failed to get desktop device context, GetDC() failed: %s\n",
107-
StringUtility::lastErrorString().c_str());
108-
return false;
109-
}
110-
111-
DeviceContextHandleWrapper maskDC(CreateCompatibleDC(desktopDC), DeviceContextHandleWrapper::Created);
112-
DeviceContextHandleWrapper colorDC(CreateCompatibleDC(desktopDC), DeviceContextHandleWrapper::Created);
113-
if (!maskDC || !colorDC) {
114-
WARNING_PRINTF(
115-
"failed to create compatible device contexts, CreateCompatibleDC() failed: %s\n",
116-
StringUtility::lastErrorString().c_str());
117-
return false;
118-
}
119-
120-
if (!maskDC.selectObject(hmask) || !colorDC.selectObject(hbmp)) {
121-
return false;
122-
}
123-
124-
bool replaced = false;
125-
for (int y = 0; y < maskBitmap.bmHeight; ++y) {
126-
for (int x = 0; x < maskBitmap.bmWidth; ++x) {
127-
COLORREF maskPixel = GetPixel(maskDC, x, y);
128-
if (maskPixel == RGB(255, 255, 255)) {
129-
SetPixel(colorDC, x, y, newColor);
130-
replaced = true;
131-
}
132-
}
133-
}
134-
135-
return replaced;
136-
}
137-
138-
HBITMAP scaleBitmap(HBITMAP hbmp, int width, int height)
139-
{
140-
HBITMAP scaledBitmap = (HBITMAP)CopyImage(hbmp, IMAGE_BITMAP, width, height, LR_COPYDELETEORG);
141-
if (!scaledBitmap) {
142-
WARNING_PRINTF("failed to scale bitmap, CopyImage() failed: %s\n", StringUtility::lastErrorString().c_str());
143-
return nullptr;
144-
}
145-
return scaledBitmap;
146-
}
84+
} // namespace Bitmap

src/Bitmap.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
// Windows
1818
#include <Windows.h>
1919

20-
HBITMAP getResourceBitmap(unsigned int id);
21-
bool replaceBitmapColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor);
22-
bool replaceBitmapMaskColor(HBITMAP hbmp, HBITMAP hmask, COLORREF newColor);
23-
HBITMAP scaleBitmap(HBITMAP hbmp, int width, int height);
20+
namespace Bitmap
21+
{
22+
23+
HBITMAP getResource(unsigned int id);
24+
bool replaceColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor);
25+
26+
} // namespace Bitmap

src/ContextMenu.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ bool showContextMenu(HWND hwnd, MinimizePlacement minimizePlacement, bool showWi
139139
return false;
140140
}
141141

142-
BitmapHandleWrapper appBitmap(getResourceBitmap(IDB_APP));
143-
BitmapHandleWrapper minimizeBitmap(getResourceBitmap(IDB_MINIMIZE));
144-
BitmapHandleWrapper restoreBitmap(getResourceBitmap(IDB_RESTORE));
145-
BitmapHandleWrapper settingsBitmap(getResourceBitmap(IDB_SETTINGS));
146-
BitmapHandleWrapper exitBitmap(getResourceBitmap(IDB_EXIT));
142+
BitmapHandleWrapper appBitmap(Bitmap::getResource(IDB_APP));
143+
BitmapHandleWrapper minimizeBitmap(Bitmap::getResource(IDB_MINIMIZE));
144+
BitmapHandleWrapper restoreBitmap(Bitmap::getResource(IDB_RESTORE));
145+
BitmapHandleWrapper settingsBitmap(Bitmap::getResource(IDB_SETTINGS));
146+
BitmapHandleWrapper exitBitmap(Bitmap::getResource(IDB_EXIT));
147147

148148
if (!appBitmap || !minimizeBitmap || !restoreBitmap || !settingsBitmap || !exitBitmap) {
149149
WARNING_PRINTF("failed to load bitmap: %s\n", StringUtility::lastErrorString().c_str());
@@ -153,13 +153,13 @@ bool showContextMenu(HWND hwnd, MinimizePlacement minimizePlacement, bool showWi
153153
DWORD menuColor = GetSysColor(COLOR_MENU);
154154
COLORREF newColor = RGB(GetBValue(menuColor), GetGValue(menuColor), GetRValue(menuColor));
155155

156-
replaceBitmapColor(appBitmap, oldColor1, newColor);
157-
replaceBitmapColor(settingsBitmap, oldColor1, newColor);
158-
replaceBitmapColor(exitBitmap, oldColor1, newColor);
156+
Bitmap::replaceColor(appBitmap, oldColor1, newColor);
157+
Bitmap::replaceColor(settingsBitmap, oldColor1, newColor);
158+
Bitmap::replaceColor(exitBitmap, oldColor1, newColor);
159159

160-
replaceBitmapColor(appBitmap, oldColor2, newColor);
161-
replaceBitmapColor(settingsBitmap, oldColor2, newColor);
162-
replaceBitmapColor(exitBitmap, oldColor2, newColor);
160+
Bitmap::replaceColor(appBitmap, oldColor2, newColor);
161+
Bitmap::replaceColor(settingsBitmap, oldColor2, newColor);
162+
Bitmap::replaceColor(exitBitmap, oldColor2, newColor);
163163

164164
MENUITEMINFOA menuItemInfo;
165165
memset(&menuItemInfo, 0, sizeof(MENUITEMINFOA));

0 commit comments

Comments
 (0)