LAPTOP-SNT8I5JK\Boounion
2025-02-20 4cc9fd5bb0c0224e88ca702e6e736e1bc138dce6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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);
}