Skip to content

Commit a9d5df4

Browse files
committed
新增任务栏资源占用图背景颜色跟随Windows主题颜色的功能
1 parent 565e680 commit a9d5df4

17 files changed

+136
-23
lines changed

TrafficMonitor/Common.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,3 +1099,20 @@ bool CCommon::GetNumberBit(unsigned int num, int bit)
10991099
{
11001100
return (num & (1 << bit)) != 0;
11011101
}
1102+
1103+
COLORREF CCommon::GetWindowsThemeColor()
1104+
{
1105+
DWORD crColorization;
1106+
BOOL fOpaqueBlend;
1107+
COLORREF theme_color{};
1108+
HRESULT result = DwmGetColorizationColor(&crColorization, &fOpaqueBlend);
1109+
if (result == S_OK)
1110+
{
1111+
BYTE r, g, b;
1112+
r = (crColorization >> 16) % 256;
1113+
g = (crColorization >> 8) % 256;
1114+
b = crColorization % 256;
1115+
theme_color = RGB(r, g, b);
1116+
}
1117+
return theme_color;
1118+
}

TrafficMonitor/Common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ class CCommon
352352
}
353353
}
354354

355+
//获取Windows主题颜色
356+
static COLORREF GetWindowsThemeColor();
357+
355358
};
356359

