From e8a27bb203fe2aff70390a5eca002d7438da9b0f Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期三, 22 十月 2025 14:24:34 +0800
Subject: [PATCH] Merge branch 'clh' into liuyang

---
 SourceCode/Bond/Servo/LogEdit.cpp |   64 ++++++++++++++++++++-----------
 1 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/SourceCode/Bond/Servo/LogEdit.cpp b/SourceCode/Bond/Servo/LogEdit.cpp
index 3b4742f..6330dc4 100644
--- a/SourceCode/Bond/Servo/LogEdit.cpp
+++ b/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 };
@@ -69,31 +77,41 @@
 
 void CLogEdit::AppendText(const char* pszText)
 {
-	SetRedraw(FALSE);
+    SetRedraw(FALSE);
 
-	// 裁剪逻辑
-	int totalLines = GetLineCount();
-	if (totalLines > m_nMaxLines) {
-		// 获取要删除的字符范围
-		int startChar = LineIndex(0);			// 第0行首字符位置
-		int endChar = LineIndex(m_nTrimLines);	// 第N行首字符位置
+    // 剪切过多行
+    int totalLines = GetLineCount();
+    if (totalLines > m_nMaxLines) {
+        int startChar = LineIndex(0);
+        int endChar = LineIndex(m_nTrimLines);
+        if (startChar >= 0 && endChar > startChar) {
+            SetSel(startChar, endChar);
+            ReplaceSel(_T(""));
+        }
+    }
 
-		if (startChar >= 0 && endChar > startChar) {
-			SetSel(startChar, endChar);
-			ReplaceSel(_T("")); // 删除前面行
-		}
-	}
+    // 保存当前选择
+    int start, end;
+    GetSel(start, end);
+    bool hasSelection = (start != end);
 
+    int endPos = GetWindowTextLength();
+    SetSel(endPos, endPos);
+    ReplaceSel(pszText);
 
-	int len = GetWindowTextLength();
-	SetSel(len, len);
-	ReplaceSel(pszText);
+    if (m_bAutoScroll && !hasSelection) {
+        LineScroll(GetLineCount());
+    }
 
-	if (m_bAutoScroll) {
-		LineScroll(GetLineCount());
-	}
+    // 恢复选择
+    if (hasSelection) {
+        SetSel(start, end);
+    }
 
-	SetRedraw(TRUE);
-	Invalidate();
-	UpdateWindow();
-}
\ No newline at end of file
+    SetRedraw(TRUE);
+
+    if (m_bAutoScroll && !hasSelection) {
+        Invalidate();
+        UpdateWindow();
+    }
+}

--
Gitblit v1.9.3