Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title bar background color and text color follow windows settings when not customized #2505

Merged
merged 4 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions Src/Common/MDITabBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "IMDITab.h"
#include "cecolor.h"
#include "RoundedRectWithShadow.h"
#include <dwmapi.h>
#include <RegKey.h>
#pragma comment(lib, "dwmapi.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
Expand Down Expand Up @@ -69,6 +72,15 @@ BOOL CMyTabCtrl::Create(CMDIFrameWnd* pMainFrame, CWnd* pParent)
m_pMainFrame = pMainFrame;
m_tooltips.Create(m_pMainFrame, TTS_NOPREFIX);
m_tooltips.AddTool(this, _T(""));
CRegKeyEx reg;
constexpr tchar_t* AccentColorInactive = _T("AccentColorInactive");
constexpr tchar_t* RegDir = _T("SOFTWARE\\Microsoft\\Windows\\DWM");
if (ERROR_SUCCESS == reg.Open(HKEY_CURRENT_USER, RegDir))
{
const auto clr = reg.ReadDword(AccentColorInactive, 0);
if (clr)
m_dwInactiveTitleColor = RGB(GetRValue(clr), GetGValue(clr), GetBValue(clr));
}
return TRUE;
}

Expand All @@ -86,16 +98,60 @@ BOOL CMyTabCtrl::PreTranslateMessage(MSG* pMsg)
return __super::PreTranslateMessage(pMsg);
}

COLORREF CMyTabCtrl::GetDwmTitlebarColors()
{
if (!m_bActive)
{
if (m_dwInactiveTitleColor)
return m_dwInactiveTitleColor;
return GetSysColor(COLOR_INACTIVECAPTION);
}
DWORD czclr = 0;
BOOL opaqueBlend = FALSE;
HRESULT hr = DwmGetColorizationColor(&czclr, &opaqueBlend);
if (SUCCEEDED(hr))
{
return RGB(static_cast<BYTE>(czclr >> 16), static_cast<BYTE>(czclr >> 8), static_cast<BYTE>(czclr));
}
return GetSysColor(COLOR_ACTIVECAPTION);
}

COLORREF CMyTabCtrl::GetDwmTitleTextColors()
{
if (!m_bActive)
{
COLORREF clr = m_dwInactiveTitleColor ? m_dwInactiveTitleColor : GetSysColor(COLOR_INACTIVECAPTION);
if (GetRValue(clr) < 128 && GetGValue(clr) < 128 && GetBValue(clr) < 128)
return RGB(245, 245, 245);
return RGB(10, 10, 10);
}
DWORD czclr = 0;
BOOL opaqueBlend = FALSE;
HRESULT hr = DwmGetColorizationColor(&czclr, &opaqueBlend);
if (SUCCEEDED(hr))
{
const BYTE r = static_cast<BYTE>(czclr >> 16);
const BYTE g = static_cast<BYTE>(czclr >> 8);
const BYTE b = static_cast<BYTE>(czclr);
if (r < 128 && g < 128 && b < 128)
return RGB(255, 255, 255);
return RGB(0, 0, 0);
}
return GetSysColor(COLOR_CAPTIONTEXT);
}

static inline COLORREF getTextColor()
{
return GetSysColor(COLOR_WINDOWTEXT);
}

