From 0e1d4f46c5ee6552abcd8cd320c113967547ec02 Mon Sep 17 00:00:00 2001 From: lededev <30518126+lededev@users.noreply.github.com> Date: Tue, 19 Nov 2024 19:43:09 +0800 Subject: [PATCH] System Icon turn gray when app inactive (#2547) * System Icon turn gray when app inactive * Lazy icon loading and create grayscale version * Tab indent --- Src/Common/MDITabBar.cpp | 2 +- Src/TitleBarHelper.cpp | 66 +++++++++++++++++++++++++++++++++++++--- Src/TitleBarHelper.h | 6 +++- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Src/Common/MDITabBar.cpp b/Src/Common/MDITabBar.cpp index a85acc3303a..d9bf92711c1 100644 --- a/Src/Common/MDITabBar.cpp +++ b/Src/Common/MDITabBar.cpp @@ -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()); } diff --git a/Src/TitleBarHelper.cpp b/Src/TitleBarHelper.cpp index 0c4715f4ac2..62e7d4bea06 100644 --- a/Src/TitleBarHelper.cpp +++ b/Src/TitleBarHelper.cpp @@ -23,6 +23,8 @@ CTitleBarHelper::CTitleBarHelper() , m_bMouseTracking(false) , m_nTrackingButton(-1) , m_nHitTest(HTNOWHERE) + , m_icon(nullptr) + , m_icon_gray(nullptr) { } @@ -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(); @@ -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); +} diff --git a/Src/TitleBarHelper.h b/Src/TitleBarHelper.h index b4e0b8eef0c..88c988982b5 100644 --- a/Src/TitleBarHelper.h +++ b/Src/TitleBarHelper.h @@ -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); } @@ -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; @@ -51,4 +53,6 @@ class CTitleBarHelper { unsigned m_nHitTest; float m_leftMargin; float m_rightMargin; + HICON m_icon; + HICON m_icon_gray; };