Skip to content

Commit

Permalink
新增根据Windows深浅色模式自动切换皮肤的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jan 15, 2025
1 parent 4ef0022 commit 040596f
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 60 deletions.
8 changes: 5 additions & 3 deletions TrafficMonitor/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ const std::map<CommonDisplayItem, wstring>& DispStrings::GetAllItems() const

void DispStrings::operator=(const DispStrings& disp_str)
{
map_str = disp_str.map_str;
std::map<CommonDisplayItem, wstring> tmp = disp_str.map_str;
//如果赋值的字符串是定义的无效字符串,则不赋值
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
for (auto iter = tmp.begin(); iter != tmp.end(); ++iter)
{
if (iter->second == NONE_STR)
iter->second.clear();
iter->second = map_str[iter->first];
}

map_str = tmp;
}

bool DispStrings::IsInvalid() const
Expand Down
5 changes: 5 additions & 0 deletions TrafficMonitor/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ struct MainConfigData
string m_connection_name; //当前选择网络的名称

wstring m_skin_name; //选择的皮肤的名称

bool skin_auto_adapt{ false }; //根据深色/浅色模式自动切换皮肤
wstring skin_name_light_mode; //浅色模式下的皮肤名称
wstring skin_name_dark_mode; //深色模式下的皮肤名称

int m_dft_notify_icon = 0; //默认的通知图标(用于区分win10的深色和浅色模式)
int m_notify_icon_selected{}; //要显示的通知区图标
bool m_notify_icon_auto_adapt{ false }; //通知区图标是否自动适应Win10深浅色模式
Expand Down
78 changes: 78 additions & 0 deletions TrafficMonitor/SkinAutoAdaptSettingDlg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SkinAutoAdaptSettingDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "TrafficMonitor.h"
#include "afxdialogex.h"
#include "SkinAutoAdaptSettingDlg.h"


// CSkinAutoAdaptSettingDlg 对话框

IMPLEMENT_DYNAMIC(CSkinAutoAdaptSettingDlg, CDialog)

CSkinAutoAdaptSettingDlg::CSkinAutoAdaptSettingDlg(const vector<wstring>& skins, CWnd* pParent /*=nullptr*/)
: CDialog(IDD_SKIN_AUTO_ADAPT_DLG, pParent)
, m_skins(skins)
{

}

CSkinAutoAdaptSettingDlg::~CSkinAutoAdaptSettingDlg()
{
}

int CSkinAutoAdaptSettingDlg::FindSkinIndex(const wstring& skin_name)
{
auto iter = std::find(m_skins.begin(), m_skins.end(), skin_name);
if (iter == m_skins.end())
return 0;
return iter - m_skins.begin();
}

void CSkinAutoAdaptSettingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_DARK_MODE_SKIN_COMBO, m_dark_mode_skin_combo);
DDX_Control(pDX, IDC_LIGHT_MODE_SKIN_COMBO, m_light_mode_skin_combo);
}


BEGIN_MESSAGE_MAP(CSkinAutoAdaptSettingDlg, CDialog)
END_MESSAGE_MAP()


// CSkinAutoAdaptSettingDlg 消息处理程序


BOOL CSkinAutoAdaptSettingDlg::OnInitDialog()
{
CDialog::OnInitDialog();

//初始化下拉列表
for (const auto& skin_path : m_skins)
{
wstring skin_name;
size_t index = skin_path.rfind(L'\\');
skin_name = skin_path.substr(index + 1);
m_dark_mode_skin_combo.AddString(skin_name.c_str());
m_light_mode_skin_combo.AddString(skin_name.c_str());
}

int dark_mode_skin = FindSkinIndex(theApp.m_cfg_data.skin_name_dark_mode);
int light_mode_skin = FindSkinIndex(theApp.m_cfg_data.skin_name_light_mode);
m_dark_mode_skin_combo.SetCurSel(dark_mode_skin);
m_light_mode_skin_combo.SetCurSel(light_mode_skin);

return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}


void CSkinAutoAdaptSettingDlg::OnOK()
{
m_dark_mode_skin = m_dark_mode_skin_combo.GetCurSel();
m_light_mode_skin = m_light_mode_skin_combo.GetCurSel();

CDialog::OnOK();
}
41 changes: 41 additions & 0 deletions TrafficMonitor/SkinAutoAdaptSettingDlg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "afxdialogex.h"


// CSkinAutoAdaptSettingDlg 对话框

