Skip to content

Commit

Permalink
System Icon turn gray when app inactive (#2547) (2)
Browse files Browse the repository at this point in the history
-    Delete the icon in the destructor of CTitleBarHelper
-    Lazy load the gray icon so that it does not need to be created until the application is inactive
  • Loading branch information
sdottaka committed Nov 19, 2024
1 parent 0e1d4f4 commit 05281ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
33 changes: 24 additions & 9 deletions Src/TitleBarHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ CTitleBarHelper::CTitleBarHelper()
{
}

CTitleBarHelper::~CTitleBarHelper()
{
if (m_icon_gray)
DestroyIcon(m_icon_gray);
}

void CTitleBarHelper::Init(CWnd *pWnd)
{
m_pWnd = pWnd;
Expand All @@ -40,8 +46,7 @@ int CTitleBarHelper::GetTopMargin() const

void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc, bool active)
{
LazyLoadIcon(pWnd);
HICON hIcon = active ? m_icon : m_icon_gray;
HICON hIcon = LazyLoadIcon(pWnd, active);
if (hIcon == nullptr)
return;
const int topMargin = GetTopMargin();
Expand Down Expand Up @@ -383,12 +388,22 @@ HICON CTitleBarHelper::CreateGrayIcon(HICON hIcon)
return hGrayIcon;
}

void CTitleBarHelper::LazyLoadIcon(CWnd* pWnd)
HICON CTitleBarHelper::LazyLoadIcon(CWnd* pWnd, bool active)
{
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);
if (active)
{
if (m_icon)
return m_icon;
m_icon = (HICON)pWnd->SendMessage(WM_GETICON, ICON_SMALL2, 0);
if (m_icon == nullptr)
m_icon = (HICON)GetClassLongPtr(pWnd->m_hWnd, GCLP_HICONSM);
return m_icon;
}
else
{
if (m_icon_gray)
return m_icon_gray;
m_icon_gray = (m_icon == nullptr) ? nullptr : CreateGrayIcon(m_icon);
return m_icon_gray;
}
}
3 changes: 2 additions & 1 deletion Src/TitleBarHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class CTitleBarHelper {
public:
CTitleBarHelper();
~CTitleBarHelper();
void Init(CWnd* pWnd);
void DrawIcon(CWnd* pWnd, CDC& dc, bool active);
void DrawButtons(CDC& dc, COLORREF textColor, COLORREF backColor);
Expand Down Expand Up @@ -42,7 +43,7 @@ class CTitleBarHelper {
void ShowSysMenu(CPoint point);
COLORREF GetIntermediateColor(COLORREF a, COLORREF b, float ratio);
HICON CreateGrayIcon(HICON hIcon);
void LazyLoadIcon(CWnd* pWnd);
HICON LazyLoadIcon(CWnd* pWnd, bool active);

CWnd* m_pWnd;
CSize m_size;
Expand Down

0 comments on commit 05281ac

Please sign in to comment.