Skip to content

Commit

Permalink
插件新增自绘显示区域的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Sep 1, 2021
1 parent 6f3acad commit 62a29b9
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 49 deletions.
72 changes: 72 additions & 0 deletions PluginDemo/CustomDrawItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "pch.h"
#include "CustomDrawItem.h"
#include "DataManager.h"

const wchar_t* CCustomDrawItem::GetItemName() const
{
return L"Custom draw item";
}

const wchar_t* CCustomDrawItem::GetItemId() const
{
return L"b4zc373y";
}

const wchar_t* CCustomDrawItem::GetItemLableText() const
{
return nullptr;
}

const wchar_t* CCustomDrawItem::GetItemValueText() const
{
return L"";
}

const wchar_t* CCustomDrawItem::GetItemValueSampleText() const
{
return L"";
}

bool CCustomDrawItem::IsCustomDraw() const
{
return true;
}

int CCustomDrawItem::GetItemWidth() const
{
return 50;
}

void CCustomDrawItem::DrawItem(void* hdc, int x, int y, int w, int h, bool dark_mode)
{
//绘图句柄
HDC hDC = (HDC)hdc;
CDC* pDC = CDC::FromHandle(hDC);
//矩形区域
CRect rect(CPoint(x, y), CSize(w, h));
//设置颜色
COLORREF color1{ dark_mode ? RGB(255, 143, 107) : RGB(173, 42, 0) };
COLORREF color2{ dark_mode ? RGB(183, 241, 96) : RGB(83, 131, 11) };
COLORREF color3{ dark_mode ? RGB(158, 218, 251) : RGB(6, 111, 168) };
//显示时、分、秒的矩形区域
CRect rect1{ rect }, rect2{ rect }, rect3{ rect };
rect1.bottom = rect.top + rect.Height() / 3;
rect2 = rect1;
rect2.MoveToY(rect1.bottom);
rect3.top = rect2.bottom;
rect1.DeflateRect(1, 1);
rect2.DeflateRect(1, 1);
rect3.DeflateRect(1, 1);
//根据当前时间计算矩形的宽度
SYSTEMTIME& time{ CDataManager::Instance().m_system_time };
int hour_width{ time.wHour * w / 24 };
int min_width{ time.wMinute * w / 60 };
int sec_width{ time.wSecond * w / 60 };
rect1.right = rect1.left + hour_width;
rect2.right = rect2.left + min_width;
rect3.right = rect3.left + sec_width;
//填充矩形
pDC->FillSolidRect(rect1, color1);
pDC->FillSolidRect(rect2, color2);
pDC->FillSolidRect(rect3, color3);
}
15 changes: 15 additions & 0 deletions PluginDemo/CustomDrawItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include "PluginInterface.h"

class CCustomDrawItem : public IPluginItem
{
// 通过 IPluginItem 继承
virtual const wchar_t* GetItemName() const override;
virtual const wchar_t* GetItemId() const override;
virtual const wchar_t* GetItemLableText() const override;
virtual const wchar_t* GetItemValueText() const override;
virtual const wchar_t* GetItemValueSampleText() const override;
virtual bool IsCustomDraw() const override;
virtual int GetItemWidth() const override;
virtual void DrawItem(void* hDC, int x, int y, int w, int h, bool dark_mode) override;
};
1 change: 1 addition & 0 deletions PluginDemo/DataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CDataManager
public:
std::wstring m_cur_time;
std::wstring m_cur_date;
SYSTEMTIME m_system_time;
SettingData m_setting_data;

