Skip to content

Commit 34ebfc4

Browse files
committed
Add pressed state highlight to title bar buttons
1 parent cbcffb5 commit 34ebfc4

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Src/TitleBarHelper.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CTitleBarHelper::CTitleBarHelper()
2222
, m_rightMargin(35.f * 3)
2323
, m_bMouseTracking(false)
2424
, m_nTrackingButton(-1)
25-
, m_nHitTest(HTNOWHERE)
25+
, m_nPressedButton(-1)
2626
, m_icon(nullptr)
2727
, m_icon_gray(nullptr)
2828
{
@@ -120,6 +120,8 @@ void CTitleBarHelper::DrawButtons(CDC& dc, COLORREF textColor, COLORREF backColo
120120
};
121121
COLORREF buttonColor = (brightness < 128) ? adjustColor(backColor, +delta) : adjustColor(backColor, -delta);
122122
colorref = (i == 2) ? RGB(0xE9, 0x48, 0x56) : buttonColor;
123+
if (m_nPressedButton != -1)
124+
colorref = adjustColor(colorref, (brightness < 128) ? +delta : -delta);
123125
}
124126
else
125127
colorref = backColor;
@@ -257,6 +259,7 @@ void CTitleBarHelper::OnNcMouseLeave()
257259
m_pWnd->InvalidateRect(&rcPart, false);
258260
}
259261
m_nTrackingButton = -1;
262+
m_nPressedButton = -1;
260263
}
261264

262265
void CTitleBarHelper::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
@@ -267,21 +270,36 @@ void CTitleBarHelper::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
267270
AfxGetMainWnd()->PostMessage(WM_CLOSE);
268271
}
269272

273+
static int HitTestToButtonIndex(UINT nHitTest)
274+
{
275+
if (nHitTest == HTMINBUTTON)
276+
return 0;
277+
else if (nHitTest == HTMAXBUTTON)
278+
return 1;
279+
else if (nHitTest == HTCLOSE)
280+
return 2;
281+
return -1;
282+
}
283+
270284
void CTitleBarHelper::OnNcLButtonDown(UINT nHitTest, CPoint point)
271285
{
272286
if (nHitTest != HTMINBUTTON && nHitTest != HTMAXBUTTON && nHitTest != HTCLOSE && nHitTest != HTSYSMENU)
273287
AfxGetMainWnd()->SendMessage(WM_NCLBUTTONDOWN, nHitTest, MAKELPARAM(point.x, point.y));
274288
else if (nHitTest == HTSYSMENU)
275289
ShowSysMenu(CPoint{ point.x + 1, point.y });
276290
else if (nHitTest == HTMINBUTTON || nHitTest == HTMAXBUTTON || nHitTest == HTCLOSE)
277-
m_nHitTest = nHitTest;
291+
{
292+
m_nPressedButton = HitTestToButtonIndex(nHitTest);
293+
CRect rcPart = GetButtonRect(m_nPressedButton);
294+
m_pWnd->InvalidateRect(&rcPart, false);
295+
}
278296
}
279297

280298
void CTitleBarHelper::OnNcLButtonUp(UINT nHitTest, CPoint point)
281299
{
282300
if (nHitTest != HTMINBUTTON && nHitTest != HTMAXBUTTON && nHitTest != HTCLOSE && nHitTest != HTSYSMENU)
283301
AfxGetMainWnd()->SendMessage(WM_NCLBUTTONUP, nHitTest, MAKELPARAM(point.x, point.y));
284-
else if (m_nHitTest != HTNOWHERE && m_nHitTest == nHitTest)
302+
else if (m_nPressedButton != -1 && m_nPressedButton == HitTestToButtonIndex(nHitTest))
285303
{
286304
if (nHitTest == HTMINBUTTON)
287305
AfxGetMainWnd()->SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
@@ -290,7 +308,7 @@ void CTitleBarHelper::OnNcLButtonUp(UINT nHitTest, CPoint point)
290308
else if (nHitTest == HTCLOSE)
291309
AfxGetMainWnd()->SendMessage(WM_SYSCOMMAND, SC_CLOSE, 0);
292310
}
293-
m_nHitTest = HTNOWHERE;
311+
m_nPressedButton = -1;
294312
}
295313

296314
void CTitleBarHelper::OnNcRButtonDown(UINT nHitTest, CPoint point)

Src/TitleBarHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CTitleBarHelper {
5151
bool m_bMouseTracking;
5252
int m_nTrackingButton;
5353
int m_dpi;
54-
unsigned m_nHitTest;
54+
int m_nPressedButton;
5555
float m_leftMargin;
5656
float m_rightMargin;
5757
HICON m_icon;

0 commit comments

Comments
 (0)