1515
1616// App
1717#include " WindowIcon.h"
18+ #include " BrushHandleWrapper.h"
1819#include " DeviceContextHandleWrapper.h"
1920#include " Log.h"
2021#include " StringUtility.h"
2122
2223namespace WindowIcon
2324{
2425
25- namespace
26- {
27-
28- bool replaceBitmapMaskColor (HBITMAP hbmp, HBITMAP hmask, COLORREF newColor);
29-
30- } // anonymous namespace
31-
3226HICON get (HWND hwnd)
3327{
3428 HICON hicon;
3529
36- hicon = (HICON)GetClassLongPtr (hwnd, GCLP_HICONSM );
30+ hicon = (HICON)SendMessage (hwnd, WM_GETICON, ICON_SMALL, 0 );
3731 if (hicon) {
3832 return hicon;
3933 }
4034
41- hicon = (HICON)GetClassLongPtr (hwnd, GCLP_HICON );
35+ hicon = (HICON)SendMessage (hwnd, WM_GETICON, ICON_BIG, 0 );
4236 if (hicon) {
4337 return hicon;
4438 }
4539
46- hicon = (HICON)SendMessage (hwnd, WM_GETICON, ICON_SMALL , 0 );
40+ hicon = (HICON)SendMessage (hwnd, WM_GETICON, ICON_SMALL2 , 0 );
4741 if (hicon) {
4842 return hicon;
4943 }
5044
51- hicon = (HICON)SendMessage (hwnd, WM_GETICON, ICON_BIG, 0 );
45+ hicon = (HICON)GetClassLongPtr (hwnd, GCLP_HICONSM );
5246 if (hicon) {
5347 return hicon;
5448 }
5549
56- hicon = (HICON)SendMessage (hwnd, WM_GETICON, ICON_SMALL2, 0 );
50+ hicon = (HICON)GetClassLongPtr (hwnd, GCLP_HICON );
5751 if (hicon) {
5852 return hicon;
5953 }
@@ -73,95 +67,63 @@ HBITMAP bitmap(HWND hwnd)
7367 return nullptr ;
7468 }
7569
76- // return bitmapFromIcon(hicon);
77-
7870 ICONINFOEXA iconInfo;
71+ memset (&iconInfo, 0 , sizeof (iconInfo));
7972 iconInfo.cbSize = sizeof (ICONINFOEXA);
8073 if (!GetIconInfoExA (hicon, &iconInfo)) {
81- return nullptr ;
82- }
83-
84- DWORD menuColor = GetSysColor (COLOR_MENU);
85- COLORREF newColor = RGB (GetBValue (menuColor), GetGValue (menuColor), GetRValue (menuColor));
86- replaceBitmapMaskColor (iconInfo.hbmColor , iconInfo.hbmMask , newColor);
87- DeleteObject (iconInfo.hbmMask );
88-
89- HBITMAP scaledBitmap = (HBITMAP)CopyImage (
90- iconInfo.hbmColor ,
91- IMAGE_BITMAP,
92- GetSystemMetrics (SM_CXMENUCHECK),
93- GetSystemMetrics (SM_CYMENUCHECK),
94- LR_COPYDELETEORG);
95- if (!scaledBitmap) {
96- WARNING_PRINTF (" failed to scale bitmap, CopyImage() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
97- } else {
98- DeleteObject (iconInfo.hbmColor );
99- iconInfo.hbmColor = scaledBitmap;
100- }
101-
102- return iconInfo.hbmColor ;
103- }
104-
105- namespace
106- {
107-
108- bool replaceBitmapMaskColor (HBITMAP hbmp, HBITMAP hmask, COLORREF newColor)
109- {
110- if (!hbmp || !hmask) {
111- return false ;
112- }
113-
114- BITMAP maskBitmap;
115- if (!GetObjectA (hmask, sizeof (BITMAP), &maskBitmap)) {
11674 WARNING_PRINTF (
117- " failed to get mask bitmap object, GetObject() failed: %s\n " ,
75+ " failed to get icon info for %#x, GetIconInfoEx() failed: %s\n " ,
76+ hwnd,
11877 StringUtility::lastErrorString ().c_str ());
119- return false ;
78+ return nullptr ;
12079 }
12180
12281 BITMAP colorBitmap;
123- if (!GetObjectA (hbmp , sizeof (BITMAP), &colorBitmap)) {
82+ if (!GetObjectA (iconInfo. hbmColor , sizeof (BITMAP), &colorBitmap)) {
12483 WARNING_PRINTF (
12584 " failed to get color bitmap object, GetObject() failed: %s\n " ,
12685 StringUtility::lastErrorString ().c_str ());
127- return false ;
86+ return nullptr ;
12887 }
12988
130- DeviceContextHandleWrapper desktopDC (GetDC (HWND_DESKTOP), DeviceContextHandleWrapper::Referenced);
131- if (!desktopDC) {
89+ DeviceContextHandleWrapper displayDC (
90+ CreateICA (" DISPLAY" , nullptr , nullptr , nullptr ),
91+ DeviceContextHandleWrapper::Created);
92+ if (!displayDC) {
13293 WARNING_PRINTF (
133- " failed to get desktop device context, GetDC () failed: %s\n " ,
94+ " failed to get desktop information context, CreateICA () failed: %s\n " ,
13495 StringUtility::lastErrorString ().c_str ());
135- return false ;
96+ return nullptr ;
13697 }
13798
138- DeviceContextHandleWrapper maskDC (CreateCompatibleDC (desktopDC), DeviceContextHandleWrapper::Created);
139- DeviceContextHandleWrapper colorDC (CreateCompatibleDC (desktopDC), DeviceContextHandleWrapper::Created);
140- if (!maskDC || !colorDC) {
99+ int cx = GetSystemMetrics (SM_CXMENUCHECK);
100+ int cy = GetSystemMetrics (SM_CYMENUCHECK);
101+
102+ HBITMAP hbitmap = CreateCompatibleBitmap (displayDC, cx, cy);
103+
104+ DeviceContextHandleWrapper bitmapDC (CreateCompatibleDC (displayDC), DeviceContextHandleWrapper::Created);
105+ if (!bitmapDC) {
141106 WARNING_PRINTF (
142- " failed to create compatible device contexts , CreateCompatibleDC() failed: %s\n " ,
107+ " failed to get desktop device context , CreateCompatibleDC() failed: %s\n " ,
143108 StringUtility::lastErrorString ().c_str ());
144- return false ;
109+ return nullptr ;
145110 }
146111
147- if (!maskDC .selectObject (hmask) || !colorDC. selectObject (hbmp )) {
148- return false ;
112+ if (!bitmapDC .selectObject (hbitmap )) {
113+ return nullptr ;
149114 }
150115
151- bool replaced = false ;
152- for (int y = 0 ; y < maskBitmap.bmHeight ; ++y) {
153- for (int x = 0 ; x < maskBitmap.bmWidth ; ++x) {
154- COLORREF maskPixel = GetPixel (maskDC, x, y);
155- if (maskPixel == RGB (255 , 255 , 255 )) {
156- SetPixel (colorDC, x, y, newColor);
157- replaced = true ;
158- }
159- }
116+ RECT rect = { 0 , 0 , cx, cy };
117+ BrushHandleWrapper brush (CreateSolidBrush (GetSysColor (COLOR_MENU)));
118+ if (!FillRect (bitmapDC, &rect, brush)) {
119+ WARNING_PRINTF (" failed to fill background, FillRect() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
160120 }
161121
162- return replaced;
163- }
122+ if (!DrawIconEx (bitmapDC, 0 , 0 , hicon, cx, cy, 0 , 0 , DI_NORMAL)) {
123+ WARNING_PRINTF (" failed to draw icon, DrawIconEx() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
124+ }
164125
165- } // anonymous namespace
126+ return hbitmap;
127+ }
166128
167129} // namespace WindowIcon
0 commit comments