class CSkinAutoAdaptSettingDlg : public CDialog
{
DECLARE_DYNAMIC(CSkinAutoAdaptSettingDlg)

public:
CSkinAutoAdaptSettingDlg(const vector<wstring>& skins, CWnd* pParent = nullptr); // 标准构造函数
virtual ~CSkinAutoAdaptSettingDlg();

int GetDarkModeSkin() const { return m_dark_mode_skin; }
int GetLightModeSkin() const { return m_light_mode_skin; }

// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_SKIN_AUTO_ADAPT_DLG };
#endif

private:
int FindSkinIndex(const wstring& skin_name);

private:
const vector<wstring>& m_skins;
CComboBox m_dark_mode_skin_combo;
CComboBox m_light_mode_skin_combo;

int m_dark_mode_skin{};
int m_light_mode_skin{};

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
virtual void OnOK();
};
37 changes: 37 additions & 0 deletions TrafficMonitor/SkinDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "TrafficMonitor.h"
#include "SkinDlg.h"
#include "afxdialogex.h"
#include "SkinAutoAdaptSettingDlg.h"


// CSkinDlg 对话框
Expand Down Expand Up @@ -84,6 +85,8 @@ BEGIN_MESSAGE_MAP(CSkinDlg, CBaseDialog)
ON_CBN_SELCHANGE(IDC_COMBO1, &CSkinDlg::OnCbnSelchangeCombo1)
ON_WM_SIZE()
ON_MESSAGE(WM_LINK_CLICKED, &CSkinDlg::OnLinkClicked)
ON_BN_CLICKED(IDC_SKIN_AUTO_ADAPT_BUTTON, &CSkinDlg::OnBnClickedSkinAutoAdaptButton)
ON_BN_CLICKED(IDC_SKIN_AUTO_ADAPT_CHECK, &CSkinDlg::OnBnClickedSkinAutoAdaptCheck)
END_MESSAGE_MAP()


Expand Down Expand Up @@ -122,6 +125,10 @@ BOOL CSkinDlg::OnInitDialog()
//显示预览图片
ShowPreview();

//初始化控件
CheckDlgButton(IDC_SKIN_AUTO_ADAPT_CHECK, theApp.m_cfg_data.skin_auto_adapt);
EnableDlgCtrl(IDC_SKIN_AUTO_ADAPT_BUTTON, theApp.m_cfg_data.skin_auto_adapt);

//设置超链接
m_skin_course.SetURL(_T("https://github.com/zhongyang219/TrafficMonitor/wiki/%E7%9A%AE%E8%82%A4%E5%88%B6%E4%BD%9C%E6%95%99%E7%A8%8B"));
m_skin_download.SetURL(_T("https://github.com/zhongyang219/TrafficMonitorSkin/blob/master/皮肤下载.md"));
Expand Down Expand Up @@ -161,3 +168,33 @@ afx_msg LRESULT CSkinDlg::OnLinkClicked(WPARAM wParam, LPARAM lParam)
}
return 0;
}


void CSkinDlg::OnBnClickedSkinAutoAdaptButton()
{
CSkinAutoAdaptSettingDlg dlg(m_skins);
if (dlg.DoModal() == IDOK)
{
int dark_mode_skin = dlg.GetDarkModeSkin();
int light_mode_skin = dlg.GetLightModeSkin();
if (dark_mode_skin >= 0 && dark_mode_skin < static_cast<int>(m_skins.size()))
theApp.m_cfg_data.skin_name_dark_mode = m_skins[dark_mode_skin];
if (light_mode_skin >= 0 && light_mode_skin < static_cast<int>(m_skins.size()))
theApp.m_cfg_data.skin_name_light_mode = m_skins[light_mode_skin];
}
}


void CSkinDlg::OnOK()
{
theApp.m_cfg_data.skin_auto_adapt = (IsDlgButtonChecked(IDC_SKIN_AUTO_ADAPT_CHECK) != FALSE);

CBaseDialog::OnOK();
}


void CSkinDlg::OnBnClickedSkinAutoAdaptCheck()
{
bool skin_auto_adapt_checked = (IsDlgButtonChecked(IDC_SKIN_AUTO_ADAPT_CHECK) != FALSE);
EnableDlgCtrl(IDC_SKIN_AUTO_ADAPT_BUTTON, skin_auto_adapt_checked);
}
4 changes: 4 additions & 0 deletions TrafficMonitor/SkinDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ class CSkinDlg : public CBaseDialog
afx_msg void OnSize(UINT nType, int cx, int cy);
protected:
afx_msg LRESULT OnLinkClicked(WPARAM wParam, LPARAM lParam);
public:
afx_msg void OnBnClickedSkinAutoAdaptButton();
virtual void OnOK();
afx_msg void OnBnClickedSkinAutoAdaptCheck();
};
10 changes: 10 additions & 0 deletions TrafficMonitor/TrafficMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ void CTrafficMonitorApp::LoadConfig()
m_cfg_data.m_skin_name = ini.GetString(_T("config"), _T("skin_selected"), _T(""));
if (m_cfg_data.m_skin_name.substr(0, 8) == L".\\skins\\") //如果读取到的皮肤名称前面有".\\skins\\",则把它删除。(用于和前一个版本保持兼容性)
m_cfg_data.m_skin_name = m_cfg_data.m_skin_name.substr(7);

