diff --git a/server/2015Remote/PreviewTipWnd.cpp b/server/2015Remote/PreviewTipWnd.cpp index 02f9650..9f0993c 100644 --- a/server/2015Remote/PreviewTipWnd.cpp +++ b/server/2015Remote/PreviewTipWnd.cpp @@ -17,6 +17,7 @@ constexpr int MAX_TEXT_W = 720; // 文本可换行宽度上限(垂直布 constexpr int MAX_IMAGE_W = 960; // 显示上限(防止 LAN 档 1280 的 JPEG 把窗口顶到屏幕外) constexpr int LOADING_W = 480; // 占位的最小宽度(图像未到达时) constexpr int LOADING_H = 270; +constexpr int SCREEN_MARGIN = 8; // 浮窗与工作区边缘的最小留白 } // namespace BEGIN_MESSAGE_MAP(CPreviewTipWnd, CWnd) @@ -218,37 +219,65 @@ void CPreviewTipWnd::RecalcLayoutAndResize() m_imageDrawH = m_imageReserveH; } + // 按给定换行宽度估算文本尺寸(项目是 MBCS,但浮窗文本是宽字符,必须显式调用 W 版本) + auto measureText = [this](int wrapW) { + CClientDC dc(this); + CFont* old = dc.SelectObject(&m_font); + CRect rcText(0, 0, wrapW, 32767); + ::DrawTextW(dc.GetSafeHdc(), m_text, m_text.GetLength(), &rcText, + DT_CALCRECT | DT_LEFT | DT_WORDBREAK | DT_NOPREFIX); + dc.SelectObject(old); + m_textW = max((int)MIN_TEXT_W, rcText.Width()); + if (m_imageDrawW > 0) m_textW = max(m_textW, m_imageDrawW); // 至少与图像同宽 + m_textH = rcText.Height(); + }; + // 文本换行宽度:与图像同宽(让文本视觉上对齐到图像下方),但不超过 MAX_TEXT_W // 没有图像时退化到 MAX_TEXT_W int textWrapW = m_imageDrawW > 0 ? min((int)MAX_TEXT_W, m_imageDrawW) : (int)MAX_TEXT_W; if (textWrapW < MIN_TEXT_W) textWrapW = MIN_TEXT_W; + measureText(textWrapW); - // 估算文本尺寸(项目是 MBCS,但浮窗文本是宽字符,必须显式调用 W 版本) - CClientDC dc(this); - CFont* old = dc.SelectObject(&m_font); - CRect rcText(0, 0, textWrapW, 32767); - ::DrawTextW(dc.GetSafeHdc(), m_text, m_text.GetLength(), &rcText, - DT_CALCRECT | DT_LEFT | DT_WORDBREAK | DT_NOPREFIX); - m_textW = max((int)MIN_TEXT_W, rcText.Width()); - if (m_imageDrawW > 0) m_textW = max(m_textW, m_imageDrawW); // 至少与图像同宽 - m_textH = rcText.Height(); - dc.SelectObject(old); + // 当前左上角 / 所在显示器工作区 + CRect rc; + GetWindowRect(&rc); + int newX = rc.left; + int newY = rc.top; + HMONITOR hMon = MonitorFromPoint(CPoint(newX, newY), MONITOR_DEFAULTTONEAREST); + MONITORINFO mi = { sizeof(mi) }; + const bool hasWork = hMon && GetMonitorInfo(hMon, &mi); + + // 竖屏客户端(Android 手机 1080x2400 之类)按桌面经验缩到 480 宽后,等比高度能到 + // 1000+ px,浮窗直接顶出屏幕。按工作区把图像再压一次;文本换行宽度依赖图像宽度, + // 压完要重算文本,两轮足够收敛(每轮只会更小)。 + if (hasWork && m_imageDrawW > 0) { + const int workW = mi.rcWork.right - mi.rcWork.left; + const int workH = mi.rcWork.bottom - mi.rcWork.top; + const int maxImageW = workW - 2 * SCREEN_MARGIN - 2 * PADDING; + for (int pass = 0; pass < 2; ++pass) { + int gap = (m_textH > 0) ? IMAGE_TEXT_GAP : 0; + int budgetH = workH - 2 * SCREEN_MARGIN - 2 * PADDING - gap - m_textH; + bool tooTall = budgetH > 0 && m_imageDrawH > budgetH; + bool tooWide = maxImageW > 0 && m_imageDrawW > maxImageW; + if (!tooTall && !tooWide) break; + + double scale = 1.0; + if (tooTall) scale = min(scale, (double)budgetH / m_imageDrawH); + if (tooWide) scale = min(scale, (double)maxImageW / m_imageDrawW); + m_imageDrawW = max(1, (int)(m_imageDrawW * scale)); + m_imageDrawH = max(1, (int)(m_imageDrawH * scale)); + textWrapW = max((int)MIN_TEXT_W, min((int)MAX_TEXT_W, m_imageDrawW)); + measureText(textWrapW); + } + } int contentW = max(m_imageDrawW, m_textW); int gap = (m_imageDrawW > 0 && m_textH > 0) ? IMAGE_TEXT_GAP : 0; int totalW = PADDING + contentW + PADDING; int totalH = PADDING + m_imageDrawH + gap + m_textH + PADDING; - // 当前左上角 - CRect rc; - GetWindowRect(&rc); - int newX = rc.left; - int newY = rc.top; - // 防止越出屏幕:右下夹紧到工作区 - HMONITOR hMon = MonitorFromPoint(CPoint(newX, newY), MONITOR_DEFAULTTONEAREST); - MONITORINFO mi = { sizeof(mi) }; - if (hMon && GetMonitorInfo(hMon, &mi)) { + if (hasWork) { if (newX + totalW > mi.rcWork.right) newX = max((int)mi.rcWork.left, mi.rcWork.right - totalW); if (newY + totalH > mi.rcWork.bottom) newY = max((int)mi.rcWork.top, mi.rcWork.bottom - totalH); }