#include "stdafx.h" #include "LogEdit.h" #define MENU_ITEM_SEL_ALL 0x666 #define MENU_ITEM_COPY 0x667 #define MENU_ITEM_CLEAR 0x668 CLogEdit::CLogEdit() { m_nMaxLineCount = 0xffff; } CLogEdit::~CLogEdit() { } BEGIN_MESSAGE_MAP(CLogEdit, CEdit) ON_WM_CONTEXTMENU() END_MESSAGE_MAP() void CLogEdit::SetMaxLineCount(int line) { m_nMaxLineCount = line; } 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::AppendText(const char* pszText) { // »ñȡѡÔñ·¶Î§ÒÔ±ã»Ö¸´ int nStart, nEnd; GetSel(nStart, nEnd); // ³¬¹ýÖ¸¶¨ÐÐÊýÔòɾ³ý×îÇ°ÃæµÄÐÐ 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(); } // ×·¼Óµ½×îºó int length = GetWindowTextLength(); SetSel(length, length); ReplaceSel(pszText); PostMessage(WM_VSCROLL, SB_BOTTOM, 0); // »Ö¸´ if (nStart == 0 && nEnd == 0) { nStart = GetWindowTextLength(); nEnd = nStart; } SetSel(nStart, nEnd); }