Fix(server): size status bar panes from measured text
The FRP/runtime/expiry panes kept fixed 250/180/180 pixel widths while the text is drawn with a system font that changes with language, display scaling and accessibility text size. Where the font is larger the expiry text is cut inside the pane: "到期: 2026-12-31 (41天)" shows the date but drops the remaining days, and the wider English "Expires: 2026-12-31 (41d)" stays cut on another machine. Measure each pane's text as wide characters (the status bar control is Unicode internally; ANSI measurement is up to 10% narrower for CJK) against several candidate fonts - the pane's own font, SYSTEM_FONT, DEFAULT_GUI_FONT, lfStatusFont and lfMessageFont - and take the widest. Widths never drop below the previously proven 250/180/180, so 100% scaling layouts are unchanged, and the expiry pane gets an extra 20px with a 220px floor for the longer English string. A new LayoutStatusPanes() applies the widths in one place and, when their sum exceeds the bar, hides panes in priority order (runtime -> FRP -> MCP -> TLS) so the last pane cannot be clipped at the window edge. OnSize, the periodic refresh and the FRP auto-proxy start/stop all go through it now. Verified with Release x64 builds; visual confirmation on the affected machines is still pending. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
de9e9a7369
commit
798456032e
2 files changed
+132
-49
No files matched your search
@@ -363,35 +363,70 @@ static UINT Indicators[] = {
|
||||
IDR_STATUSBAR_EXPIRE
|
||||
};
|
||||
|
||||
// 状态栏短标签分区(TLS/MCP)的宽度:按当前语言下两种取值的实际文本宽度取大者,
|
||||
// 再留出左右边距。硬编码像素在不同语言/DPI 下容易被截断或浪费空间。
|
||||
// 状态栏分区宽度:按当前语言/DPI 下的实际文本宽度加左右边距。硬编码像素在
|
||||
// 125%/150% 缩放或换字体时会把文字截掉(「到期: ... (41天)」只剩日期就是这个原因)。
|
||||
//
|
||||
// 文本按宽字符测量:状态栏控件内部是 Unicode,ANSI 串会先按系统代码页转成宽字符再
|
||||
// 绘制。中文串两种方式差可达 10%(本机 134 vs 153px),按 ANSI 测会偏窄。
|
||||
static int MeasureStatusTextWide(HDC dc, const CString& text)
|
||||
{
|
||||
const int len = text.GetLength();
|
||||
if (len <= 0)
|
||||
return 0;
|
||||
const int n = ::MultiByteToWideChar(CP_ACP, 0, text, len, nullptr, 0);
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
std::wstring w((size_t)n, L'\0');
|
||||
::MultiByteToWideChar(CP_ACP, 0, text, len, &w[0], n);
|
||||
SIZE sz = {};
|
||||
::GetTextExtentPoint32W(dc, w.c_str(), n, &sz);
|
||||
return (int)sz.cx;
|
||||
}
|
||||
|
||||
// 难点是「控件到底用哪个字体画」。状态栏没有显式设过字体时,MFC 自己按 DC 默认字体
|
||||
// (SYSTEM_FONT)计算分区宽度,而系统「状态栏字体」(lfStatusFont)往往更窄——本机
|
||||
// 实测同一串文字 SYSTEM_FONT 134px、状态栏字体 109px。只按后者测就会把分区算窄、
|
||||
// 把文字截掉,所以这里几种候选字体都测一遍取最大值。
|
||||
static int CalcStatusPaneWidth(const CStatusBar& bar, const CString& text)
|
||||
{
|
||||
if (text.IsEmpty())
|
||||
return 0;
|
||||
|
||||
CClientDC dc((CWnd*)&bar);
|
||||
HGDIOBJ candidates[5] = {};
|
||||
int count = 0;
|
||||
|
||||
if (CFont* pFont = bar.GetFont()) // 应用显式设过字体:控件就用它
|
||||
candidates[count++] = (HGDIOBJ)pFont->GetSafeHandle();
|
||||
candidates[count++] = ::GetStockObject(SYSTEM_FONT); // 无字体时的绘制/测量默认
|
||||
candidates[count++] = ::GetStockObject(DEFAULT_GUI_FONT); // 另一种常见的默认
|
||||
|
||||
CFont statusFont, messageFont; // 系统字体表里的两种可能
|
||||
NONCLIENTMETRICS ncm = {};
|
||||
ncm.cbSize = sizeof(ncm);
|
||||
if (::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0)) {
|
||||
if (statusFont.CreateFontIndirect(&ncm.lfStatusFont))
|
||||
candidates[count++] = (HGDIOBJ)statusFont.GetSafeHandle();
|
||||
if (messageFont.CreateFontIndirect(&ncm.lfMessageFont))
|
||||
candidates[count++] = (HGDIOBJ)messageFont.GetSafeHandle();
|
||||
}
|
||||
|
||||
int width = 0;
|
||||
for (int i = 0; i < count; ++i) {
|
||||
HGDIOBJ old = dc.SelectObject(candidates[i]);
|
||||
width = max(width, MeasureStatusTextWide(dc, text));
|
||||
if (old != nullptr)
|
||||
dc.SelectObject(old);
|
||||
}
|
||||
return width + 20;
|
||||
}
|
||||
|
||||
// 状态栏短标签分区(TLS/MCP)的宽度:按当前语言下各取值的实际文本宽度取大者。
|
||||
static int CalcStatusFlagPaneWidth(const CStatusBar& bar, const CString& a, const CString& b,
|
||||
const CString& c = CString())
|
||||
{
|
||||
CClientDC dc((CWnd*)&bar);
|
||||
CFont* pOld = nullptr;
|
||||
CFont* pFont = bar.GetFont();
|
||||
// 状态栏没有显式设过字体时,绘制用的是系统的「状态栏字体」(Segoe UI 9pt),
|
||||
// 比 DEFAULT_GUI_FONT 宽约 8%:100% 缩放下靠边距还能兜住,缩放比更高就会截断,
|
||||
// 所以这里直接按状态栏字体测量。
|
||||
CFont statusFont;
|
||||
if (pFont == nullptr) {
|
||||
NONCLIENTMETRICS ncm = {};
|
||||
ncm.cbSize = sizeof(ncm);
|
||||
if (::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0)
|
||||
&& statusFont.CreateFontIndirect(&ncm.lfStatusFont))
|
||||
pFont = &statusFont;
|
||||
}
|
||||
if (pFont == nullptr)
|
||||
pFont = CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT));
|
||||
if (pFont != nullptr)
|
||||
pOld = dc.SelectObject(pFont);
|
||||
CSize szA = dc.GetTextExtent(a);
|
||||
CSize szB = dc.GetTextExtent(b);
|
||||
CSize szC = c.IsEmpty() ? CSize(0, 0) : dc.GetTextExtent(c);
|
||||
if (pOld != nullptr)
|
||||
dc.SelectObject(pOld);
|
||||
return max(max(szA.cx, szB.cx), szC.cx) + 16;
|
||||
return max(max(CalcStatusPaneWidth(bar, a), CalcStatusPaneWidth(bar, b)),
|
||||
CalcStatusPaneWidth(bar, c));
|
||||
}
|
||||
|
||||
std::string EventName()
|
||||
@@ -3572,10 +3607,12 @@ void CMy2015RemoteDlg::StartFrpcAuto(const FrpAutoConfig& cfg)
|
||||
|
||||
// 设置状态栏显示的 FRP 地址 (IP:Port)
|
||||
m_strFrpAddr.Format(_T("%hs:%d"), cfg.serverAddr.c_str(), cfg.remotePort);
|
||||
// 直接更新状态栏 FRP 分区(不触发 OnSize 避免循环)
|
||||
// 直接更新状态栏 FRP 分区(不触发 OnSize 避免循环)。宽度与 UpdateStatusBarStats
|
||||
// 一致,按实际地址文本实测:硬编码 250px 在高缩放比机器上会截掉端口。
|
||||
if (m_StatusBar.GetSafeHwnd()) {
|
||||
m_StatusBar.SetPaneInfo(3, m_StatusBar.GetItemID(3), SBPS_NORMAL, 250);
|
||||
m_nPaneFrpWidth = max(250, CalcStatusPaneWidth(m_StatusBar, m_strFrpAddr));
|
||||
m_StatusBar.SetPaneText(3, m_strFrpAddr);
|
||||
LayoutStatusPanes();
|
||||
}
|
||||
|
||||
Mprintf("[FRP-Auto] 启动自动代理: %s:%d -> frps:%d\n",
|
||||
@@ -3602,10 +3639,12 @@ void CMy2015RemoteDlg::StopFrpcAuto()
|
||||
|
||||
// 清除状态栏 FRP 地址
|
||||
m_strFrpAddr.Empty();
|
||||
// 直接更新状态栏 FRP 分区(不触发 OnSize 避免循环)
|
||||
// 直接更新状态栏 FRP 分区(不触发 OnSize 避免循环)。宽度归零隐藏,下一次
|
||||
// UpdateStatusBarStats 会按 FRPS/WAN/LAN 回退地址重新实测。
|
||||
if (m_StatusBar.GetSafeHwnd()) {
|
||||
m_StatusBar.SetPaneInfo(3, m_StatusBar.GetItemID(3), SBPS_NORMAL, 0);
|
||||
m_nPaneFrpWidth = 0;
|
||||
m_StatusBar.SetPaneText(3, _T(""));
|
||||
LayoutStatusPanes();
|
||||
}
|
||||
|
||||
Mprintf("[FRP-Auto] 已停止\n");
|
||||
@@ -3944,20 +3983,9 @@ void CMy2015RemoteDlg::OnSize(UINT nType, int cx, int cy)
|
||||
Rect.right=cx;
|
||||
Rect.bottom=cy;
|
||||
m_StatusBar.MoveWindow(Rect);
|
||||
// 6个分区:消息(自动拉伸)、TLS、MCP、FRP地址(250px)、运行统计(180px)、到期时间(180px)
|
||||
int paneExpire = m_strExpireDate.IsEmpty() ? 0 : 180; // 无到期信息时隐藏
|
||||
// 优先显示上级 FRPC,其次显示本机 FRPS
|
||||
int paneFrp = 250;
|
||||
int paneRuntime = 180;
|
||||
int paneTls = max(0, m_nPaneTlsWidth);
|
||||
int paneMcp = max(0, m_nPaneMcpWidth);
|
||||
int paneMsg = max(0, cx - paneTls - paneMcp - paneFrp - paneRuntime - paneExpire - 20);
|
||||
m_StatusBar.SetPaneInfo(0, m_StatusBar.GetItemID(0), SBPS_STRETCH, paneMsg);
|
||||
m_StatusBar.SetPaneInfo(1, m_StatusBar.GetItemID(1), SBPS_NORMAL, paneTls);
|
||||
m_StatusBar.SetPaneInfo(2, m_StatusBar.GetItemID(2), SBPS_NORMAL, paneMcp);
|
||||
m_StatusBar.SetPaneInfo(3, m_StatusBar.GetItemID(3), SBPS_NORMAL, paneFrp);
|
||||
m_StatusBar.SetPaneInfo(4, m_StatusBar.GetItemID(4), SBPS_NORMAL, paneRuntime);
|
||||
m_StatusBar.SetPaneInfo(5, m_StatusBar.GetItemID(5), SBPS_NORMAL, paneExpire);
|
||||
// 6个分区:消息(自动拉伸)、TLS、MCP、FRP地址、运行统计、到期时间。
|
||||
// 宽度由 UpdateStatusBarStats 按实测文本算好,这里只负责按当前窗口宽度摆位。
|
||||
LayoutStatusPanes();
|
||||
}
|
||||
|
||||
if(m_ToolBar.m_hWnd!=NULL) { //工具条
|
||||
@@ -4240,9 +4268,7 @@ void CMy2015RemoteDlg::UpdateStatusBarStats()
|
||||
m_nPaneTlsWidth = CalcStatusFlagPaneWidth(m_StatusBar, _TR("TLS: 有"), _TR("TLS: 无"),
|
||||
_TR("TLS: 内置"));
|
||||
m_nPaneMcpWidth = CalcStatusFlagPaneWidth(m_StatusBar, strMcp, strMcpWidest);
|
||||
m_StatusBar.SetPaneInfo(1, m_StatusBar.GetItemID(1), SBPS_NORMAL, m_nPaneTlsWidth);
|
||||
m_StatusBar.SetPaneText(1, strTls);
|
||||
m_StatusBar.SetPaneInfo(2, m_StatusBar.GetItemID(2), SBPS_NORMAL, m_nPaneMcpWidth);
|
||||
m_StatusBar.SetPaneText(2, strMcp);
|
||||
|
||||
// === 分区4:运行统计 ===
|
||||
@@ -4326,12 +4352,21 @@ void CMy2015RemoteDlg::UpdateStatusBarStats()
|
||||
strFrpDisplay.Format(_T("LAN %s:%d"), m_localPrivateIP.c_str(), THIS_CFG.Get1Int("settings", "ghost", ';', 6543));
|
||||
}
|
||||
}
|
||||
// 根据是否有内容设置分区宽度
|
||||
int paneFrpWidth = 250;
|
||||
m_StatusBar.SetPaneInfo(3, m_StatusBar.GetItemID(3), SBPS_NORMAL, paneFrpWidth);
|
||||
// 分区宽度按实际地址文本实测,但**不小于**改动前的固定宽度:250/180/180 在
|
||||
// 100% 缩放下是验证过的,实测值只用于在高缩放比/大字体机器上把它们撑大。
|
||||
m_nPaneFrpWidth = max(250, CalcStatusPaneWidth(m_StatusBar, strFrpDisplay));
|
||||
m_StatusBar.SetPaneText(3, strFrpDisplay);
|
||||
|
||||
// === 分区4:运行统计 ===
|
||||
// 宽度取「当前文本」与「可能出现的最长格式」的较大者:数字随时间增长时分区
|
||||
// 不会每分钟跳一次,缩放比高的机器上也不会把 MTBF/运行时长截掉。
|
||||
CString strRuntimeWidest =
|
||||
_TR("运行:") + CString(_T(" 99999d 23h ")) + _TR("(无崩溃)");
|
||||
m_nPaneRuntimeWidth = max(180,
|
||||
max(CalcStatusPaneWidth(m_StatusBar, strRuntime),
|
||||
CalcStatusPaneWidth(m_StatusBar, strRuntimeWidest)));
|
||||
m_nPaneRuntimeWidth = max(m_nPaneRuntimeWidth,
|
||||
CalcStatusPaneWidth(m_StatusBar, CString(_T("MTBF: 999d23h | 100.0%"))));
|
||||
m_StatusBar.SetPaneText(4, strRuntime);
|
||||
|
||||
// === 分区5:到期时间 ===
|
||||
@@ -4383,8 +4418,52 @@ void CMy2015RemoteDlg::UpdateStatusBarStats()
|
||||
strDays.Format(_T("(%lld"), diffDays);
|
||||
strExpire = _TR("到期:") + CString(_T(" ")) + strDate + _T(" ") + strDays + _TR("天") + _T(")");
|
||||
}
|
||||
// 宽度按文本实测,再多留一层富余:不同系统对英文字形("Expires:" 里的西文)
|
||||
// 的字体回退和 GetTextExtent 有出入,多留 20px 总比截断好。下限 220 保证
|
||||
// 「Expires: 2026-12-31 (41d)」这类比中文更长的串有足够空间。
|
||||
m_nPaneExpireWidth = max(220, CalcStatusPaneWidth(m_StatusBar, strExpire) + 20);
|
||||
m_StatusBar.SetPaneText(5, strExpire);
|
||||
} else {
|
||||
// 无授权信息:分区宽度归零(隐藏)
|
||||
m_nPaneExpireWidth = 0;
|
||||
}
|
||||
|
||||
// 各分区宽度都已按当前语言/DPI 算好,统一交给布局函数落位:窗口不够宽时
|
||||
// 它会按优先级收掉低价值分区,保证「到期时间」不会被挤出右边缘。
|
||||
LayoutStatusPanes();
|
||||
}
|
||||
|
||||
// 按状态栏实际宽度布置分区。所有分区宽度之和(加上消息区下限)超过窗口时,按
|
||||
// 运行统计 → FRP → MCP → TLS 的顺序隐藏,避免最后一个分区被窗口右边缘裁掉——
|
||||
// 英文串比中文长、缩放比更高、窗口更窄,都会把总宽度顶上去,这里统一兜底。
|
||||
void CMy2015RemoteDlg::LayoutStatusPanes()
|
||||
{
|
||||
if (!m_StatusBar.GetSafeHwnd())
|
||||
return;
|
||||
|
||||
CRect rc;
|
||||
m_StatusBar.GetClientRect(&rc);
|
||||
|
||||
int widths[6] = { 0, m_nPaneTlsWidth, m_nPaneMcpWidth, m_nPaneFrpWidth,
|
||||
m_nPaneRuntimeWidth, m_nPaneExpireWidth };
|
||||
static const int kDropOrder[] = { 4, 3, 2, 1 };
|
||||
const int kMinMessageWidth = 120;
|
||||
|
||||
int fixedWidth = 0;
|
||||
for (int i = 1; i <= 5; ++i)
|
||||
fixedWidth += max(0, widths[i]);
|
||||
|
||||
for (int i = 0; i < (int)(sizeof(kDropOrder) / sizeof(kDropOrder[0]))
|
||||
&& fixedWidth + kMinMessageWidth > rc.Width(); ++i) {
|
||||
const int idx = kDropOrder[i];
|
||||
fixedWidth -= max(0, widths[idx]);
|
||||
widths[idx] = 0;
|
||||
}
|
||||
|
||||
const int paneMsg = max(0, rc.Width() - fixedWidth - 20);
|
||||
m_StatusBar.SetPaneInfo(0, m_StatusBar.GetItemID(0), SBPS_STRETCH, paneMsg);
|
||||
for (int i = 1; i <= 5; ++i)
|
||||
m_StatusBar.SetPaneInfo(i, m_StatusBar.GetItemID(i), SBPS_NORMAL, max(0, widths[i]));
|
||||
}
|
||||
|
||||
void CMy2015RemoteDlg::DeletePopupWindow(BOOL bForce)
|
||||
|
||||
@@ -479,7 +479,11 @@ public:
|
||||
CString m_strFrpAddr; // FRP 地址 (IP:Port),由上级提供
|
||||
int m_nPaneTlsWidth = 60; // 状态栏 TLS 分区宽度(按当前语言实测文本宽度)
|
||||
int m_nPaneMcpWidth = 60; // 状态栏 MCP 分区宽度(按当前语言实测文本宽度)
|
||||
int m_nPaneFrpWidth = 250; // 状态栏 FRP 分区宽度(按当前文本实测)
|
||||
int m_nPaneRuntimeWidth = 180; // 运行统计分区宽度(按最长可能文本实测)
|
||||
int m_nPaneExpireWidth = 0; // 到期时间分区宽度(按当前文本实测;0 = 无授权/隐藏)
|
||||
void UpdateStatusBarStats(); // 更新状态栏统计信息
|
||||
void LayoutStatusPanes(); // 按窗口宽度布置状态栏分区(空间不足时按优先级隐藏)
|
||||
CTrueColorToolBar m_ToolBar;
|
||||
CGridDialog * m_gridDlg = NULL;
|
||||
std::vector<DllInfo*> m_DllList;
|
||||
|
||||
Reference in new issue
Block a user