Skip to content

Commit

Permalink
System Icon turn gray when app inactive (WinMerge#2547)
Browse files Browse the repository at this point in the history
* System Icon turn gray when app inactive

* Lazy icon loading and create grayscale version

* Tab indent
  • Loading branch information
lededev authored Nov 19, 2024
1 parent 2537e21 commit 0e1d4f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Src/Common/MDITabBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,6 @@ void CMDITabBar::OnPaint()
if (!m_bOnTitleBar)
return __super::OnPaint();
CPaintDC dc(this);
m_titleBar.DrawIcon(AfxGetMainWnd(), dc);
m_titleBar.DrawIcon(AfxGetMainWnd(), dc, m_tabCtrl.GetActive());
m_titleBar.DrawButtons(dc, CTitleBarHelper::GetTextColor(m_tabCtrl.GetActive()), m_tabCtrl.GetBackColor());
}
66 changes: 62 additions & 4 deletions Src/TitleBarHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ CTitleBarHelper::CTitleBarHelper()
, m_bMouseTracking(false)
, m_nTrackingButton(-1)
, m_nHitTest(HTNOWHERE)
, m_icon(nullptr)
, m_icon_gray(nullptr)
{
}

Expand All @@ -36,11 +38,10 @@ int CTitleBarHelper::GetTopMargin() const
return 0;
}

void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc)
void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc, bool active)
{
HICON hIcon = (HICON)pWnd->SendMessage(WM_GETICON, ICON_SMALL2, 0);
if (hIcon == nullptr)
hIcon = (HICON)GetClassLongPtr(pWnd->m_hWnd, GCLP_HICONSM);
LazyLoadIcon(pWnd);
HICON hIcon = active ? m_icon : m_icon_gray;
if (hIcon == nullptr)
return;
const int topMargin = GetTopMargin();
Expand Down Expand Up @@ -334,3 +335,60 @@ 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 = {0};
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::LazyLoadIcon(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);
}
6 changes: 5 additions & 1 deletion Src/TitleBarHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CTitleBarHelper {
public:
CTitleBarHelper();
void Init(CWnd* pWnd);
void DrawIcon(CWnd* pWnd, CDC& dc);
void DrawIcon(CWnd* pWnd, CDC& dc, bool active);
void DrawButtons(CDC& dc, COLORREF textColor, COLORREF backColor);
int GetTopMargin() const;
int GetLeftMargin() const { return PointToPixel(m_leftMargin); }
Expand All @@ -41,6 +41,8 @@ class CTitleBarHelper {

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

CWnd* m_pWnd;
CSize m_size;
Expand All @@ -51,4 +53,6 @@ class CTitleBarHelper {
unsigned m_nHitTest;
float m_leftMargin;
float m_rightMargin;
HICON m_icon;
HICON m_icon_gray;
};

0 comments on commit 0e1d4f4

Please sign in to comment.