Skip to content

Commit 7512c76

Browse files
committed
增加xml皮肤文件解析失败时的处理,避免失败时主窗口无法显示,更换皮肤对话框的预览图中显示错误信息
1 parent b4865f8 commit 7512c76

File tree

6 files changed

+126
-100
lines changed

6 files changed

+126
-100
lines changed

TrafficMonitor/SkinFile.cpp

Lines changed: 120 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,20 @@ bool CSkinFile::Load(const wstring& skin_name)
9292
file_path = theApp.m_skin_path + skin_name + L"\\skin.ini";
9393
CFilePathHelper file_path_helper{ file_path };
9494
wstring ext = file_path_helper.GetFileExtension();
95+
m_is_error = false;
9596
if (ext == L"ini")
97+
{
9698
LoadFromIni(file_path);
99+
}
97100
else
98-
LoadFromXml(file_path);
101+
{
102+
if (!LoadFromXml(file_path))
103+
{
104+
LoadFromIni(std::wstring());
105+
m_skin_info.text_color = { RGB(255, 255, 255) };
106+
m_is_error = true;
107+
}
108+
}
99109

100110
CSkinManager::SkinSettingDataFronSkin(m_setting_data, *this);
101111

@@ -144,7 +154,7 @@ bool CSkinFile::Load(const wstring& skin_name)
144154
return CCommon::FileExist(file_path.c_str());
145155
}
146156

147-
void CSkinFile::LoadFromXml(const wstring& file_path)
157+
bool CSkinFile::LoadFromXml(const wstring& file_path)
148158
{
149159
m_skin_info = SkinInfo();
150160
m_layout_info = LayoutInfo();
@@ -255,6 +265,11 @@ void CSkinFile::LoadFromXml(const wstring& file_path)
255265
}
256266
});
257267
}
268+
else
269+
{
270+
//xml解析失败
271+
return false;
272+
}
258273

259274
//载入显示文本
260275
for (const auto& display_text_item : display_text_map)
@@ -282,7 +297,7 @@ void CSkinFile::LoadFromXml(const wstring& file_path)
282297
}
283298
}
284299
}
285-
300+
return true;
286301
}
287302

288303
void CSkinFile::LoadFromIni(const wstring& file_path)
@@ -402,130 +417,136 @@ void CSkinFile::DrawPreview(CDC* pDC, CRect rect)
402417
draw.Create(pDC, nullptr);
403418
//设置字体
404419
draw.SetFont(&m_font);
405-
//绘制背景
406-
CRect rect_s(CPoint(m_preview_info.s_pos.x, m_preview_info.s_pos.y), CSize(m_layout_info.layout_s.width, m_layout_info.layout_s.height));
407-
CRect rect_l(CPoint(m_preview_info.l_pos.x, m_preview_info.l_pos.y), CSize(m_layout_info.layout_l.width, m_layout_info.layout_l.height));
408-
if (IsPNG()) //png背景使用GDI+绘制
420+
if (m_is_error)
409421
{
410-
CDrawCommonEx gdiplus_drawer(pDC);
411-
gdiplus_drawer.DrawImage(m_background_png_s, rect_s.TopLeft(), rect_s.Size(), IDrawCommon::StretchMode::STRETCH);
412-
gdiplus_drawer.DrawImage(m_background_png_l, rect_l.TopLeft(), rect_l.Size(), IDrawCommon::StretchMode::STRETCH);
422+
//显示错误信息
423+
draw.DrawWindowText(rect, CCommon::LoadText(IDS_SKIN_FILE_ERROR_INFO), RGB(252, 128, 45));
413424
}
414425
else
415426
{
416-
if (m_background_s.IsNull())
417-
draw.FillRect(rect_s, RGB(230, 230, 230));
427+
//绘制背景
428+
CRect rect_s(CPoint(m_preview_info.s_pos.x, m_preview_info.s_pos.y), CSize(m_layout_info.layout_s.width, m_layout_info.layout_s.height));
429+
CRect rect_l(CPoint(m_preview_info.l_pos.x, m_preview_info.l_pos.y), CSize(m_layout_info.layout_l.width, m_layout_info.layout_l.height));
430+
if (IsPNG()) //png背景使用GDI+绘制
431+
{
432+
CDrawCommonEx gdiplus_drawer(pDC);
433+
gdiplus_drawer.DrawImage(m_background_png_s, rect_s.TopLeft(), rect_s.Size(), IDrawCommon::StretchMode::STRETCH);
434+
gdiplus_drawer.DrawImage(m_background_png_l, rect_l.TopLeft(), rect_l.Size(), IDrawCommon::StretchMode::STRETCH);
435+
}
418436
else
419-
draw.DrawBitmap(m_background_s, rect_s.TopLeft(), rect_s.Size());
420-
if (m_background_l.IsNull())
421-
draw.FillRect(rect_l, RGB(230, 230, 230));
422-
draw.DrawBitmap(m_background_l, rect_l.TopLeft(), rect_l.Size());
423-
}
424-
425-
std::set<CommonDisplayItem> all_skin_items;
426-
GetSkinDisplayItems(all_skin_items);
427-
428-
//获取每个项目显示的文本
429-
std::map<DisplayItem, DrawStr> map_str;
430-
for (auto iter = all_skin_items.begin(); iter != all_skin_items.end(); ++iter)
431-
{
432-
if (iter->IsPlugin())
433-
continue;
434-
DrawStr draw_str;
435-
draw_str.value = CommonDisplayItem(iter->ItemType()).GetItemValueSampleText(true);
436-
if (!m_layout_info.no_label)
437437
{
438-
if (m_setting_data.disp_str.IsInvalid())
439-
draw_str.label = iter->DefaultString(true).c_str();
438+
if (m_background_s.IsNull())
439+
draw.FillRect(rect_s, RGB(230, 230, 230));
440440
else
441-
draw_str.label = m_setting_data.disp_str.GetConst(*iter).c_str();
441+
draw.DrawBitmap(m_background_s, rect_s.TopLeft(), rect_s.Size());
442+
if (m_background_l.IsNull())
443+
draw.FillRect(rect_l, RGB(230, 230, 230));
444+
draw.DrawBitmap(m_background_l, rect_l.TopLeft(), rect_l.Size());
442445
}
443-
map_str[iter->ItemType()] = draw_str;
444-
}
445446

