Skip to content

Commit

Permalink
Delay icon loading and create grayscale version
Browse files Browse the repository at this point in the history
  • Loading branch information
lededev committed Nov 19, 2024
1 parent fa6650d commit aeaf91e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
1 change: 0 additions & 1 deletion Src/Merge2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// remains consistent on all systems.

IDR_MAINFRAME ICON "res\\Merge.ico"
IDR_MAINFRAME_ICON_GRAY ICON "res\\Merge_gray.ico"
IDR_MERGEPROJECT ICON "res\\MergeProject.ico"
IDR_MERGEDOCTYPE ICON "res\\MergeDoc.ico"
IDR_DIRDOCTYPE ICON "res\\MergeDir.ico"
Expand Down
63 changes: 61 additions & 2 deletions Src/TitleBarHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ CTitleBarHelper::CTitleBarHelper()
, m_bMouseTracking(false)
, m_nTrackingButton(-1)
, m_nHitTest(HTNOWHERE)
, m_icon(nullptr)
, m_icon_gray(nullptr)
{
}

void CTitleBarHelper::Init(CWnd *pWnd)
{
m_pWnd = pWnd;
m_icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_icon_gray = AfxGetApp()->LoadIcon(IDR_MAINFRAME_ICON_GRAY);
}

int CTitleBarHelper::GetTopMargin() const
Expand All @@ -40,6 +40,7 @@ int CTitleBarHelper::GetTopMargin() const

void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc, bool active)
{
DelayLoadIcon(pWnd);
HICON hIcon = active ? m_icon : m_icon_gray;
if (hIcon == nullptr)
return;
Expand Down Expand Up @@ -334,3 +335,61 @@ void CTitleBarHelper::ReloadAccentColor()
{
CAccentColor::Get().Reload();
}

HICON CTitleBarHelper::CreateGrayIcon(HICON hIcon)
{
ICONINFO iconInfo;
GetIconInfo(hIcon, &iconInfo);

BITMAP bitmap;
GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bitmap);
const int width = bitmap.bmWidth;
const int height = bitmap.bmHeight;
const int pixsize = width * height;

BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;

RGBQUAD* pixels = new RGBQUAD[pixsize];
HDC hdc = GetDC(NULL);
GetDIBits(hdc, iconInfo.hbmColor, 0, height, pixels, &bmi, DIB_RGB_COLORS);

for (int i = 0; i < pixsize; i++)
{
BYTE gray = (BYTE)(0.3 * pixels[i].rgbRed + 0.59 * pixels[i].rgbGreen + 0.11 * pixels[i].rgbBlue);
pixels[i].rgbRed = gray;
pixels[i].rgbGreen = gray;
pixels[i].rgbBlue = gray;
}

HBITMAP hbmGray = CreateCompatibleBitmap(hdc, width, height);
SetDIBits(hdc, hbmGray, 0, height, pixels, &bmi, DIB_RGB_COLORS);

ICONINFO grayIconInfo = iconInfo;
grayIconInfo.hbmColor = hbmGray;
HICON hGrayIcon = CreateIconIndirect(&grayIconInfo);

DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
DeleteObject(hbmGray);
ReleaseDC(NULL, hdc);
delete[] pixels;

return hGrayIcon;
}

void CTitleBarHelper::DelayLoadIcon(CWnd* pWnd)
{
if (m_icon && m_icon_gray)
return;
m_icon = (HICON)pWnd->SendMessage(WM_GETICON, ICON_SMALL2, 0);
if (m_icon == nullptr)
m_icon = (HICON)GetClassLongPtr(pWnd->m_hWnd, GCLP_HICONSM);
m_icon_gray = (m_icon == nullptr) ? nullptr : CreateGrayIcon(m_icon);
}
2 changes: 2 additions & 0 deletions Src/TitleBarHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class CTitleBarHelper {

void ShowSysMenu(CPoint point);
COLORREF GetIntermediateColor(COLORREF a, COLORREF b, float ratio);
HICON CreateGrayIcon(HICON hIcon);
void DelayLoadIcon(CWnd* pWnd);

CWnd* m_pWnd;
CSize m_size;
Expand Down
Binary file removed Src/res/Merge_gray.ico
Binary file not shown.
1 change: 0 additions & 1 deletion Src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#define IDR_POPUP_PLUGIN_COMMAND_LINE_MENU 126
#define IDR_POPUP_PLUGIN_ADD_MENU 127
#define IDR_POPUP_DIRVIEW_COMPAREMETHOD 128
#define IDR_MAINFRAME_ICON_GRAY 129
#define IDD_ABOUTBOX 200
#define IDD_OPEN 202
#define IDD_SAVECLOSING 203
Expand Down

0 comments on commit aeaf91e

Please sign in to comment.