357360
/**

TrafficMonitor/CommonData.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,32 @@ unsigned __int64 TaskBarSettingData::GetNetspeedFigureMaxValueInBytes() const
229229
else
230230
return static_cast<unsigned __int64>(netspeed_figure_max_value) * 1024 * 1024;
231231
}
232+
233+
COLORREF TaskBarSettingData::GetUsageGraphColor() const
234+
{
235+
if (graph_color_following_system)
236+
{
237+
COLORREF theme_color = theApp.GetThemeColor();
238+
//转换为HLS
239+
double h, l, s;
240+
CDrawingManager::RGBtoHSL(theme_color, &h, &s, &l);
241+
//根据当前系统深浅色模式指定亮度
242+
if (theApp.m_last_light_mode)
243+
{
244+
//浅色任务栏,将亮度设为0.7
245+
l = 0.7;
246+
}
247+
else
248+
{
249+
//深色任务栏,将亮度设为0.4
250+
l = 0.4;
251+
}
252+
//转换回RGB
253+
COLORREF graph_color = CDrawingManager::HLStoRGB_ONE(h, l, s);
254+
return graph_color;
255+
}
256+
else
257+
{
258+
return status_bar_color;
259+
}
260+
}

TrafficMonitor/CommonData.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ struct TaskBarSettingData : public PublicSettingData
304304
int netspeed_figure_max_value; //网速占用图的最大值
305305
int netspeed_figure_max_value_unit{}; //网速占用图最大值的单位(0: KB, 1: MB)
306306
unsigned __int64 GetNetspeedFigureMaxValueInBytes() const; //获取网速占用图的最大值(以字节为单位)
307+
bool graph_color_following_system{ false }; //占用图颜色跟随系统主题色
308+
COLORREF GetUsageGraphColor() const; //获取占用图的颜色
307309

308310
bool disable_d2d{ false };//是否禁用d2d绘图
309311
DWORD update_layered_window_error_code{0}; // 使用D2D1渲染时,UpdateLayeredWindowIndirect失败的错误代码,会在关闭任务栏窗口时被重置为0

TrafficMonitor/TaskBarDlg.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,12 @@ void CTaskBarDlg::DisableRenderFeatureIfNecessary(CSupportedRenderEnums& ref_sup
606606

607607
void CTaskBarDlg::TryDrawStatusBar(IDrawCommon& drawer, const CRect& rect_bar, int usage_percent)
608608
{
609+
COLORREF graph_color = theApp.m_taskbar_data.GetUsageGraphColor();
609610
CSize fill_size = CSize(rect_bar.Width() * usage_percent / 100, rect_bar.Height());
610611
CRect rect_fill(rect_bar.TopLeft(), fill_size);
611612
if (theApp.m_taskbar_data.show_graph_dashed_box)
612-
drawer.DrawRectOutLine(rect_bar, theApp.m_taskbar_data.status_bar_color, 1, true);
613-
drawer.FillRect(rect_fill, theApp.m_taskbar_data.status_bar_color);
613+
drawer.DrawRectOutLine(rect_bar, graph_color, 1, true);
614+
drawer.FillRect(rect_fill, graph_color);
614615
}
615616

616617
bool CTaskBarDlg::AdjustWindowPos(bool force_adjust)
@@ -1578,8 +1579,9 @@ bool CTaskBarDlg::CheckClickedItem(CPoint point)
15781579
void CTaskBarDlg::TryDrawGraph(IDrawCommon& drawer, const CRect& value_rect, CommonDisplayItem item_type)
15791580
{
15801581
std::list<int>& list = m_map_history_data[item_type];
1582+
COLORREF graph_color = theApp.m_taskbar_data.GetUsageGraphColor();
15811583
if (theApp.m_taskbar_data.show_graph_dashed_box)
1582-
drawer.DrawRectOutLine(value_rect, theApp.m_taskbar_data.status_bar_color, 1, true);
1584+
drawer.DrawRectOutLine(value_rect, graph_color, 1, true);
15831585
int i{ -1 };
15841586
for (const auto& item : list)
15851587
{
@@ -1591,7 +1593,7 @@ void CTaskBarDlg::TryDrawGraph(IDrawCommon& drawer, const CRect& value_rect, Com
15911593
//从右往左画线
15921594
CPoint start_point = CPoint(value_rect.right - i, value_rect.bottom);
15931595
int height = item * value_rect.Height() / 100;
1594-
drawer.DrawLine(start_point, height, theApp.m_taskbar_data.status_bar_color);
1596+
drawer.DrawLine(start_point, height, graph_color);
15951597
}
15961598
}
15971599

TrafficMonitor/TaskBarSettingsDlg.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void CTaskBarSettingsDlg::DrawStaticColor()
6868
}
6969
m_back_color_static.SetFillColor(m_data.back_color);
7070
//m_trans_color_static.SetFillColor(m_data.transparent_color);
71-
m_status_bar_color_static.SetFillColor(m_data.status_bar_color);
71+
m_status_bar_color_static.SetFillColor(m_data.GetUsageGraphColor());
7272
}
7373

7474
void CTaskBarSettingsDlg::IniUnitCombo()
@@ -194,8 +194,9 @@ bool CTaskBarSettingsDlg::InitializeControls()
194194
{ CtrlTextInfo::L2, IDC_NET_SPEED_FIGURE_MAX_VALUE_UNIT_COMBO }
195195
});
196196
RepositionTextBasedControls({
197-
{ CtrlTextInfo::L4, IDC_USAGE_GRAPH_COLOR_STATIC },
198-
{ CtrlTextInfo::L3, IDC_TEXT_COLOR_STATIC3 }
197+
{ CtrlTextInfo::L2, IDC_USAGE_GRAPH_COLOR_STATIC },
198+
{ CtrlTextInfo::L1, IDC_TEXT_COLOR_STATIC3 },
199+
{ CtrlTextInfo::C0, IDC_USAGE_GRAPH_FOLLOW_SYSTEM_CHECK, CtrlTextInfo::W16 }
199200
});
200201
RepositionTextBasedControls({
201202
{ CtrlTextInfo::L4, IDC_GRAPH_DISPLAY_MODE_STATIC },
@@ -272,6 +273,7 @@ BEGIN_MESSAGE_MAP(CTaskBarSettingsDlg, CTabDlg)
272273
ON_BN_CLICKED(IDC_WIN11_SETTINGS_BUTTON, &CTaskBarSettingsDlg::OnBnClickedWin11SettingsButton)
273274
ON_BN_CLICKED(IDC_TASKBAR_WND_IN_SECONDARY_DISPLAY_CHECK, &CTaskBarSettingsDlg::OnBnClickedTaskbarWndInSecondaryDisplayCheck)
274275
ON_CBN_SELCHANGE(IDC_DISPLAY_TO_SHOW_TASKBAR_WND_COMBO, &CTaskBarSettingsDlg::OnCbnSelchangeDisplayToShowTaskbarWndCombo)
276+
ON_BN_CLICKED(IDC_USAGE_GRAPH_FOLLOW_SYSTEM_CHECK, &CTaskBarSettingsDlg::OnBnClickedUsageGraphFollowSystemCheck)
275277
END_MESSAGE_MAP()
276278

277279

@@ -383,6 +385,8 @@ BOOL CTaskBarSettingsDlg::OnInitDialog()
383385
else
384386
CheckDlgButton(IDC_CM_GRAPH_BAR_RADIO, TRUE);
385387
CheckDlgButton(IDC_SHOW_DASHED_BOX, m_data.show_graph_dashed_box);
388+
CheckDlgButton(IDC_USAGE_GRAPH_FOLLOW_SYSTEM_CHECK, m_data.graph_color_following_system);
389+
386390
m_item_space_edit.SetRange(0, 32);
387391
m_item_space_edit.SetValue(m_data.item_space);
388392
CTaskBarDlg* taskbar_dlg{ CTrafficMonitorDlg::Instance()->GetTaskbarWindow() };
@@ -663,6 +667,11 @@ afx_msg LRESULT CTaskBarSettingsDlg::OnStaticClicked(WPARAM wParam, LPARAM lPara
663667
if (colorDlg.DoModal() == IDOK)
664668
{
665669
m_data.status_bar_color = colorDlg.GetColor();
670+
671+
//更改了资源占用图的颜色后,去掉“跟随Windows主题颜色”的勾选
672+
CheckDlgButton(IDC_USAGE_GRAPH_FOLLOW_SYSTEM_CHECK, FALSE);
673+
m_data.graph_color_following_system = false;
674+
666675
DrawStaticColor();
667676
m_style_modified = true;
668677
}
@@ -970,3 +979,10 @@ void CTaskBarSettingsDlg::OnCbnSelchangeDisplayToShowTaskbarWndCombo()
970979

971980
}
972981
}
982+
983+
984+
void CTaskBarSettingsDlg::OnBnClickedUsageGraphFollowSystemCheck()
985+
{
986+
m_data.graph_color_following_system = (IsDlgButtonChecked(IDC_USAGE_GRAPH_FOLLOW_SYSTEM_CHECK) != FALSE);
987+
DrawStaticColor();
988+
}

TrafficMonitor/TaskBarSettingsDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,5 @@ class CTaskBarSettingsDlg : public CTabDlg
122122
afx_msg void OnBnClickedWin11SettingsButton();
123123
afx_msg void OnBnClickedTaskbarWndInSecondaryDisplayCheck();
124124
afx_msg void OnCbnSelchangeDisplayToShowTaskbarWndCombo();
125+
afx_msg void OnBnClickedUsageGraphFollowSystemCheck();
125126
};

TrafficMonitor/TrafficMonitor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ CTrafficMonitorApp::CTrafficMonitorApp()
5151
#endif
5252

5353
CheckWindows11Taskbar();
54+
m_theme_color = CCommon::GetWindowsThemeColor();
5455
}
5556

5657
void CTrafficMonitorApp::LoadLanguageConfig()
@@ -303,6 +304,7 @@ void CTrafficMonitorApp::LoadConfig()
303304
m_taskbar_data.show_netspeed_figure = ini.GetBool(L"task_bar", L"show_netspeed_figure", false);
304305
m_taskbar_data.netspeed_figure_max_value = ini.GetInt(L"task_bar", L"netspeed_figure_max_value", 512);
305306
m_taskbar_data.netspeed_figure_max_value_unit = ini.GetInt(L"task_bar", L"netspeed_figure_max_value_unit", 0);
307+
m_taskbar_data.graph_color_following_system = ini.GetBool(L"task_bar", L"graph_color_following_system", false);
306308

307309
if (CTaskBarDlgDrawCommonSupport::CheckSupport())
308310
m_taskbar_data.disable_d2d = ini.GetBool(L"task_bar", L"disable_d2d", true);
@@ -482,6 +484,7 @@ void CTrafficMonitorApp::SaveConfig()
482484
ini.WriteBool(L"task_bar", L"show_netspeed_figure", m_taskbar_data.show_netspeed_figure);
483485
ini.WriteInt(L"task_bar", L"netspeed_figure_max_value", m_taskbar_data.netspeed_figure_max_value);
484486
ini.WriteInt(L"task_bar", L"netspeed_figure_max_value_unit", m_taskbar_data.netspeed_figure_max_value_unit);
487+
ini.WriteBool(L"task_bar", L"graph_color_following_system", m_taskbar_data.graph_color_following_system);
485488

486489
ini.WriteBool(L"task_bar", L"disable_d2d", m_taskbar_data.disable_d2d);
487490
ini.WriteBool(L"task_bar", L"enable_colorful_emoji", m_taskbar_data.enable_colorful_emoji);
@@ -1360,6 +1363,16 @@ bool CTrafficMonitorApp::DPIFromRect(const RECT& rect, UINT* out_dpi_x, UINT* ou
13601363
return hr == S_OK;
13611364
}
13621365

1366+
COLORREF CTrafficMonitorApp::GetThemeColor() const
1367+
{
1368+
return m_theme_color;
1369+
}
1370+
1371+
void CTrafficMonitorApp::SetThemeColor(COLORREF color)
1372+
{
1373+
m_theme_color = color;
1374+
}
1375+
13631376
void CTrafficMonitorApp::OnHelp()
13641377
{
13651378
// TODO: 在此添加命令处理程序代码

TrafficMonitor/TrafficMonitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ class CTrafficMonitorApp : public CWinApp
167167

168168
bool DPIFromRect(const RECT& rect, UINT* out_dpi_x, UINT* out_dpi_y);
169169

170+
COLORREF GetThemeColor() const;
171+
void SetThemeColor(COLORREF color);
172+
170173
private:
171174
//int m_no_multistart_warning_time{}; //用于设置在开机后多长时间内不弹出“已经有一个程序正在运行”的警告提示
172175
bool m_no_multistart_warning{}; //如果为false,则永远都不会弹出“已经有一个程序正在运行”的警告提示
@@ -180,6 +183,7 @@ class CTrafficMonitorApp : public CWinApp
180183
ULONG_PTR m_gdiplusToken{};
181184

182185
bool m_is_windows11_taskbar{ false }; //是否为Windows11的任务栏
186+
COLORREF m_theme_color{};
183187

184188
// 重写
185189
public:

TrafficMonitor/TrafficMonitor.rc

376 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)