m_cfg_data.skin_auto_adapt = ini.GetBool(L"skins", L"skin_auto_adapt", false);
m_cfg_data.skin_name_dark_mode = ini.GetString(L"skins", L"skin_name_dark_mode", L"");
m_cfg_data.skin_name_light_mode = ini.GetString(L"skins", L"skin_name_light_mode", L"");

m_cfg_data.m_notify_icon_selected = ini.GetInt(_T("config"), _T("notify_icon_selected"), (m_win_version.IsWindows7() || m_win_version.IsWindows8Or8point1() ? 2 : m_cfg_data.m_dft_notify_icon)); //Win7/8/8.1默认使用蓝色通知区图标,因为隐藏通知区图标后白色图标会看不清,其他系统默认使用白色图标
m_cfg_data.m_notify_icon_auto_adapt = ini.GetBool(_T("config"), _T("notify_icon_auto_adapt"), true);
if (m_cfg_data.m_notify_icon_auto_adapt)
Expand Down Expand Up @@ -335,6 +340,11 @@ void CTrafficMonitorApp::SaveConfig()
ini.WriteInt(L"config", L"hide_main_window", m_cfg_data.m_hide_main_window);
ini.WriteString(L"connection", L"connection_name", CCommon::StrToUnicode(m_cfg_data.m_connection_name.c_str()));
ini.WriteString(_T("config"), _T("skin_selected"), m_cfg_data.m_skin_name.c_str());

ini.WriteBool(L"skins", L"skin_auto_adapt", m_cfg_data.skin_auto_adapt);
ini.WriteString(L"skins", L"skin_name_dark_mode", m_cfg_data.skin_name_dark_mode);
ini.WriteString(L"skins", L"skin_name_light_mode", m_cfg_data.skin_name_light_mode);

ini.WriteInt(L"config", L"notify_icon_selected", m_cfg_data.m_notify_icon_selected);
ini.WriteBool(L"config", L"notify_icon_auto_adapt", m_cfg_data.m_notify_icon_auto_adapt);

Expand Down
Binary file modified TrafficMonitor/TrafficMonitor.rc
Binary file not shown.
2 changes: 2 additions & 0 deletions TrafficMonitor/TrafficMonitor.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
<ClInclude Include="SelectConnectionsDlg.h" />
<ClInclude Include="SetItemOrderDlg.h" />
<ClInclude Include="SimpleXML.h" />
<ClInclude Include="SkinAutoAdaptSettingDlg.h" />
<ClInclude Include="SkinDlg.h" />
<ClInclude Include="SkinFile.h" />
<ClInclude Include="SpinEdit.h" />
Expand Down Expand Up @@ -654,6 +655,7 @@
<ClCompile Include="SelectConnectionsDlg.cpp" />
<ClCompile Include="SetItemOrderDlg.cpp" />
<ClCompile Include="SimpleXML.cpp" />
<ClCompile Include="SkinAutoAdaptSettingDlg.cpp" />
<ClCompile Include="SkinDlg.cpp" />
<ClCompile Include="SkinFile.cpp" />
<ClCompile Include="SpinEdit.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions TrafficMonitor/TrafficMonitor.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@
<ClInclude Include="WindowsWebExperienceDetector.h">
<Filter>源文件和头文件\公共的类\WindowsWebExperienceDetector</Filter>
</ClInclude>
<ClInclude Include="SkinAutoAdaptSettingDlg.h">
<Filter>源文件和头文件\对话框类\SkinDlg</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="StaticEx.cpp">
Expand Down Expand Up @@ -723,6 +726,9 @@
<ClCompile Include="WindowsWebExperienceDetector.cpp">
<Filter>源文件和头文件\公共的类\WindowsWebExperienceDetector</Filter>
</ClCompile>
<ClCompile Include="SkinAutoAdaptSettingDlg.cpp">
<Filter>源文件和头文件\对话框类\SkinDlg</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="TrafficMonitor.rc">
Expand Down
Loading

0 comments on commit 040596f

Please sign in to comment.