SourceCode/Bond/Servo/LogEdit.cpp
@@ -21,12 +21,13 @@
BEGIN_MESSAGE_MAP(CLogEdit, CEdit)
   ON_WM_CONTEXTMENU()
   ON_WM_VSCROLL()
    ON_WM_MOUSEWHEEL()
END_MESSAGE_MAP()
void CLogEdit::SetMaxLineCount(int line)
{
   m_nMaxLines = line;
   m_nTrimLines = min(m_nMaxLines, 100);
   m_nTrimLines = min(m_nMaxLines, 4000);
}
void CLogEdit::OnContextMenu(CWnd* pWnd, CPoint point)
@@ -60,6 +61,13 @@
   CEdit::OnVScroll(nSBCode, nPos, pScrollBar);
}
BOOL CLogEdit::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    // 每次滚动时检查是否还在底部
    m_bAutoScroll = IsScrollBarAtBottom();
    return CEdit::OnMouseWheel(nFlags, zDelta, pt);
}
BOOL CLogEdit::IsScrollBarAtBottom()
{
   SCROLLINFO si = { sizeof(si), SIF_ALL };
@@ -71,29 +79,39 @@
{
   SetRedraw(FALSE);
   // 裁剪逻辑
    // 剪切过多行
   int totalLines = GetLineCount();
   if (totalLines > m_nMaxLines) {
      // 获取要删除的字符范围
      int startChar = LineIndex(0);         // 第0行首字符位置
      int endChar = LineIndex(m_nTrimLines);   // 第N行首字符位置
        int startChar = LineIndex(0);
        int endChar = LineIndex(m_nTrimLines);
      if (startChar >= 0 && endChar > startChar) {
         SetSel(startChar, endChar);
         ReplaceSel(_T("")); // 删除前面行
            ReplaceSel(_T(""));
      }
   }
    // 保存当前选择
    int start, end;
    GetSel(start, end);
    bool hasSelection = (start != end);
   int len = GetWindowTextLength();
   SetSel(len, len);
   ReplaceSel(pszText);
    int endPos = GetWindowTextLength();
    SetSel(endPos, endPos);
    ReplaceSel(lpszText);
   if (m_bAutoScroll) {
    if (m_bAutoScroll && !hasSelection) {
      LineScroll(GetLineCount());
   }
    // 恢复选择
    if (hasSelection) {
        SetSel(start, end);
    }
   SetRedraw(TRUE);
    if (m_bAutoScroll && !hasSelection) {
   Invalidate();
   UpdateWindow();
}
}