Skip to content

Commit 9556606

Browse files
committed
Fix calc font rectangle width
1 parent 496e050 commit 9556606

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

TrafficMonitor/TaskBarDlg.cpp

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -386,21 +386,20 @@ void CTaskBarDlg::DrawDisplayItem(IDrawCommon& drawer, DisplayItem type, CRect r
386386
format_str = _T("%s");
387387
else
388388
format_str = _T("%s/s");
389-
CString str_in_speed = CCommon::DataSizeToString(theApp.m_in_speed, theApp.m_taskbar_data);
390-
CString str_out_speed = CCommon::DataSizeToString(theApp.m_out_speed, theApp.m_taskbar_data);
391-
CString str_total_speed = CCommon::DataSizeToString(theApp.m_in_speed + theApp.m_out_speed, theApp.m_taskbar_data);
392-
//if (theApp.m_taskbar_data.swap_up_down)
393-
// std::swap(str_in_speed, str_out_speed);
389+
394390
if (type == TDI_UP)
395391
{
392+
CString str_out_speed = CCommon::DataSizeToString(theApp.m_out_speed, theApp.m_taskbar_data);
396393
str_value.Format(format_str, str_out_speed.GetString());
397394
}
398395
else if (type == TDI_DOWN)
399396
{
397+
CString str_in_speed = CCommon::DataSizeToString(theApp.m_in_speed, theApp.m_taskbar_data);
400398
str_value.Format(format_str, str_in_speed.GetString());
401399
}
402400
else
403401
{
402+
CString str_total_speed = CCommon::DataSizeToString(theApp.m_in_speed + theApp.m_out_speed, theApp.m_taskbar_data);
404403
str_value.Format(format_str, str_total_speed.GetString());
405404
}
406405
}
@@ -435,11 +434,6 @@ void CTaskBarDlg::DrawDisplayItem(IDrawCommon& drawer, DisplayItem type, CRect r
435434
break;
436435
}
437436
str_value = CCommon::UsageToString(usage, theApp.m_taskbar_data);
438-
439-
//如果CPU或内存利用率达到100%,会导致显示不全,此时将绘图区域向右扩展一些
440-
int text_width = m_pDC->GetTextExtent(str_value).cx;
441-
if (usage >= 100 && rect_value.Width() < text_width)
442-
rect_value.right = rect_value.left + text_width;
443437
}
444438