private:
Expand Down
8 changes: 6 additions & 2 deletions PluginDemo/PluginDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ IPluginItem* CPluginDemo::GetItem(int index)
return &m_system_date;
case 1:
return &m_system_time;
case 2:
return &m_custom_draw_item;
default:
break;
}
Expand All @@ -24,8 +26,8 @@ IPluginItem* CPluginDemo::GetItem(int index)
void CPluginDemo::DataRequired()
{
//获取时间和日期
SYSTEMTIME system_time;
GetSystemTime(&system_time);
SYSTEMTIME& system_time{ CDataManager::Instance().m_system_time };
GetLocalTime(&system_time);
wchar_t buff[128];
swprintf_s(buff, L"%d/%.2d/%.2d", system_time.wYear, system_time.wMonth, system_time.wDay);
CDataManager::Instance().m_cur_date = buff;
Expand Down Expand Up @@ -53,6 +55,8 @@ const wchar_t* CPluginDemo::GetInfo(PluginInfoIndex index)
return L"zhongyang219";
case TMI_COPYRIGHT:
return L"Copyright (C) by Zhong Yang 2021";
case TMI_VERSION:
return L"1.0";
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions PluginDemo/PluginDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "PluginInterface.h"
#include "PluginSystemDate.h"
#include "PluginSystemTime.h"
#include "CustomDrawItem.h"

class CPluginDemo : public ITMPlugin
{
Expand All @@ -17,6 +18,7 @@ class CPluginDemo : public ITMPlugin
private:
CPluginSystemDate m_system_date;
CPluginSystemTime m_system_time;
CCustomDrawItem m_custom_draw_item;
};

#ifdef __cplusplus
Expand Down
8 changes: 4 additions & 4 deletions PluginDemo/PluginDemo.rc
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ END

STRINGTABLE
BEGIN
IDS_PLUGIN_NAME "����ʱ����"
IDS_PLUGIN_DESCRIPTION "�ṩ��ʾʱ������ڵ�ʾ�������"
IDS_PLUGIN_NAME "TrafficMonitorʾ�����"
IDS_PLUGIN_DESCRIPTION "����TrafficMonitor��ʾ�������Ϊ�����߿���TrafficMonitor����ṩ������"
END

#endif // ����(���壬�й�) resources
Expand Down Expand Up @@ -170,8 +170,8 @@ END

STRINGTABLE
BEGIN
IDS_PLUGIN_NAME "Date time plugin"
IDS_PLUGIN_DESCRIPTION "Examples of plug-in to provide display time and date."
IDS_PLUGIN_NAME "A sample plug-in for TrafficMonitor."
IDS_PLUGIN_DESCRIPTION "A sample plug-in for TrafficMonitor, providing an example for developers to develop TrafficMonitor plug-ins."
END

#endif // Ӣ��(����) resources
Expand Down
2 changes: 2 additions & 0 deletions PluginDemo/PluginDemo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\include\PluginInterface.h" />
<ClInclude Include="CustomDrawItem.h" />
<ClInclude Include="DataManager.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="OptionsDlg.h" />
Expand All @@ -174,6 +175,7 @@
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CustomDrawItem.cpp" />
<ClCompile Include="DataManager.cpp" />
<ClCompile Include="OptionsDlg.cpp" />
<ClCompile Include="pch.cpp">
Expand Down
6 changes: 6 additions & 0 deletions PluginDemo/PluginDemo.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<ClInclude Include="OptionsDlg.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CustomDrawItem.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand All @@ -62,6 +65,9 @@
<ClCompile Include="PluginDemo.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CustomDrawItem.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="PluginDemo.rc">
Expand Down
11 changes: 7 additions & 4 deletions TrafficMonitor/PluginInfoDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void CPluginInfoDlg::ShowInfo()
if (m_cur_index >= 0 && m_cur_index < static_cast<int>(theApp.m_plugins.GetPlugins().size()))
{
auto& plugin = theApp.m_plugins.GetPlugins()[m_cur_index];
m_info_list.SetItemText(RI_NAME, 1, plugin.name.c_str());
m_info_list.SetItemText(RI_DESCRIPTION, 1, plugin.description.c_str());
m_info_list.SetItemText(RI_NAME, 1, plugin.properties.at(ITMPlugin::TMI_NAME).c_str());
m_info_list.SetItemText(RI_DESCRIPTION, 1, plugin.properties.at(ITMPlugin::TMI_DESCRIPTION).c_str());
m_info_list.SetItemText(RI_FILE_NAME, 1, CFilePathHelper(plugin.file_path).GetFileName().c_str());
m_info_list.SetItemText(RI_FILE_PATH, 1, plugin.file_path.c_str());
m_info_list.SetItemText(RI_ITEM_NUM, 1, std::to_wstring(plugin.plugin_items.size()).c_str());
Expand All @@ -47,8 +47,9 @@ void CPluginInfoDlg::ShowInfo()
if (!plugin.plugin_items.empty())
item_names.pop_back();
m_info_list.SetItemText(RI_ITEM_NAMES, 1, item_names.c_str());
m_info_list.SetItemText(RI_AUTHOR, 1, plugin.author.c_str());
m_info_list.SetItemText(RI_COPYRIGHT, 1, plugin.copyright.c_str());
m_info_list.SetItemText(RI_AUTHOR, 1, plugin.properties.at(ITMPlugin::TMI_AUTHOR).c_str());
m_info_list.SetItemText(RI_COPYRIGHT, 1, plugin.properties.at(ITMPlugin::TMI_COPYRIGHT).c_str());
m_info_list.SetItemText(RI_VERSION, 1, plugin.properties.at(ITMPlugin::TMI_VERSION).c_str());
}

//显示当前选择指示
Expand Down Expand Up @@ -209,6 +210,8 @@ CString CPluginInfoDlg::GetRowName(int row_index)
return CCommon::LoadText(IDS_AUTHOR);
case CPluginInfoDlg::RI_COPYRIGHT:
return CCommon::LoadText(IDS_COPYRIGHT);
case CPluginInfoDlg::RI_VERSION:
return CCommon::LoadText(IDS_VERSION);
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions TrafficMonitor/PluginInfoDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CPluginInfoDlg : public CBaseDialog
RI_ITEM_NAMES,
RI_AUTHOR,
RI_COPYRIGHT,
RI_VERSION,
RI_MAX
};

