Skip to content

Commit aeaf91e

Browse files
committed
Delay icon loading and create grayscale version
1 parent fa6650d commit aeaf91e

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

Src/Merge2.rc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// remains consistent on all systems.
1313

1414
IDR_MAINFRAME ICON "res\\Merge.ico"
15-
IDR_MAINFRAME_ICON_GRAY ICON "res\\Merge_gray.ico"
1615
IDR_MERGEPROJECT ICON "res\\MergeProject.ico"
1716
IDR_MERGEDOCTYPE ICON "res\\MergeDoc.ico"
1817
IDR_DIRDOCTYPE ICON "res\\MergeDir.ico"

Src/TitleBarHelper.cpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ CTitleBarHelper::CTitleBarHelper()
2323
, m_bMouseTracking(false)
2424
, m_nTrackingButton(-1)
2525
, m_nHitTest(HTNOWHERE)
26+
, m_icon(nullptr)
27+
, m_icon_gray(nullptr)
2628
{
2729
}
2830

2931
void CTitleBarHelper::Init(CWnd *pWnd)
3032
{
3133
m_pWnd = pWnd;
32-
m_icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
33-
m_icon_gray = AfxGetApp()->LoadIcon(IDR_MAINFRAME_ICON_GRAY);
3434
}
3535

3636
int CTitleBarHelper::GetTopMargin() const
@@ -40,6 +40,7 @@ int CTitleBarHelper::GetTopMargin() const
4040

4141
void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc, bool active)
4242
{
43+
DelayLoadIcon(pWnd);
4344
HICON hIcon = active ? m_icon : m_icon_gray;
4445
if (hIcon == nullptr)
4546
return;
@@ -334,3 +335,61 @@ void CTitleBarHelper::ReloadAccentColor()
334335
{
335336
CAccentColor::Get().Reload();
336337
}
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+
}

Src/TitleBarHelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class CTitleBarHelper {
4141

4242
void ShowSysMenu(CPoint point);
4343
COLORREF GetIntermediateColor(COLORREF a, COLORREF b, float ratio);
44+
HICON CreateGrayIcon(HICON hIcon);
45+
void DelayLoadIcon(CWnd* pWnd);
4446

4547
CWnd* m_pWnd;
4648
CSize m_size;

Src/res/Merge_gray.ico

-66.5 KB
Binary file not shown.

Src/resource.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#define IDR_POPUP_PLUGIN_COMMAND_LINE_MENU 126
3131
#define IDR_POPUP_PLUGIN_ADD_MENU 127
3232
#define IDR_POPUP_DIRVIEW_COMPAREMETHOD 128
33-
#define IDR_MAINFRAME_ICON_GRAY 129
3433
#define IDD_ABOUTBOX 200
3534
#define IDD_OPEN 202
3635
#define IDD_SAVECLOSING 203

0 commit comments

Comments
 (0)