445439
//绘制温度
@@ -465,7 +459,8 @@ void CTaskBarDlg::DrawDisplayItem(IDrawCommon& drawer, DisplayItem type, CRect r
465459
}
466460
str_value = CCommon::TemperatureToString(temperature, theApp.m_taskbar_data);
467461
}
468-
else if (type == TDI_CPU_FREQ) {
462+
else if (type == TDI_CPU_FREQ)
463+
{
469464
str_value = CCommon::FreqToString(theApp.m_cpu_freq, theApp.m_taskbar_data);
470465
}
471466

@@ -993,70 +988,97 @@ void CTaskBarDlg::CalculateWindowSize()
993988
//计算显示上传下载部分所需要的宽度
994989
CString sample_str;
995990
int value_width{};
996-
wstring digits(theApp.m_taskbar_data.digits_number, L'8'); //根据数据位数生成指定个数的“8”
991+
992+
//计算常见符号和数字的字符宽度,处理非等宽字符显示问题
993+
static std::vector<CString> char_list = {
994+
_T("0"), _T("1"), _T("2"),
995+
_T("3"), _T("4"), _T("5"),
996+
_T("6"), _T("7"), _T("8"),
997+
_T("9"), _T("%"), _T("."),
998+
};
999+
1000+
int max_char_width = 0;
1001+
for (auto& str : char_list)
1002+
{
1003+
max_char_width += m_pDC->GetTextExtent(str).cx;
1004+
}
1005+
max_char_width = static_cast<int>(ceil(static_cast<float>(max_char_width) / static_cast<float>(char_list.size())));
1006+
9971007
bool hide_unit{ theApp.m_taskbar_data.hide_unit && theApp.m_taskbar_data.speed_unit != SpeedUnit::AUTO };
9981008
if (theApp.m_taskbar_data.speed_short_mode)
9991009
{
10001010
if (hide_unit)
1001-
sample_str.Format(_T("%s."), digits.c_str());
1011+
sample_str = _T(".");
10021012
else
1003-
sample_str.Format(_T("%s.M/s"), digits.c_str());
1013+
sample_str = _T(".M/s");
10041014
}
10051015
else
10061016
{
10071017
if (hide_unit)
1008-
sample_str.Format(_T("%s.8"), digits.c_str());
1018+
sample_str = _T(".8");
10091019
else
1010-
sample_str.Format(_T("%s.8MB/s"), digits.c_str());
1020+
sample_str = _T(".8MB/s");
10111021
}
1022+
10121023
if (!hide_unit && theApp.m_taskbar_data.separate_value_unit_with_space)
10131024
sample_str += _T(' ');
10141025
if (theApp.m_taskbar_data.speed_short_mode && !theApp.m_taskbar_data.unit_byte && !theApp.m_taskbar_data.hide_unit)
10151026
sample_str += _T('b');
1016-
value_width = m_pDC->GetTextExtent(sample_str).cx; //计算使用当前字体显示文本需要的宽度值
1027+
1028+
//计算使用当前字体显示文本需要的宽度值
1029+
value_width = m_pDC->GetTextExtent(sample_str).cx + max_char_width * theApp.m_taskbar_data.digits_number;
10171030
item_widths[TDI_UP].value_width = value_width;
10181031
item_widths[TDI_DOWN].value_width = value_width;
10191032
item_widths[TDI_TOTAL_SPEED].value_width = value_width;
10201033

1021-
//计算显示CPU、内存部分所需要的宽度
1022-
CString str;
1034+
//计算显示CPU、内存部分所需要的宽度(Max 100%)
1035+
int max_temp_char_count;
10231036
if (theApp.m_taskbar_data.hide_percent)
10241037
{
1025-
str = _T("99");
1038+
max_temp_char_count = 3;
10261039
}
10271040
else if (theApp.m_taskbar_data.separate_value_unit_with_space)
10281041
{
1029-
str = _T("99 %");
1042+
max_temp_char_count = 5;
10301043
}
10311044
else
10321045
{
1033-
str = _T("99%");
1046+
max_temp_char_count = 4;
10341047
}
1035-
value_width = m_pDC->GetTextExtent(str).cx;
1048+
value_width = max_char_width * max_temp_char_count;
1049+
10361050
//内存显示的宽度
1051+
CString str;
10371052
int memory_width{ value_width };
10381053
if (theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_USED || theApp.m_taskbar_data.memory_display == MemoryDisplay::MEMORY_AVAILABLE)
10391054
{
10401055
if (theApp.m_taskbar_data.separate_value_unit_with_space)
1041-
str = _T("19.99 GB");
1056+
str = _T(" GB");
10421057
else
1043-
str = _T("19.99GB");
1044-
memory_width = m_pDC->GetTextExtent(str).cx;
1058+
str = _T("GB");
1059+
1060+
// 考虑最大为99.99GB的情况
1061+
max_temp_char_count = 5;
1062+
memory_width = m_pDC->GetTextExtent(str).cx + max_char_width * max_temp_char_count;
10451063
}
1064+
10461065
item_widths[TDI_CPU].value_width = value_width;
10471066
item_widths[TDI_MEMORY].value_width = memory_width;
10481067
item_widths[TDI_GPU_USAGE].value_width = value_width;
10491068
item_widths[TDI_HDD_USAGE].value_width = value_width;
10501069

1051-
item_widths[TDI_CPU_FREQ].value_width = m_pDC->GetTextExtent(_T("1.00 GHz")).cx;
1070+
max_temp_char_count = 4;
1071+
item_widths[TDI_CPU_FREQ].value_width = m_pDC->GetTextExtent(_T(" GHz")).cx + max_char_width * max_temp_char_count;
10521072

10531073
//计算温度显示的宽度
1074+
max_temp_char_count = 2;
10541075
if (theApp.m_taskbar_data.separate_value_unit_with_space)
1055-
str = _T("99 °C");
1076+
str = _T(" °C");
10561077
else
1057-
str = _T("99°C");
1058-
value_width = m_pDC->GetTextExtent(str).cx;
1078+
str = _T("°C");
1079+
value_width = m_pDC->GetTextExtent(str).cx + max_char_width * max_temp_char_count;
10591080
value_width += DPI(2);
1081+
10601082
item_widths[TDI_CPU_TEMP].value_width = value_width;
10611083
item_widths[TDI_GPU_TEMP].value_width = value_width;
10621084
item_widths[TDI_HDD_TEMP].value_width = value_width;

0 commit comments

Comments
 (0)