| | |
| | | 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) |
| | |
| | | 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 }; |
| | |
| | | |
| | | 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(); |
| | | } |
| | | SetRedraw(TRUE); |
| | | |
| | | if (m_bAutoScroll && !hasSelection) { |
| | | Invalidate(); |
| | | UpdateWindow(); |
| | | } |
| | | } |