446-
//获取文本颜色
447-
std::map<CommonDisplayItem, COLORREF> text_colors{};
448-
if (m_setting_data.specify_each_item_color)
449-
{
450-
text_colors = m_setting_data.text_colors;
451-
}
452-
else if (!m_setting_data.text_colors.empty())
453-
{
454-
for (const auto& item : all_skin_items)
447+
std::set<CommonDisplayItem> all_skin_items;
448+
GetSkinDisplayItems(all_skin_items);
449+
450+
//获取每个项目显示的文本
451+
std::map<DisplayItem, DrawStr> map_str;
452+
for (auto iter = all_skin_items.begin(); iter != all_skin_items.end(); ++iter)
455453
{
456-
text_colors[item] = m_setting_data.text_colors.begin()->second;
454+
if (iter->IsPlugin())
455+
continue;
456+
DrawStr draw_str;
457+
draw_str.value = CommonDisplayItem(iter->ItemType()).GetItemValueSampleText(true);
458+
if (!m_layout_info.no_label)
459+
{
460+
if (m_setting_data.disp_str.IsInvalid())
461+
draw_str.label = iter->DefaultString(true).c_str();
462+
else
463+
draw_str.label = m_setting_data.disp_str.GetConst(*iter).c_str();
464+
}
465+
map_str[iter->ItemType()] = draw_str;
457466
}
458-
}
459467