Expand Down
9 changes: 5 additions & 4 deletions TrafficMonitor/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ void CPluginManager::LoadPlugins()
if (plugin_info.plugin == nullptr)
continue;
//获取插件信息
plugin_info.name = WcharArrayToWString(plugin_info.plugin->GetInfo(ITMPlugin::TMI_NAME));
plugin_info.description = WcharArrayToWString(plugin_info.plugin->GetInfo(ITMPlugin::TMI_DESCRIPTION));
plugin_info.author = WcharArrayToWString(plugin_info.plugin->GetInfo(ITMPlugin::TMI_AUTHOR));
plugin_info.copyright = WcharArrayToWString(plugin_info.plugin->GetInfo(ITMPlugin::TMI_COPYRIGHT));
for (int i{}; i < ITMPlugin::TMI_MAX; i++)
{
ITMPlugin::PluginInfoIndex index{ static_cast<ITMPlugin::PluginInfoIndex>(i) };
plugin_info.properties[index] = WcharArrayToWString(plugin_info.plugin->GetInfo(index));
}

//获取插件显示项目
int index = 0;
Expand Down
6 changes: 2 additions & 4 deletions TrafficMonitor/PluginManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "PluginInterface.h"
#include <memory>
#include <map>

typedef ITMPlugin* (*pfTMPluginCreateInstance)();

Expand All @@ -26,10 +27,7 @@ class CPluginManager
std::vector<IPluginItem*> plugin_items; //插件提供的所有显示项目
PluginState state{}; //插件的状态
DWORD error_code{}; //错误代码(GetLastError的返回值)
wstring name;
wstring description;
wstring author;
wstring copyright;
std::map<ITMPlugin::PluginInfoIndex, std::wstring> properties; //插件属性
};

CPluginManager();
Expand Down
2 changes: 1 addition & 1 deletion TrafficMonitor/PluginManagerDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ BOOL CPluginManagerDlg::OnInitDialog()
}
int index = m_list_ctrl.GetItemCount();
m_list_ctrl.InsertItem(index, file_name.c_str());
m_list_ctrl.SetItemText(index, 1, plugin.name.c_str());
m_list_ctrl.SetItemText(index, 1, plugin.properties.at(ITMPlugin::TMI_NAME).c_str());
m_list_ctrl.SetItemText(index, 2, status);
}

