Skip to content

Commit

Permalink
打开了D2D渲染后自动开启“背景透明”并关闭“根据任务栏颜色自动设置背景色”
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Mar 31, 2023
1 parent 809ebc7 commit 496e050
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 60 deletions.
35 changes: 35 additions & 0 deletions TrafficMonitor/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Common.h"
#include "CalendarHelper.h"
#include "TrafficMonitor.h"
#include "WindowsSettingHelper.h"

///////////////////////////////////////////////////////////////////////////////////
int Date::week() const
Expand Down Expand Up @@ -153,6 +154,40 @@ std::set<std::wstring>& StringSet::data()
return string_set;
}

bool TaskBarSettingData::IsTaskbarTransparent() const
{
if (CWindowsSettingHelper::IsWindows10LightTheme() || theApp.m_win_version.IsWindows8Or8point1() || theApp.m_is_windows11_taskbar)
return (transparent_color == back_color);
else
return transparent_color == 0;
}

void TaskBarSettingData::SetTaskabrTransparent(bool transparent)
{
if (transparent)
{
if (CWindowsSettingHelper::IsWindows10LightTheme() || theApp.m_win_version.IsWindows8Or8point1() || theApp.m_is_windows11_taskbar)
{
//浅色模式下要设置任务栏窗口透明,只需将透明色设置成和背景色一样即可
CCommon::TransparentColorConvert(back_color);
transparent_color = back_color;
}
else
{
//深色模式下,背景色透明将透明色设置成黑色
transparent_color = 0;
}
}
else
{
//要设置任务栏窗口不透明,只需将透明色设置成和背景色不一样即可
if (back_color != TASKBAR_TRANSPARENT_COLOR1)
transparent_color = TASKBAR_TRANSPARENT_COLOR1;
else
transparent_color = TASKBAR_TRANSPARENT_COLOR2;
}
}

