mrDarker
2025-06-26 da96e6da0b677c6a4e96308aaecd3d619a8e4db2
SourceCode/Bond/Servo/LogEdit.cpp
@@ -8,7 +8,9 @@
CLogEdit::CLogEdit()
{
   m_nMaxLineCount = 0xffff;
   m_nMaxLines = 0xffff;
   m_nTrimLines = 100;
   m_bAutoScroll = TRUE;
}
@@ -18,11 +20,13 @@
BEGIN_MESSAGE_MAP(CLogEdit, CEdit)
   ON_WM_CONTEXTMENU()
   ON_WM_VSCROLL()
END_MESSAGE_MAP()
void CLogEdit::SetMaxLineCount(int line)
{
   m_nMaxLineCount = line;
   m_nMaxLines = line;
   m_nTrimLines = min(m_nMaxLines, 100);
}
void CLogEdit::OnContextMenu(CWnd* pWnd, CPoint point)
@@ -49,39 +53,47 @@
   }
}
void CLogEdit::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
   // 每次滚动时检查是否还在底部
   m_bAutoScroll = IsScrollBarAtBottom();
   CEdit::OnVScroll(nSBCode, nPos, pScrollBar);
}
BOOL CLogEdit::IsScrollBarAtBottom()
{
   SCROLLINFO si = { sizeof(si), SIF_ALL };
   GetScrollInfo(SB_VERT, &si);
   return (si.nPos + (int)si.nPage >= si.nMax);
}
void CLogEdit::AppendText(const char* pszText)
{
   // 获取选择范围以便恢复
   int nStart, nEnd;
   GetSel(nStart, nEnd);
   SetRedraw(FALSE);
   // 裁剪逻辑
   int totalLines = GetLineCount();
   if (totalLines > m_nMaxLines) {
      // 获取要删除的字符范围
      int startChar = LineIndex(0);         // 第0行首字符位置
      int endChar = LineIndex(m_nTrimLines);   // 第N行首字符位置
   // 超过指定行数则删除最前面的行
   int nLineCount = GetLineCount();
   while (nLineCount > m_nMaxLineCount) {
      int nLin1End = LineIndex(1);
      nStart -= nLin1End;
      if (nStart < 0) nStart = 0;
      nEnd -= nLin1End;
      if (nEnd < 0) nEnd = 0;
      SetSel(0, nLin1End);
      ReplaceSel(_T(""));
      nLineCount = GetLineCount();
      if (startChar >= 0 && endChar > startChar) {
         SetSel(startChar, endChar);
         ReplaceSel(_T("")); // 删除前面行
      }
   }
   // 追加到最后
   int length = GetWindowTextLength();
   SetSel(length, length);
   int len = GetWindowTextLength();
   SetSel(len, len);
   ReplaceSel(pszText);
   PostMessage(WM_VSCROLL, SB_BOTTOM, 0);
   // 恢复
   if (nStart == 0 && nEnd == 0) {
      nStart = GetWindowTextLength();
      nEnd = nStart;
   if (m_bAutoScroll) {
      LineScroll(GetLineCount());
   }
   SetSel(nStart, nEnd);
   SetRedraw(TRUE);
   Invalidate();
   UpdateWindow();
}