@@ -23,14 +23,14 @@ CTitleBarHelper::CTitleBarHelper()
23
23
, m_bMouseTracking(false)
24
24
, m_nTrackingButton(-1)
25
25
, m_nHitTest(HTNOWHERE)
26
+ , m_icon(nullptr)
27
+ , m_icon_gray(nullptr)
26
28
{
27
29
}
28
30
29
31
void CTitleBarHelper::Init(CWnd *pWnd)
30
32
{
31
33
m_pWnd = pWnd;
32
- m_icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
33
- m_icon_gray = AfxGetApp()->LoadIcon(IDR_MAINFRAME_ICON_GRAY);
34
34
}
35
35
36
36
int CTitleBarHelper::GetTopMargin() const
@@ -40,6 +40,7 @@ int CTitleBarHelper::GetTopMargin() const
40
40
41
41
void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc, bool active)
42
42
{
43
+ DelayLoadIcon(pWnd);
43
44
HICON hIcon = active ? m_icon : m_icon_gray;
44
45
if (hIcon == nullptr)
45
46
return;
@@ -334,3 +335,61 @@ void CTitleBarHelper::ReloadAccentColor()
334
335
{
335
336
CAccentColor::Get().Reload();
336
337
}
338
+
339
+ HICON CTitleBarHelper::CreateGrayIcon(HICON hIcon)
340
+ {
341
+ ICONINFO iconInfo;
342
+ GetIconInfo(hIcon, &iconInfo);
343
+
344
+ BITMAP bitmap;
345
+ GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bitmap);
346
+ const int width = bitmap.bmWidth;
347
+ const int height = bitmap.bmHeight;
348
+ const int pixsize = width * height;
349
+
350
+ BITMAPINFO bmi;
351
+ ZeroMemory(&bmi, sizeof(BITMAPINFO));
352
+ bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
353
+ bmi.bmiHeader.biWidth = width;
354
+ bmi.bmiHeader.biHeight = height;
355
+ bmi.bmiHeader.biPlanes = 1;
356
+ bmi.bmiHeader.biBitCount = 32;
357
+ bmi.bmiHeader.biCompression = BI_RGB;
358
+
359
+ RGBQUAD* pixels = new RGBQUAD[pixsize];
360
+ HDC hdc = GetDC(NULL);
361
+ GetDIBits(hdc, iconInfo.hbmColor, 0, height, pixels, &bmi, DIB_RGB_COLORS);
362
+
363
+ for (int i = 0; i < pixsize; i++)
364
+ {
365
+ BYTE gray = (BYTE)(0.3 * pixels[i].rgbRed + 0.59 * pixels[i].rgbGreen + 0.11 * pixels[i].rgbBlue);
366
+ pixels[i].rgbRed = gray;
367
+ pixels[i].rgbGreen = gray;
368
+ pixels[i].rgbBlue = gray;
369
+ }
370
+
371
+ HBITMAP hbmGray = CreateCompatibleBitmap(hdc, width, height);
372
+ SetDIBits(hdc, hbmGray, 0, height, pixels, &bmi, DIB_RGB_COLORS);
373
+
374
+ ICONINFO grayIconInfo = iconInfo;
375
+ grayIconInfo.hbmColor = hbmGray;
376
+ HICON hGrayIcon = CreateIconIndirect(&grayIconInfo);
377
+
378
+ DeleteObject(iconInfo.hbmColor);
379
+ DeleteObject(iconInfo.hbmMask);
380
+ DeleteObject(hbmGray);
381
+ ReleaseDC(NULL, hdc);
382
+ delete[] pixels;
383
+
384
+ return hGrayIcon;
385
+ }
386
+
387
+ void CTitleBarHelper::DelayLoadIcon(CWnd* pWnd)
388
+ {
389
+ if (m_icon && m_icon_gray)
390
+ return;
391
+ m_icon = (HICON)pWnd->SendMessage(WM_GETICON, ICON_SMALL2, 0);
392
+ if (m_icon == nullptr)
393
+ m_icon = (HICON)GetClassLongPtr(pWnd->m_hWnd, GCLP_HICONSM);
394
+ m_icon_gray = (m_icon == nullptr) ? nullptr : CreateGrayIcon(m_icon);
395
+ }
0 commit comments