Skip to content

Commit 1aba06d

Browse files
committed
代码重构,将获取内置显示项目数值和示例文本的处理移动到CommonDisplayItem中;修正CPU/内存利用率达到100时超出边界的问题
1 parent f968cb7 commit 1aba06d

File tree

6 files changed

+288
-286
lines changed

6 files changed

+288
-286
lines changed

TrafficMonitor/DisplayItem.cpp

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,249 @@ const wchar_t* CommonDisplayItem::GetItemIniKeyName() const
166166
}
167167
}
168168

169+
CString CommonDisplayItem::GetItemValueText(bool is_main_window) const
170+
{
171+
if (is_plugin)
172+
{
173+
return plugin_item->GetItemValueText();
174+
}
175+
else
176+
{
177+
const PublicSettingData* cfg_data{};
178+
if (is_main_window)
179+
cfg_data = &theApp.m_main_wnd_data;
180+
else
181+
cfg_data = &theApp.m_taskbar_data;
182+
CString str_value;
183+
switch (item_type)
184+
{
185+
//上传、下载、总网速
186+
case TDI_UP:
187+
case TDI_DOWN:
188+
case TDI_TOTAL_SPEED:
189+
{
190+
CString str_in_speed = CCommon::DataSizeToString(theApp.m_in_speed, *cfg_data);
191+
CString str_out_speed = CCommon::DataSizeToString(theApp.m_out_speed, *cfg_data);
192+
CString str_total_speed = CCommon::DataSizeToString(theApp.m_in_speed + theApp.m_out_speed, *cfg_data);
193+
if (!cfg_data->hide_unit || cfg_data->speed_unit == SpeedUnit::AUTO)
194+
{
195+
str_in_speed += _T("/s");
196+
str_out_speed += _T("/s");
197+
str_total_speed += _T("/s");
198+
}
199+
//交换上传和下载位置
200+
if (is_main_window && theApp.m_main_wnd_data.swap_up_down)
201+
std::swap(str_in_speed, str_out_speed);
202+
if (item_type == TDI_UP)
203+
str_value = str_out_speed;
204+
else if (item_type == TDI_DOWN)
205+
str_value = str_in_speed;
206+
else
207+
str_value = str_total_speed;
208+
}
209+
break;
210+
//CPU利用率
211+
case TDI_CPU:
212+
str_value = CCommon::UsageToString(theApp.m_cpu_usage, *cfg_data);
213+
break;
214+
//内存利用率
215+
case TDI_MEMORY:
216+
if (cfg_data->memory_display == MemoryDisplay::MEMORY_USED)
217+
str_value = CCommon::DataSizeToString(static_cast<unsigned long long>(theApp.m_used_memory) * 1024, cfg_data->separate_value_unit_with_space);
218+
else if (cfg_data->memory_display == MemoryDisplay::MEMORY_AVAILABLE)
219+
str_value = CCommon::DataSizeToString((static_cast<unsigned long long>(theApp.m_total_memory) - static_cast<unsigned long long>(theApp.m_used_memory)) * 1024, cfg_data->separate_value_unit_with_space);
220+
else
221+
str_value = CCommon::UsageToString(theApp.m_memory_usage, *cfg_data);
222+
break;
223+
//显卡利用率
224+
case TDI_GPU_USAGE:
225+
str_value = CCommon::UsageToString(theApp.m_gpu_usage, *cfg_data);
226+
break;
227+
//硬盘利用率
228+
case TDI_HDD_USAGE:
229+
str_value = CCommon::UsageToString(theApp.m_hdd_usage, *cfg_data);
230+
break;
231+
//CPU温度
232+
case TDI_CPU_TEMP:
233+
str_value = CCommon::TemperatureToString(theApp.m_cpu_temperature, *cfg_data);
234+
break;
235+
//显卡温度
236+
case TDI_GPU_TEMP:
237+
str_value = CCommon::TemperatureToString(theApp.m_gpu_temperature, *cfg_data);
238+
break;
239+
//硬盘温度
240+
case TDI_HDD_TEMP:
241+
str_value = CCommon::TemperatureToString(theApp.m_hdd_temperature, *cfg_data);
242+
break;
243+
//主板温度
244+
case TDI_MAIN_BOARD_TEMP:
245+
str_value = CCommon::TemperatureToString(theApp.m_main_board_temperature, *cfg_data);
246+
break;
247+
//CPU频率
248+
case TDI_CPU_FREQ:
249+
str_value = CCommon::FreqToString(theApp.m_cpu_freq, *cfg_data);
250+
break;
251+
//总流量
252+
case TDI_TODAY_TRAFFIC:
253+
str_value = CCommon::KBytesToString((theApp.m_today_up_traffic + theApp.m_today_down_traffic) / 1024u);
254+
break;
255+
default:
256+
break;
257+
}
258+
return str_value;
259+
}
260+
}
261+
262+
CString CommonDisplayItem::GetItemValueSampleText(bool is_main_window) const
263+
{
264+
if (is_plugin)
265+
{
266+
return plugin_item->GetItemValueSampleText();
267+
}
268+
//主窗口(用于绘制预览图)
269+
else if (is_main_window)
270+
{
271+
CString sample_str;
272+
switch (item_type)
273+
{
274+
case TDI_UP:
275+
sample_str = _T("88.8 KB/s");
276+
break;
277+
case TDI_DOWN:
278+
sample_str = _T("88.9 KB/s");
279+
break;
280+
case TDI_TOTAL_SPEED:
281+
sample_str = _T("90 KB/s");
282+
break;
283+
case TDI_TODAY_TRAFFIC:
284+
sample_str = _T("100 MB");
285+
break;
286+
case TDI_CPU:
287+
sample_str = _T("50 %");
288+
break;
289+
case TDI_MEMORY:
290+
sample_str = _T("51 %");
291+
break;
292+
case TDI_CPU_TEMP: case TDI_GPU_TEMP: case TDI_HDD_TEMP: case TDI_MAIN_BOARD_TEMP:
293+
sample_str = _T("40 °C");
294+
break;
295+
case TDI_CPU_FREQ:
296+
sample_str = _T("1.0 GHz");
297+
break;
298+
default:
299+
sample_str = _T("99");
300+
break;
301+
}
302+
return sample_str;
303+
}
304+
//任务栏窗口(用于计算任务栏窗口宽度)
305+
else
306+
{
307+
CString sample_str;
308+
switch (item_type)
309+
{
310+
//网速
311+
case TDI_UP:
312+
case TDI_DOWN:
313+
case TDI_TOTAL_SPEED:
314+
{
315+
wstring digits(theApp.m_taskbar_data.digits_number, L'8'); //根据数据位数生成指定个数的“8”
316+
bool hide_unit{ theApp.m_taskbar_data.hide_unit && theApp.m_taskbar_data.speed_unit != SpeedUnit::AUTO };
317+
if (theApp.m_taskbar_data.speed_short_mode)
318+
{
319+
if (hide_unit)
320+
sample_str.Format(_T("%s."), digits.c_str());
321+
else
322+
sample_str.Format(_T("%s.M/s"), digits.c_str());
323+
}
324+
else
325+
{
326+
if (hide_unit)
327+
sample_str.Format(_T("%s.8"), digits.c_str());
328+
else
329+
sample_str.Format(_T("%s.8MB/s"), digits.c_str());
330+
}
331+
if (!hide_unit && theApp.m_taskbar_data.separate_value_unit_with_space)
332+
sample_str += _T(' ');
333+
if (theApp.m_taskbar_data.speed_short_mode && !theApp.m_taskbar_data.unit_byte && !theApp.m_taskbar_data.hide_unit)
334+
sample_str += _T('b');
335+
}
336+
break;
337+
//占用率百分比
338+
case TDI_CPU:
339+
case TDI_MEMORY:
340+
case TDI_GPU_USAGE:
341+
case TDI_HDD_USAGE:
342+
{
343+
//获取当前数值
344+
int value = 0;
345+
if (item_type == TDI_CPU)
346+
value = theApp.m_cpu_usage;
347+
else if (item_type == TDI_MEMORY)
348+
value = theApp.m_memory_usage;
349+
else if (item_type == TDI_GPU_USAGE)
350+
value = theApp.m_gpu_usage;
351+
else if (item_type == TDI_HDD_USAGE)
352+
value = theApp.m_hdd_usage;
353+
354+
//当数值达到100时,使用字符串“100”作为宽度,防止显示不全
355+
if (value >= 100)
356+
sample_str = _T("100");
357+
else
358+
sample_str = _T("99");
359+
if (!theApp.m_taskbar_data.hide_percent)
360+
{
361+
if (theApp.m_taskbar_data.separate_value_unit_with_space)
362+
sample_str += _T(" %");
363+
else
364+
sample_str += _T("%");
365+
}
366+
//内存显示不为已使用百分比时
367+
if (item_type == TDI_MEMORY)
368+
{
369+
if (theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_USED || theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_AVAILABLE)
370+
{
371+
//宽度为总内存的宽度
372+
sample_str = CCommon::DataSizeToString(static_cast<unsigned long long>(theApp.m_total_memory) * 1024, theApp.m_taskbar_data.separate_value_unit_with_space);
373+
}
374+
}
375+
}
376+
break;
377+
//温度
378+
case TDI_CPU_TEMP:
379+
case TDI_GPU_TEMP:
380+
case TDI_HDD_TEMP:
381+
case TDI_MAIN_BOARD_TEMP:
382+
{
383+
if (theApp.m_taskbar_data.separate_value_unit_with_space)
384+
sample_str = _T("99 °C");
385+
else
386+
sample_str = _T("99°C");
387+
}
388+
break;
389+
//CPU频率
390+
case TDI_CPU_FREQ:
391+
{
392+
if (theApp.m_taskbar_data.separate_value_unit_with_space)
393+
sample_str = _T("1.00 GHz");
394+
else
395+
sample_str = _T("1.00GHz");
396+
}
397+
break;
398+
//流量
399+
case TDI_TODAY_TRAFFIC:
400+
{
401+
if (theApp.m_taskbar_data.separate_value_unit_with_space)
402+
sample_str = _T("999.99 MB");
403+
else
404+
sample_str = _T("999.99MB");
405+
}
406+
break;
407+
}
408+
return sample_str;
409+
}
410+
}
411+
169412
///////////////////////////////////////////////////////////////////////////////////////////////
170413
DisplayItemSet::DisplayItemSet(std::initializer_list<DisplayItem> items)
171414
: data(items)