460-
//绘制预览图文本
461-
auto drawPreviewText = [&](Layout& layout, const PreviewInfo::Pos& pos)
462-
{
463-
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
468+
//获取文本颜色
469+
std::map<CommonDisplayItem, COLORREF> text_colors{};
470+
if (m_setting_data.specify_each_item_color)
464471
{
465-
if (layout.layout_items[iter->first].show)
472+
text_colors = m_setting_data.text_colors;
473+
}
474+
else if (!m_setting_data.text_colors.empty())
475+
{
476+
for (const auto& item : all_skin_items)
466477
{
467-
CPoint point;
468-
point.SetPoint(layout.layout_items[iter->first].x, layout.layout_items[iter->first].y);
469-
point.Offset(pos.x, pos.y);
470-
CRect rect(point, CSize(layout.layout_items[iter->first].width, m_layout_info.text_height));
471-
COLORREF text_color{};
472-
text_color = text_colors[iter->first];
473-
DrawSkinText(draw, iter->second, rect, text_color, layout.layout_items[iter->first].align);
478+
text_colors[item] = m_setting_data.text_colors.begin()->second;
474479
}
475480
}
476481

477-
//绘制插件项目
478-
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
479-
{
480-
LayoutItem layout_item = layout.GetItem(plugin_item);
481-
if (layout_item.show)
482+
//绘制预览图文本
483+
auto drawPreviewText = [&](Layout& layout, const PreviewInfo::Pos& pos) {
484+
for (auto iter = map_str.begin(); iter != map_str.end(); ++iter)
482485
{
483-
COLORREF cl{};
484-
auto iter = text_colors.find(plugin_item);
485-
if (iter != text_colors.end())
486-
cl = iter->second;
487-
else if (!text_colors.empty())
488-
cl = text_colors.begin()->second;
489-
//矩形区域
490-
CPoint point;
491-
point.SetPoint(layout_item.x, layout_item.y);
492-
point.Offset(pos.x, pos.y);
493-
CRect rect(point, CSize(layout_item.width, m_layout_info.text_height));
494-
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(plugin_item);
495-
if (plugin != nullptr && plugin->GetAPIVersion() >= 2)
486+
if (layout.layout_items[iter->first].show)
496487
{
497-
plugin->OnExtenedInfo(ITMPlugin::EI_DRAW_TASKBAR_WND, L"0");
488+
CPoint point;
489+
point.SetPoint(layout.layout_items[iter->first].x, layout.layout_items[iter->first].y);
490+
point.Offset(pos.x, pos.y);
491+
CRect rect(point, CSize(layout.layout_items[iter->first].width, m_layout_info.text_height));
492+
COLORREF text_color{};
493+
text_color = text_colors[iter->first];
494+
DrawSkinText(draw, iter->second, rect, text_color, layout.layout_items[iter->first].align);
498495
}
499-
if (plugin_item->IsCustomDraw())
496+
}
497+
498+
//绘制插件项目
499+
for (const auto& plugin_item : theApp.m_plugins.GetPluginItems())
500+
{
501+
LayoutItem layout_item = layout.GetItem(plugin_item);
502+
if (layout_item.show)
500503
{
501-
int brightness{ (GetRValue(cl) + GetGValue(cl) + GetBValue(cl)) / 2 };
504+
COLORREF cl{};
505+
auto iter = text_colors.find(plugin_item);
506+
if (iter != text_colors.end())
507+
cl = iter->second;
508+
else if (!text_colors.empty())
509+
cl = text_colors.begin()->second;
510+
//矩形区域
511+
CPoint point;
512+
point.SetPoint(layout_item.x, layout_item.y);
513+
point.Offset(pos.x, pos.y);
514+
CRect rect(point, CSize(layout_item.width, m_layout_info.text_height));
515+
ITMPlugin* plugin = theApp.m_plugins.GetPluginByItem(plugin_item);
502516
if (plugin != nullptr && plugin->GetAPIVersion() >= 2)
503517
{
504-
plugin->OnExtenedInfo(ITMPlugin::EI_VALUE_TEXT_COLOR, std::to_wstring(cl).c_str());
518+
plugin->OnExtenedInfo(ITMPlugin::EI_DRAW_TASKBAR_WND, L"0");
519+
}
520+
if (plugin_item->IsCustomDraw())
521+
{
522+
int brightness{ (GetRValue(cl) + GetGValue(cl) + GetBValue(cl)) / 2 };
523+
if (plugin != nullptr && plugin->GetAPIVersion() >= 2)
524+
{
525+
plugin->OnExtenedInfo(ITMPlugin::EI_VALUE_TEXT_COLOR, std::to_wstring(cl).c_str());
526+
}
527+
draw.GetDC()->SetTextColor(cl);
528+
plugin_item->DrawItem(draw.GetDC()->GetSafeHdc(), point.x, point.y, layout_item.width, m_layout_info.text_height, brightness >= 128);
505529
}
506-
draw.GetDC()->SetTextColor(cl);
507-
plugin_item->DrawItem(draw.GetDC()->GetSafeHdc(), point.x, point.y, layout_item.width, m_layout_info.text_height, brightness >= 128);
508-
}
509-
else
510-
{
511-
//绘制文本
512-
DrawStr draw_str;
513-
if (m_skin_info.display_text.IsInvalid())
514-
draw_str.label = plugin_item->GetItemLableText();
515530
else
516-
draw_str.label = m_skin_info.display_text.GetConst(plugin_item).c_str();
517-
draw_str.value = plugin_item->GetItemValueSampleText();
518-
DrawSkinText(draw, draw_str, rect, cl, layout_item.align);
531+
{
532+
//绘制文本
533+
DrawStr draw_str;
534+
if (m_skin_info.display_text.IsInvalid())
535+
draw_str.label = plugin_item->GetItemLableText();
536+
else
537+
draw_str.label = m_skin_info.display_text.GetConst(plugin_item).c_str();
538+
draw_str.value = plugin_item->GetItemValueSampleText();
539+
DrawSkinText(draw, draw_str, rect, cl, layout_item.align);
540+
}
519541
}
520542
}
521-
}
543+
};
522544

523-
};
524-
525-
//绘制小预览图文本
526-
drawPreviewText(m_layout_info.layout_s, m_preview_info.s_pos);
527-
//绘制大预览图文本
528-
drawPreviewText(m_layout_info.layout_l, m_preview_info.l_pos);
545+
//绘制小预览图文本
546+
drawPreviewText(m_layout_info.layout_s, m_preview_info.s_pos);
547+
//绘制大预览图文本
548+
drawPreviewText(m_layout_info.layout_l, m_preview_info.l_pos);
549+
}
529550
}
530551