Expand Down
76 changes: 47 additions & 29 deletions TrafficMonitor/TaskBarDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,38 +364,48 @@ void CTaskBarDlg::DrawPluginItem(CDrawCommon& drawer, IPluginItem* item, CRect r
{
if (item == nullptr)
return;
COLORREF text_color = theApp.m_taskbar_data.dft_text_colors;
if (!theApp.m_taskbar_data.text_colors.empty())
if (item->IsCustomDraw())
{
text_color = theApp.m_taskbar_data.text_colors.begin()->second.label;
//根据背景色的亮度判断深色还是浅色模式
const COLORREF& bk{ theApp.m_taskbar_data.back_color };
int background_brightness{ (GetRValue(bk) + GetGValue(bk) + GetBValue(bk)) / 3 };
//由插件自绘
item->DrawItem(drawer.GetDC()->GetSafeHdc(), rect.left, rect.top, rect.Width(), rect.Height(), background_brightness < 128);
}

CRect rect_label, rect_value;
rect_label = rect_value = rect;
if (label_width > 0)
else
{
if (!vertical)
COLORREF text_color = theApp.m_taskbar_data.dft_text_colors;
if (!theApp.m_taskbar_data.text_colors.empty())
{
rect_label = rect_value = rect;
rect_label.right = rect_label.left + label_width;
rect_value.left = rect_label.right;
text_color = theApp.m_taskbar_data.text_colors.begin()->second.label;
}
else

CRect rect_label, rect_value;
rect_label = rect_value = rect;
if (label_width > 0)
{
rect_label.bottom = rect_label.top + rect.Height() / 2;
rect_value.top = rect_label.bottom;
if (!vertical)
{
rect_label = rect_value = rect;
rect_label.right = rect_label.left + label_width;
rect_value.left = rect_label.right;
}
else
{
rect_label.bottom = rect_label.top + rect.Height() / 2;
rect_value.top = rect_label.bottom;
}
}
//画标签
CString lable_text = item->GetItemLableText();
lable_text += L' ';
drawer.DrawWindowText(rect_label, lable_text, text_color, (vertical ? Alignment::CENTER : Alignment::LEFT));
//画数值
Alignment value_alignment{ theApp.m_taskbar_data.value_right_align ? Alignment::RIGHT : Alignment::LEFT }; //数值的对齐方式
if (vertical)
value_alignment = Alignment::CENTER;
drawer.DrawWindowText(rect_value, item->GetItemValueText(), text_color, value_alignment);
}
//画标签
CString lable_text = item->GetItemLableText();
lable_text += L' ';
drawer.DrawWindowText(rect_label, lable_text, text_color, (vertical ? Alignment::CENTER : Alignment::LEFT));
//画数值
Alignment value_alignment{ theApp.m_taskbar_data.value_right_align ? Alignment::RIGHT : Alignment::LEFT }; //数值的对齐方式
if (vertical)
value_alignment = Alignment::CENTER;
drawer.DrawWindowText(rect_value, item->GetItemValueText(), text_color, value_alignment);

}

void CTaskBarDlg::MoveWindow(CRect rect)
Expand Down Expand Up @@ -768,11 +778,19 @@ void CTaskBarDlg::CalculateWindowSize()
ItemWidthInfo width_info;
width_info.is_plugin = true;
width_info.plugin_item = plugin;
CString lable_text = plugin->GetItemLableText();
if (!lable_text.IsEmpty())
lable_text += L' ';
width_info.item_width.label_width = m_pDC->GetTextExtent(lable_text).cx;
width_info.item_width.value_width = m_pDC->GetTextExtent(plugin->GetItemValueSampleText()).cx;
if (plugin->IsCustomDraw())
{
width_info.item_width.label_width = 0;
width_info.item_width.value_width = theApp.DPI(plugin->GetItemWidth());
}
else
{
CString lable_text = plugin->GetItemLableText();
if (!lable_text.IsEmpty())
lable_text += L' ';
width_info.item_width.label_width = m_pDC->GetTextExtent(lable_text).cx;
width_info.item_width.value_width = m_pDC->GetTextExtent(plugin->GetItemValueSampleText()).cx;
}
m_item_widths.push_back(width_info);
}
}
Expand Down
Binary file modified TrafficMonitor/TrafficMonitor.rc
Binary file not shown.
1 change: 1 addition & 0 deletions TrafficMonitor/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
#define IDS_PLUGIN_NAME 362
#define IDS_DISABLED 363
#define IDS_RESTART_TO_APPLY_CHANGE_INFO 364
#define IDS_VERSION 365
#define IDC_STATIC_INFO 1001
#define IDC_STATIC1 1002
#define IDC_STATIC_DOWN 1002
Expand Down
Loading

0 comments on commit 62a29b9

Please sign in to comment.