void TaskBarSettingData::ValidItemSpace()
{
if (item_space < 0)
Expand Down
2 changes: 2 additions & 0 deletions TrafficMonitor/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ struct TaskBarSettingData : public PublicSettingData
int light_default_style{ -1 }; //浅色主题时使用的预设方案
bool auto_set_background_color{ false }; //根据任务栏颜色自动设置背景色
bool auto_save_taskbar_color_settings_to_preset{}; //当启用“自动适应Windows10深色/浅色主题”时,是否在颜色设置有更改时自动将当前颜色设置保存到对应的预设
bool IsTaskbarTransparent() const;
void SetTaskabrTransparent(bool transparent);

CTaskbarItemOrderHelper item_order;
unsigned int m_tbar_display_item{ TDI_UP | TDI_DOWN }; //任务栏窗口显示的项目
Expand Down
2 changes: 1 addition & 1 deletion TrafficMonitor/TaskBarDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ void CTaskBarDlg::MoveWindow(CRect rect)

void CTaskBarDlg::DisableRenderFeatureIfNecessary(CSupportedRenderEnums& ref_supported_render_enums)
{
bool is_transparent = CTaskbarDefaultStyle::IsTaskbarTransparent(theApp.m_taskbar_data);
bool is_transparent = theApp.m_taskbar_data.IsTaskbarTransparent();
// UpdateLayeredWindowIndirect失败则禁用D2D
if (theApp.m_taskbar_data.update_layered_window_error_code)
{
Expand Down
19 changes: 5 additions & 14 deletions TrafficMonitor/TaskBarSettingsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void CTaskBarSettingsDlg::ApplyDefaultStyle(int index)
theApp.m_taskbar_default_style.ApplyDefaultStyle(index, m_data);
DrawStaticColor();
((CButton*)GetDlgItem(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK))->SetCheck(m_data.specify_each_item_color);
m_background_transparent_chk.SetCheck(IsTaskbarTransparent());
m_background_transparent_chk.SetCheck(m_data.IsTaskbarTransparent());
}

void CTaskBarSettingsDlg::ModifyDefaultStyle(int index)
Expand All @@ -114,15 +114,6 @@ void CTaskBarSettingsDlg::EnableControl()
//EnableDlgCtrl(IDC_TASKBAR_WND_SNAP_CHECK, theApp.m_win_version.IsWindows11OrLater() && !m_data.tbar_wnd_on_left);
}

void CTaskBarSettingsDlg::SetTaskabrTransparent(bool transparent)
{
CTaskbarDefaultStyle::SetTaskabrTransparent(transparent, m_data);
}

bool CTaskBarSettingsDlg::IsTaskbarTransparent()
{
return CTaskbarDefaultStyle::IsTaskbarTransparent(m_data);
}

void CTaskBarSettingsDlg::SetControlMouseWheelEnable(bool enable)
{
Expand Down Expand Up @@ -277,7 +268,7 @@ BOOL CTaskBarSettingsDlg::OnInitDialog()
}
((CButton*)GetDlgItem(IDC_HIDE_PERCENTAGE_CHECK))->SetCheck(m_data.hide_percent);
((CButton*)GetDlgItem(IDC_SPECIFY_EACH_ITEM_COLOR_CHECK))->SetCheck(m_data.specify_each_item_color);
m_background_transparent_chk.SetCheck(IsTaskbarTransparent());
m_background_transparent_chk.SetCheck(m_data.IsTaskbarTransparent());
m_atuo_adapt_light_theme_chk.SetCheck(m_data.auto_adapt_light_theme);
m_auto_set_back_color_chk.SetCheck(m_data.auto_set_background_color);
m_auto_set_back_color_chk.EnableWindow(theApp.m_win_version.IsWindows8OrLater());
Expand Down Expand Up @@ -475,7 +466,7 @@ void CTaskBarSettingsDlg::OnOK()
m_data.digits_number = m_digit_number_combo.GetCurSel() + 3;

bool is_taskbar_transparent_checked = (m_background_transparent_chk.GetCheck() != 0);
SetTaskabrTransparent(is_taskbar_transparent_checked);
m_data.SetTaskabrTransparent(is_taskbar_transparent_checked);

SaveColorSettingToDefaultStyle();

Expand Down Expand Up @@ -544,7 +535,7 @@ afx_msg LRESULT CTaskBarSettingsDlg::OnStaticClicked(WPARAM wParam, LPARAM lPara
CMFCColorDialogEx colorDlg(m_data.back_color, 0, this);
if (colorDlg.DoModal() == IDOK)
{
bool background_transparent = IsTaskbarTransparent();
bool background_transparent = m_data.IsTaskbarTransparent();
m_data.back_color = colorDlg.GetColor();
if (m_data.back_color == m_data.text_colors.begin()->second.label)
MessageBox(CCommon::LoadText(IDS_SAME_BACK_TEXT_COLOR_WARNING), NULL, MB_ICONWARNING);
Expand Down Expand Up @@ -701,7 +692,7 @@ void CTaskBarSettingsDlg::OnBnClickedBackgroundTransparentCheck()
{
// TODO: 在此添加控件通知处理程序代码
bool checked = (m_background_transparent_chk.GetCheck() != 0);
SetTaskabrTransparent(checked);
m_data.SetTaskabrTransparent(checked);
m_style_modified = true;
}

Expand Down
3 changes: 0 additions & 3 deletions TrafficMonitor/TaskBarSettingsDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ class CTaskBarSettingsDlg : public CTabDlg

protected:
void EnableControl();

void SetTaskabrTransparent(bool transparent);
bool IsTaskbarTransparent();
virtual void SetControlMouseWheelEnable(bool enable) override;

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
Expand Down
34 changes: 0 additions & 34 deletions TrafficMonitor/TaskbarDefaultStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,40 +110,6 @@ void CTaskbarDefaultStyle::ModifyDefaultStyle(int index, TaskBarSettingData & da
m_default_style[index].specify_each_item_color = data.specify_each_item_color;
}

bool CTaskbarDefaultStyle::IsTaskbarTransparent(const TaskBarSettingData& data)
{
if (CWindowsSettingHelper::IsWindows10LightTheme() || theApp.m_win_version.IsWindows8Or8point1() || theApp.m_is_windows11_taskbar)
return (data.transparent_color == data.back_color);
else
return data.transparent_color == 0;
}

void CTaskbarDefaultStyle::SetTaskabrTransparent(bool transparent, TaskBarSettingData& data)
{
if (transparent)
{
if (CWindowsSettingHelper::IsWindows10LightTheme() || theApp.m_win_version.IsWindows8Or8point1() || theApp.m_is_windows11_taskbar)
{
//浅色模式下要设置任务栏窗口透明,只需将透明色设置成和背景色一样即可
CCommon::TransparentColorConvert(data.back_color);
data.transparent_color = data.back_color;
}
else
{
//深色模式下,背景色透明将透明色设置成黑色
data.transparent_color = 0;
}
}
else
{
//要设置任务栏窗口不透明,只需将透明色设置成和背景色不一样即可
if (data.back_color != TASKBAR_TRANSPARENT_COLOR1)
data.transparent_color = TASKBAR_TRANSPARENT_COLOR1;
else
data.transparent_color = TASKBAR_TRANSPARENT_COLOR2;
}
}

bool CTaskbarDefaultStyle::IsTaskBarStyleDataValid(const TaskBarStyleData& data)
{
for (const auto& item : data.text_colors)
Expand Down
3 changes: 0 additions & 3 deletions TrafficMonitor/TaskbarDefaultStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class CTaskbarDefaultStyle
//static void ApplyDefaultLightStyle(TaskBarSettingData& data); //应用默认的浅色模式预设
void ModifyDefaultStyle(int index, TaskBarSettingData& data); //将当前颜色设置保存到一个预设方案

static bool IsTaskbarTransparent(const TaskBarSettingData& data);
static void SetTaskabrTransparent(bool transparent, TaskBarSettingData& data);

static bool IsTaskBarStyleDataValid(const TaskBarStyleData& data); //判断一个颜色模式是否有效

private:
Expand Down
2 changes: 1 addition & 1 deletion TrafficMonitor/TrafficMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void CTrafficMonitorApp::LoadConfig()
//任务栏窗口设置
m_taskbar_data.back_color = ini.GetInt(_T("task_bar"), _T("task_bar_back_color"), m_taskbar_data.dft_back_color);
m_taskbar_data.transparent_color = ini.GetInt(_T("task_bar"), _T("transparent_color"), m_taskbar_data.dft_transparent_color);
if (CTaskbarDefaultStyle::IsTaskbarTransparent(m_taskbar_data)) //如果任务栏背景透明,则需要将颜色转换一下
if (m_taskbar_data.IsTaskbarTransparent()) //如果任务栏背景透明,则需要将颜色转换一下
{
CCommon::TransparentColorConvert(m_taskbar_data.back_color);
CCommon::TransparentColorConvert(m_taskbar_data.transparent_color);
Expand Down
16 changes: 12 additions & 4 deletions TrafficMonitor/TrafficMonitorDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ void CTrafficMonitorDlg::ApplySettings(COptionsDlg& optionsDlg)
bool is_alow_out_of_border_changed = (optionsDlg.m_tab1_dlg.m_data.m_alow_out_of_border != theApp.m_main_wnd_data.m_alow_out_of_border);
bool is_show_notify_icon_changed = (optionsDlg.m_tab3_dlg.m_data.show_notify_icon != theApp.m_general_data.show_notify_icon);
bool is_connections_hide_changed = (optionsDlg.m_tab3_dlg.m_data.connections_hide.data() != theApp.m_general_data.connections_hide.data());
bool d2d_turned_on = (theApp.m_taskbar_data.disable_d2d && !optionsDlg.m_tab2_dlg.m_data.disable_d2d);

theApp.m_main_wnd_data = optionsDlg.m_tab1_dlg.m_data;
theApp.m_taskbar_data = optionsDlg.m_tab2_dlg.m_data;
Expand All @@ -713,6 +714,13 @@ void CTrafficMonitorDlg::ApplySettings(COptionsDlg& optionsDlg)

SetTextFont();

//打开了D2D渲染后自动开启“背景透明”并关闭“根据任务栏颜色自动设置背景色”
if (d2d_turned_on)
{
theApp.m_taskbar_data.SetTaskabrTransparent(true);
theApp.m_taskbar_data.auto_set_background_color = false;
}

//CTaskBarDlg::SaveConfig();
if (IsTaskbarWndValid())
{
Expand Down Expand Up @@ -1617,10 +1625,10 @@ void CTrafficMonitorDlg::OnTimer(UINT_PTR nIDEvent)
theApp.SaveConfig();
restart_taskbar_dlg = true;
}
bool is_taskbar_transparent{ CTaskbarDefaultStyle::IsTaskbarTransparent(theApp.m_taskbar_data) };
bool is_taskbar_transparent{ theApp.m_taskbar_data.IsTaskbarTransparent()};
if (!is_taskbar_transparent)
{
CTaskbarDefaultStyle::SetTaskabrTransparent(false, theApp.m_taskbar_data);
theApp.m_taskbar_data.SetTaskabrTransparent(false);
restart_taskbar_dlg = true;
}
if (restart_taskbar_dlg && IsTaskbarWndValid())
Expand Down Expand Up @@ -1698,9 +1706,9 @@ void CTrafficMonitorDlg::OnTimer(UINT_PTR nIDEvent)
COLORREF color = ::GetPixel(m_desktop_dc, pointx, pointy); //取任务栏窗口左侧1像素处的颜色作为背景色
if (!CCommon::IsColorSimilar(color, theApp.m_taskbar_data.back_color) && (/*CWindowsSettingHelper::IsWindows10LightTheme() ||*/ color != 0))
{
bool is_taskbar_transparent{ CTaskbarDefaultStyle::IsTaskbarTransparent(theApp.m_taskbar_data) };
bool is_taskbar_transparent{ theApp.m_taskbar_data.IsTaskbarTransparent()};
theApp.m_taskbar_data.back_color = color;
CTaskbarDefaultStyle::SetTaskabrTransparent(is_taskbar_transparent, theApp.m_taskbar_data);
theApp.m_taskbar_data.SetTaskabrTransparent(is_taskbar_transparent);
if (is_taskbar_transparent)
m_tBarDlg->ApplyWindowTransparentColor();
}
Expand Down

0 comments on commit 496e050

Please sign in to comment.