531552
void CSkinFile::DrawInfo(CDC* pDC, bool show_more_info)

TrafficMonitor/SkinFile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class CSkinFile
121121
void GetSkinDisplayItems(std::set<CommonDisplayItem>& skin_all_items) const;
122122

123123
private:
124-
void LoadFromXml(const wstring& file_path); //从xml文件读取皮肤数据
124+
bool LoadFromXml(const wstring& file_path); //从xml文件读取皮肤数据
125125
void LoadFromIni(const wstring& file_path); //从ini文件读取皮肤数据(用于兼容旧版皮肤)
126126

127127
CSkinFile::Layout LayoutFromXmlNode(tinyxml2::XMLElement* ele);
@@ -155,4 +155,5 @@ class CSkinFile
155155
Gdiplus::Image* m_background_png_l{};
156156
int m_alpha{ 255 }; //不透明度,仅当背景为png时有效
157157
SkinSettingData m_setting_data;
158+
bool m_is_error{ false };
158159
};

TrafficMonitor/language.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
#define IDS_RESTORE_FROM_SLEEP_LOG L"IDS_RESTORE_FROM_SLEEP_LOG"
218218
#define IDS_PLUGIN_NEW_VERSION_INFO L"IDS_PLUGIN_NEW_VERSION_INFO"
219219
#define IDS_TRAFFICMONITOR_PLUGIN_NITIFICATION L"IDS_TRAFFICMONITOR_PLUGIN_NITIFICATION"
220+
#define IDS_SKIN_FILE_ERROR_INFO L"IDS_SKIN_FILE_ERROR_INFO"
220221

221222
#define TXT_OK L"TXT_OK"
222223
#define TXT_CANCEL L"TXT_CANCEL"

TrafficMonitor/language/English.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ IDS_SECONDARY_DISPLAY = "Secondary display <%1%>"
221221
IDS_RESTORE_FROM_SLEEP_LOG = "The system has been restored from hibernation, the connection has been reinitialized. (<%1%> times has been reinitialized.)"
222222
IDS_PLUGIN_NEW_VERSION_INFO = "Update available, latest version: <%1%>"
223223
IDS_TRAFFICMONITOR_PLUGIN_NITIFICATION = "TrafficMonitor Plugin Notification"
224+
IDS_SKIN_FILE_ERROR_INFO = "Skin file parse failed!"
224225

225226
TXT_OK = "OK"
226227
TXT_CANCEL = "Cancel"

TrafficMonitor/language/Simplified_Chinese.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ IDS_SECONDARY_DISPLAY = "副显示器 <%1%>"
221221
IDS_RESTORE_FROM_SLEEP_LOG = "系统已从休眠状态恢复,已重新初始化连接。(已重新初始化<%1%>次)"
222222
IDS_PLUGIN_NEW_VERSION_INFO = "有更新,最新版本:<%1%>"
223223
IDS_TRAFFICMONITOR_PLUGIN_NITIFICATION = "TrafficMonitor 插件通知"
224+
IDS_SKIN_FILE_ERROR_INFO = "皮肤文件解析失败!"
224225

225226
TXT_OK = "确定"
226227
TXT_CANCEL = "取消"

TrafficMonitor/language/Traditional_Chinese.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ IDS_SECONDARY_DISPLAY = "副顯示器 <%1%>"
221221
IDS_RESTORE_FROM_SLEEP_LOG = "系統已從休眠狀態恢復,已重新初始化連線。(已重新初始化<%1%>次)"
222222
IDS_PLUGIN_NEW_VERSION_INFO = "有更新,最新版本:<%1%>"
223223
IDS_TRAFFICMONITOR_PLUGIN_NITIFICATION = "TrafficMonitor 外掛通知"
224+
IDS_SKIN_FILE_ERROR_INFO = "面板文件解析失敗!"
224225

225226
TXT_OK = "確定"
226227
TXT_CANCEL = "取消"

0 commit comments

Comments
 (0)