From 72f3802bd7ab24b672c951a287787b5dea253f3b Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期六, 02 八月 2025 10:48:36 +0800
Subject: [PATCH] Merge branch 'clh' into liuyang
---
SourceCode/Bond/EAPSimulator/LogEdit.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 117 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/EAPSimulator/LogEdit.cpp b/SourceCode/Bond/EAPSimulator/LogEdit.cpp
new file mode 100644
index 0000000..d6e75ad
--- /dev/null
+++ b/SourceCode/Bond/EAPSimulator/LogEdit.cpp
@@ -0,0 +1,117 @@
+#include "pch.h"
+#include "LogEdit.h"
+
+
+#define MENU_ITEM_SEL_ALL 0x666
+#define MENU_ITEM_COPY 0x667
+#define MENU_ITEM_CLEAR 0x668
+
+CLogEdit::CLogEdit()
+{
+ m_nMaxLines = 0xffff;
+ m_nTrimLines = 100;
+ m_bAutoScroll = TRUE;
+}
+
+
+CLogEdit::~CLogEdit()
+{
+}
+
+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, 4000);
+}
+
+void CLogEdit::OnContextMenu(CWnd* pWnd, CPoint point)
+{
+ HMENU hMenu = CreatePopupMenu();
+ InsertMenu(hMenu, 0, MF_BYPOSITION, MENU_ITEM_SEL_ALL, "全选");
+ InsertMenu(hMenu, 1, MF_BYPOSITION, MENU_ITEM_COPY, "复制");
+ InsertMenu(hMenu, 2, MF_BYPOSITION | MF_SEPARATOR, NULL, NULL);
+ InsertMenu(hMenu, 3, MF_BYPOSITION, MENU_ITEM_CLEAR, "全部清除");
+ int cmd = ::TrackPopupMenu(hMenu,
+ TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
+ point.x, point.y + 2, 0, m_hWnd, NULL);
+ DestroyMenu(hMenu);
+
+ if (cmd == MENU_ITEM_SEL_ALL) {
+ SetFocus();
+ this->SetSel(0, -1);
+ }
+ else if (cmd == MENU_ITEM_COPY) {
+ this->Copy();
+ }
+ else if (cmd == MENU_ITEM_CLEAR) {
+ SetWindowText(_T(""));
+ }
+}
+
+void CLogEdit::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
+{
+ // 每次滚动时检查是否还在底部
+ m_bAutoScroll = IsScrollBarAtBottom();
+ 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 };
+ GetScrollInfo(SB_VERT, &si);
+ return (si.nPos + (int)si.nPage >= si.nMax);
+}
+
+void CLogEdit::AppendText(const char* pszText)
+{
+ SetRedraw(FALSE);
+
+ // 剪切过多行
+ 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(""));
+ }
+ }
+
+ // 保存当前选择
+ int start, end;
+ GetSel(start, end);
+ bool hasSelection = (start != end);
+
+ int endPos = GetWindowTextLength();
+ SetSel(endPos, endPos);
+ ReplaceSel(pszText);
+
+ if (m_bAutoScroll && !hasSelection) {
+ LineScroll(GetLineCount());
+ }
+
+ // 恢复选择
+ if (hasSelection) {
+ SetSel(start, end);
+ }
+
+ SetRedraw(TRUE);
+
+ if (m_bAutoScroll && !hasSelection) {
+ Invalidate();
+ UpdateWindow();
+ }
+}
--
Gitblit v1.9.3