LAPTOP-SNT8I5JK\Boounion
2025-08-20 5652c0bf59490f923e87b63251ad3f88b541636f
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "stdafx.h"
#include "CExpandableListCtrl.h"
 
IMPLEMENT_DYNAMIC(CExpandableListCtrl, CListCtrl)
 
CExpandableListCtrl::CExpandableListCtrl() {}
CExpandableListCtrl::~CExpandableListCtrl() {}
 
BEGIN_MESSAGE_MAP(CExpandableListCtrl, CListCtrl)
    ON_WM_CREATE()
    ON_NOTIFY_REFLECT(NM_CLICK, &CExpandableListCtrl::OnClick)
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CExpandableListCtrl::OnCustomDraw)
END_MESSAGE_MAP()
 
int CExpandableListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CListCtrl::OnCreate(lpCreateStruct) == -1)
        return -1;
 
    // 报表风格列举例
    SetExtendedStyle(GetExtendedStyle()
        | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES);
 
    // 示例列(可在外部设置)
    if (GetHeaderCtrl() == nullptr || GetHeaderCtrl()->GetItemCount() == 0) {
        InsertColumn(0, _T("名称"), LVCFMT_LEFT, 260);
        InsertColumn(1, _T("状态"), LVCFMT_LEFT, 120);
        InsertColumn(2, _T("描述"), LVCFMT_LEFT, 260);
    }
 
    return 0;
}
 
CExpandableListCtrl::Node* CExpandableListCtrl::InsertRoot(const std::vector<CString>& cols)
{
    auto n = std::make_unique<Node>((int)max(1, (int)cols.size()));
    for (size_t i = 0; i < cols.size(); ++i) n->cols[i] = cols[i];
    n->level = 0;
    Node* raw = n.get();
    m_roots.emplace_back(std::move(n));
    return raw;
}
 
CExpandableListCtrl::Node* CExpandableListCtrl::InsertChild(Node* parent, const std::vector<CString>& cols)
{
    ASSERT(parent);
    auto n = std::make_unique<Node>((int)max(1, (int)cols.size()));
    for (size_t i = 0; i < cols.size(); ++i) n->cols[i] = cols[i];
    n->parent = parent;
    n->level = parent->level + 1;
    Node* raw = n.get();
    parent->children.emplace_back(std::move(n));
    return raw;
}
 
void CExpandableListCtrl::appendVisible(Node* n)
{
    m_visible.push_back(n);
    if (n->expanded) {
        for (auto& ch : n->children) {
            appendVisible(ch.get());
        }
    }
}
 
void CExpandableListCtrl::RebuildVisible()
{
    // 1) 重建可见序列
    m_visible.clear();
    for (auto& r : m_roots) appendVisible(r.get());
 
    // 2) 重绘/重填数据
    SetRedraw(FALSE);
    DeleteAllItems();
 
    // 插入可见行
    for (int i = 0; i < (int)m_visible.size(); ++i) {
        Node* n = m_visible[i];
        LVITEM lvi{};
        lvi.mask = LVIF_TEXT;
        lvi.iItem = i;
        lvi.iSubItem = 0;
        lvi.pszText = const_cast<LPTSTR>((LPCTSTR)(n->cols.empty() ? _T("") : n->cols[0]));
        InsertItem(&lvi);
 
        for (int col = 1; col < GetHeaderCtrl()->GetItemCount(); ++col) {
            CString txt = (col < (int)n->cols.size()) ? n->cols[col] : _T("");
            SetItemText(i, col, txt);
        }
    }
    SetRedraw(TRUE);
    Invalidate();
}
 
void CExpandableListCtrl::Expand(Node* n)
{
    if (!n || n->children.empty()) return;
    if (!n->expanded) { n->expanded = true; RebuildVisible(); }
}
 
void CExpandableListCtrl::Collapse(Node* n)
{
    if (!n || n->children.empty()) return;
    if (n->expanded) { n->expanded = false; RebuildVisible(); }
}
 
void CExpandableListCtrl::Toggle(Node* n)
{
    if (!n || n->children.empty()) return;
    n->expanded = !n->expanded;
    RebuildVisible();
}
 
CExpandableListCtrl::Node* CExpandableListCtrl::GetNodeByVisibleIndex(int i) const
{
    if (i < 0 || i >= (int)m_visible.size()) return nullptr;
    return m_visible[i];
}
 