TrafficMonitor/DisplayItem.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ struct CommonDisplayItem
5151

5252
//获取一个显示项目的显示文本保存在ini文件中的key的名称
5353
const wchar_t* GetItemIniKeyName() const;
54+
55+
/**
56+
* @brief 获取一个显示项目的数值文本
57+
* @param is_main_window 如果为true则为主窗口,否则为任务栏窗口
58+
* @return 显示的文本
59+
*/
60+
CString GetItemValueText(bool is_main_window) const;
61+
62+
/**
63+
* @brief 获取一个显示项目的数值示例文本
64+
* @param is_main_window 如果为true则为主窗口,否则为任务栏窗口
65+
* @return 示例文本
66+
*/
67+
CString GetItemValueSampleText(bool is_main_window) const;
5468
};
5569

5670

TrafficMonitor/SkinFile.cpp

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -432,36 +432,7 @@ void CSkinFile::DrawPreview(CDC* pDC, CRect rect)
432432
if (iter->is_plugin)
433433
continue;
434434
DrawStr draw_str;
435-
switch (iter->item_type)
436-
{
437-
case TDI_UP:
438-
draw_str.value = _T("88.8 KB/s");
439-
break;
440-
case TDI_DOWN:
441-
draw_str.value = _T("88.9 KB/s");
442-
break;
443-
case TDI_TOTAL_SPEED:
444-
draw_str.value = _T("90 KB/s");
445-
break;
446-
case TDI_TODAY_TRAFFIC:
447-
draw_str.value = _T("100 MB");
448-
break;
449-
case TDI_CPU:
450-
draw_str.value = _T("50 %");
451-
break;
452-
case TDI_MEMORY:
453-
draw_str.value = _T("51 %");
454-
break;
455-
case TDI_CPU_TEMP: case TDI_GPU_TEMP: case TDI_HDD_TEMP: case TDI_MAIN_BOARD_TEMP:
456-
draw_str.value = _T("40 °C");
457-
break;
458-
case TDI_CPU_FREQ:
459-
draw_str.value = _T("1.0 GHz");
460-
break;
461-
default:
462-
draw_str.value = _T("99");
463-
break;
464-
}
435+
draw_str.value = CommonDisplayItem(iter->item_type).GetItemValueSampleText(true);
465436
if (!m_layout_info.no_label)
466437
{
467438
if (m_setting_data.disp_str.IsInvalid())
@@ -627,64 +598,17 @@ void CSkinFile::DrawInfo(CDC* pDC, bool show_more_info)
627598

628599
void CSkinFile::DrawItemsInfo(IDrawCommon& drawer, Layout& layout, CFont& font) const
629600
{
630-
//获取每个项目显示的文本
601+
//获取每个项目显示的文本和数值文本
631602
std::map<DisplayItem, DrawStr> map_str;
632603
if (!m_layout_info.no_label)
633604
{
634605
for (const auto& display_item : AllDisplayItems)
635606
{
636607
map_str[display_item].label = theApp.m_main_wnd_data.disp_str.GetConst(display_item).c_str();
608+
map_str[display_item].value = CommonDisplayItem(display_item).GetItemValueText(true);
637609
}
638610
}
639611

640-
//上传/下载
641-
CString in_speed = CCommon::DataSizeToString(theApp.m_in_speed, theApp.m_main_wnd_data);
642-
CString out_speed = CCommon::DataSizeToString(theApp.m_out_speed, theApp.m_main_wnd_data);
643-
CString total_speed = CCommon::DataSizeToString(theApp.m_in_speed + theApp.m_out_speed, theApp.m_main_wnd_data);
644-
if (!theApp.m_main_wnd_data.hide_unit || theApp.m_main_wnd_data.speed_unit == SpeedUnit::AUTO)
645-
{
646-
in_speed += _T("/s");
647-
out_speed += _T("/s");
648-
total_speed += _T("/s");
649-
}
650-
map_str[TDI_UP].value = out_speed.GetString();
651-
map_str[TDI_DOWN].value = in_speed.GetString();
652-
map_str[TDI_TOTAL_SPEED].value = total_speed.GetString();
653-
654-
if (theApp.m_main_wnd_data.swap_up_down) //交换上传和下载位置
655-
{
656-
std::swap(map_str[TDI_UP], map_str[TDI_DOWN]);
657-
}
658-
659-
//CPU/内存/显卡利用率
660-
map_str[TDI_CPU].value = CCommon::UsageToString(theApp.m_cpu_usage, theApp.m_main_wnd_data);
661-
662-
map_str[TDI_CPU_FREQ].value = CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_main_wnd_data);
663-
CString str_memory_value;
664-
if (theApp.m_main_wnd_data.memory_display == MemoryDisplay::MEMORY_USED)
665-
str_memory_value = CCommon::DataSizeToString(static_cast<unsigned long long>(theApp.m_used_memory) * 1024, theApp.m_main_wnd_data.separate_value_unit_with_space);
666-
else if (theApp.m_main_wnd_data.memory_display == MemoryDisplay::MEMORY_AVAILABLE)
667-
str_memory_value = CCommon::DataSizeToString((static_cast<unsigned long long>(theApp.m_total_memory) - static_cast<unsigned long long>(theApp.m_used_memory)) * 1024, theApp.m_main_wnd_data.separate_value_unit_with_space);
668-
else
669-
str_memory_value = CCommon::UsageToString(theApp.m_memory_usage, theApp.m_main_wnd_data);
670-
map_str[TDI_MEMORY].value = str_memory_value;
671-
map_str[TDI_GPU_USAGE].value = CCommon::UsageToString(theApp.m_gpu_usage, theApp.m_main_wnd_data);
672-
map_str[TDI_HDD_USAGE].value = CCommon::UsageToString(theApp.m_hdd_usage, theApp.m_main_wnd_data);
673-
674-
//温度
675-
auto getTemperatureStr = [&](DisplayItem display_item, float temperature)
676-
{
677-
map_str[display_item].value = CCommon::TemperatureToString(temperature, theApp.m_main_wnd_data);
678-
};
679-
getTemperatureStr(TDI_CPU_TEMP, theApp.m_cpu_temperature);
680-
getTemperatureStr(TDI_GPU_TEMP, theApp.m_gpu_temperature);
681-
getTemperatureStr(TDI_HDD_TEMP, theApp.m_hdd_temperature);
682-
getTemperatureStr(TDI_MAIN_BOARD_TEMP, theApp.m_main_board_temperature);
683-
684-
//总流量
685-
CString str_traffic = CCommon::KBytesToString((theApp.m_today_up_traffic + theApp.m_today_down_traffic) / 1024u);
686-
map_str[TDI_TODAY_TRAFFIC].value = str_traffic.GetString();
687-
688612
//获取文本颜色
689613
std::map<CommonDisplayItem, COLORREF> text_colors{};
690614
if (theApp.m_main_wnd_data.specify_each_item_color)

0 commit comments

Comments
 (0)