COLORREF CMyTabCtrl::GetBackColor() const
COLORREF CMyTabCtrl::GetBackColor()
{
const COLORREF clr = GetSysColor(COLOR_3DFACE);
if (!m_bOnTitleBar)
return clr;
if (!m_bCustomSystemColor)
return GetDwmTitlebarColors();
const COLORREF bgclr = m_bActive ?
RGB(GetRValue(clr), std::clamp(GetGValue(clr) + 8, 0, 255), std::clamp(GetBValue(clr) + 8, 0, 255))
: clr;
Expand Down Expand Up @@ -123,7 +179,9 @@ void CMyTabCtrl::OnPaint()
const int nCount = GetItemCount();
if (nCount == 0)
{
dc.SetTextColor(getTextColor());
const COLORREF winTitleTextColor = (m_bOnTitleBar && !m_bCustomSystemColor) ?
GetDwmTitleTextColors() : GetSysColor(COLOR_WINDOWTEXT);
dc.SetTextColor(winTitleTextColor);
TCHAR szBuf[256];
AfxGetMainWnd()->GetWindowText(szBuf, sizeof(szBuf) / sizeof(szBuf[0]));
dc.DrawText(szBuf, -1, &rcClient, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
Expand Down Expand Up @@ -387,7 +445,9 @@ void CMyTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
}
else
{
SetTextColor(lpDraw->hDC, GetSysColor(COLOR_BTNTEXT));
const COLORREF txtclr = (m_bOnTitleBar && !m_bCustomSystemColor) ?
GetDwmTitleTextColors() : GetSysColor(COLOR_BTNTEXT);
SetTextColor(lpDraw->hDC, txtclr);
}
CSize iconsize(determineIconSize(), determineIconSize());
rc.left += sw + pd + iconsize.cx;
Expand Down
10 changes: 9 additions & 1 deletion Src/Common/MDITabBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CMyTabCtrl : public CTabCtrl
, m_nTooltipTabItemIndex(-1)
, m_bOnTitleBar(false)
, m_bActive(false)
, m_dwInactiveTitleColor(0)
{}

protected:
Expand All @@ -33,11 +34,13 @@ class CMyTabCtrl : public CTabCtrl
bool m_bCloseButtonDown;
bool m_bOnTitleBar;
bool m_bActive;
bool m_bCustomSystemColor;
CRect m_rcCurrentCloseButtom;
int m_nDraggingTabItemIndex;
int m_nTooltipTabItemIndex; /**< Index of the tab displaying tooltip */
CMDIFrameWnd *m_pMainFrame;
CToolTipCtrl m_tooltips; /**< Tooltip for the tab */
COLORREF m_dwInactiveTitleColor;

public:
BOOL Create(CMDIFrameWnd* pMainFrame, CWnd* pParent);
Expand All @@ -47,7 +50,9 @@ class CMyTabCtrl : public CTabCtrl
void SetOnTitleBar(bool onTitleBar) { m_bOnTitleBar = onTitleBar; }
bool GetActive() const { return m_bActive; }
void SetActive(bool bActive) { m_bActive = bActive; }
COLORREF GetBackColor() const;
COLORREF GetBackColor();
bool GetCustomSystemColor() const { return m_bCustomSystemColor; }
void SetCustomSystemColor(bool bCustom) { m_bCustomSystemColor = bCustom; }

// Overrides
// ClassWizard generated virtual function overrides
Expand Down Expand Up @@ -81,6 +86,8 @@ class CMyTabCtrl : public CTabCtrl
void SwapTabs(int nIndexA, int nIndexB);
int GetMaxTitleLength() const;
void UpdateToolTips(int index);
COLORREF GetDwmTitleTextColors();
COLORREF GetDwmTitlebarColors();
};

/**
Expand Down Expand Up @@ -108,6 +115,7 @@ class CMDITabBar : public CControlBar
virtual ~CMDITabBar() {}
BOOL Update(bool bOnTitleBar, bool bMaxmized);
void UpdateActive(bool bActive);
void UpdateCustomSystemColor(bool bCustom) { m_tabCtrl.SetCustomSystemColor(bCustom); };
BOOL Create(CMDIFrameWnd* pParentWnd);
void UpdateTabs() { m_tabCtrl.UpdateTabs(); }
bool GetAutoMaxWidth() const { return m_tabCtrl.GetAutoMaxWidth(); }
Expand Down
7 changes: 5 additions & 2 deletions Src/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
m_bTabsOnTitleBar = GetOptionsMgr()->GetBool(OPT_TABBAR_ON_TITLEBAR);

m_wndTabBar.Update(m_bTabsOnTitleBar.value_or(false), false);
m_wndTabBar.UpdateCustomSystemColor(GetOptionsMgr()->GetBool(OPT_SYSCOLOR_HOOK_ENABLED));

if (m_bTabsOnTitleBar.value_or(false) && !m_wndTabBar.Create(this))
{
Expand Down Expand Up @@ -1229,9 +1230,11 @@ void CMainFrame::OnOptions()
for (auto pImgMergeFrame : GetAllImgMergeFrames())
pImgMergeFrame->RefreshOptions();

if (sysColorHookEnabled != GetOptionsMgr()->GetBool(OPT_SYSCOLOR_HOOK_ENABLED) ||
sysColorsSerialized != GetOptionsMgr()->GetString(OPT_SYSCOLOR_HOOK_COLORS))
const bool optSysColorHookEnabled = GetOptionsMgr()->GetBool(OPT_SYSCOLOR_HOOK_ENABLED);
if (sysColorHookEnabled != optSysColorHookEnabled ||
sysColorsSerialized != GetOptionsMgr()->GetString(OPT_SYSCOLOR_HOOK_COLORS))
{
m_wndTabBar.UpdateCustomSystemColor(optSysColorHookEnabled);
theApp.ReloadCustomSysColors();
AfxGetMainWnd()->SendMessage(WM_SYSCOLORCHANGE);
RedrawWindow(nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE | RDW_ALLCHILDREN);
Expand Down
Loading