CRect CExpandableListCtrl::expanderRectForRow(int row) const
{
    CRect rc;
    // 取首列矩形
    if (!GetSubItemRect(row, 0, LVIR_BOUNDS, rc))
        return CRect(0, 0, 0, 0);
 
    Node* n = const_cast<CExpandableListCtrl*>(this)->GetNodeByVisibleIndex(row);
    int indent = (n ? n->level : 0);
 
    // 缩进:每级给 16px
    int left = rc.left + m_expanderPadding + indent * 16;
    CRect box(left, rc.CenterPoint().y - m_expanderSize / 2,
        left + m_expanderSize, rc.CenterPoint().y + m_expanderSize / 2);
    return box;
}
 
void CExpandableListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pia = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    if (pia->iItem >= 0) {
        CPoint pt = pia->ptAction;
 
        // 命中展开按钮?
        CRect expRc = expanderRectForRow(pia->iItem);
        if (expRc.PtInRect(pt)) {
            Node* n = GetNodeByVisibleIndex(pia->iItem);
            if (n && !n->children.empty()) {
                Toggle(n);
            }
        }
    }
    *pResult = 0;
}
 
void CExpandableListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMLVCUSTOMDRAW pCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
 
    switch (pCD->nmcd.dwDrawStage)
    {
    case CDDS_PREPAINT:
        *pResult = CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYSUBITEMDRAW;
        return;
 
    case CDDS_ITEMPREPAINT:
        *pResult = CDRF_NOTIFYSUBITEMDRAW;
        return;
 
    case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
    {
        const int row = (int)pCD->nmcd.dwItemSpec;
        const int col = pCD->iSubItem;
        CDC* pDC = CDC::FromHandle(pCD->nmcd.hdc);
 
        if (col == 0)
        {
            CRect rc; GetSubItemRect(row, 0, LVIR_BOUNDS, rc);
            Node* n = GetNodeByVisibleIndex(row);
            if (!n) { *pResult = CDRF_DODEFAULT; return; }
 
            // 1) 背景/前景颜色:按是否选中
            const bool selected = (GetItemState(row, LVIS_SELECTED) & LVIS_SELECTED) != 0;
            const bool focusOnCtrl = (GetSafeHwnd() == ::GetFocus());
            COLORREF bk = selected ? GetSysColor(focusOnCtrl ? COLOR_HIGHLIGHT : COLOR_3DFACE)
                : ListView_GetBkColor(m_hWnd);
            COLORREF txt = selected ? GetSysColor(focusOnCtrl ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT)
                : ListView_GetTextColor(m_hWnd);
 
            // 仅在需要时填充背景(避免“黑一片”)
            CBrush bkBrush(bk);
            pDC->FillRect(rc, &bkBrush);
 
            // 2) 展开/折叠指示
            if (!n->children.empty())
            {
                CRect box = expanderRectForRow(row);
                // 画三角(▶/▼),并恢复画笔/画刷
                HGDIOBJ oldPen = pDC->SelectObject(GetStockObject(BLACK_PEN));
                HGDIOBJ oldBrush = pDC->SelectObject(GetStockObject(BLACK_BRUSH));
                POINT tri[3];
                if (n->expanded) { // ▼
                    tri[0] = { box.left + 2, box.top + 2 };
                    tri[1] = { box.right - 2, box.top + 2 };
                    tri[2] = { box.CenterPoint().x, box.bottom - 2 };
                }
                else {           // ▶
                    tri[0] = { box.left + 2, box.top + 2 };
                    tri[1] = { box.right - 2, box.CenterPoint().y };
                    tri[2] = { box.left + 2, box.bottom - 2 };
                }
                pDC->Polygon(tri, 3);
                pDC->SelectObject(oldPen);
                pDC->SelectObject(oldBrush);
            }
 
            // 3) 文本:右移避免遮挡
            const int indentPx = n->level * 16;
            CRect textRc = rc;
            textRc.left = rc.left + m_expanderPadding + indentPx + m_expanderSize + m_textGap;
 
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetTextColor(txt);
            CString txt0 = n->cols.empty() ? _T("") : n->cols[0];
            pDC->DrawText(txt0, textRc, DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS);
 
            // 首列自绘完毕
            *pResult = CDRF_SKIPDEFAULT;
            return;
        }
 
        // 其他列默认绘制
        *pResult = CDRF_DODEFAULT;
        return;
    }
 
    }
 
    *pResult = CDRF_DODEFAULT;
}