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
#pragma once
#include <vector>
#include <memory>
 
class CExpandableListCtrl : public CListCtrl
{
    DECLARE_DYNAMIC(CExpandableListCtrl)
 
public:
    struct Node {
        Node* parent = nullptr;
        std::vector<std::unique_ptr<Node>> children;
        std::vector<CString> cols; // ¸÷ÁÐÎı¾
        bool expanded = false;
        int level = 0; // Ëõ½ø²ã¼¶
 
        Node(int nCols = 1) : cols(nCols) {}
    };
 
    CExpandableListCtrl();
    virtual ~CExpandableListCtrl();
 
    // Êý¾Ý¹¹½¨
    Node* InsertRoot(const std::vector<CString>& cols);
    Node* InsertChild(Node* parent, const std::vector<CString>& cols);
 
    // Õ¹¿ª/ÕÛµþ
    void Expand(Node* n);
    void Collapse(Node* n);
    void Toggle(Node* n);
 
    // Ë¢Ð¿ɼûÁбí
    void RebuildVisible();
 
    // ±ã½Ý£ºÍ¨¹ý¿É¼ûÐкÅÈ¡ Node*
    Node* GetNodeByVisibleIndex(int i) const;
 
protected:
    // ÏûÏ¢
    afx_msg int  OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnClick(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
 
    DECLARE_MESSAGE_MAP()
 
private:
    std::vector<std::unique_ptr<Node>> m_roots;   // ¶¥²ã½Úµã
    std::vector<Node*>                 m_visible; // Õ¹¿ªºóµÄ¿É¼û½Úµã˳Ðò
    int  m_expanderPadding = 6;   // Ê×ÁÐÄÚ²à±ß¾à
    int  m_expanderSize = 10;  // Ð¡Èý½Ç/·½¿é´óС
    int  m_textGap = 6;
 
    void appendVisible(Node* n);
    CRect expanderRectForRow(int row) const; // Ê×ÁÐÕ¹¿ª°´Å¥ÇøÓò
};