LAPTOP-SNT8I5JK\Boounion
2025-04-26 e1b3a1f5e35b09be95d694bc259b0ba92c4d3436
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
// CPanelMaster.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "CPanelMaster.h"
#include "afxdialogex.h"
#include "Common.h"
#include "VerticalLine.h"
 
 
// CPanelMaster 对话框
 
IMPLEMENT_DYNAMIC(CPanelMaster, CDialogEx)
 
CPanelMaster::CPanelMaster(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_PANEL_MASTER, pParent)
{
    m_crBkgnd = PANEL_MASTER_BACKGROUND_COLOR;
    m_hbrBkgnd = nullptr;
    m_nPanelWidth = 388;
}
 
CPanelMaster::~CPanelMaster()
{
}
 
void CPanelMaster::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_TREE1, m_treeCtrl);
}
 
 
BEGIN_MESSAGE_MAP(CPanelMaster, CDialogEx)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_NOTIFY(BYVERTICALLINE_MOVEX, IDC_LINE1, &CPanelMaster::OnVLineMoveX)
    ON_WM_TIMER()
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CPanelMaster::OnTvnSelchangedTree1)
END_MESSAGE_MAP()
 
 
// CPanelMaster 消息处理程序
 
 
int CPanelMaster::getPanelWidth()
{
    return m_nPanelWidth;
}
 
BOOL CPanelMaster::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
 
    CVerticalLine* pLine1 = CVerticalLine::Hook(GetDlgItem(IDC_LINE1)->GetSafeHwnd());
    pLine1->SetBkgndColor(RGB(225, 225, 225));
    pLine1->SetLineColor(RGB(198, 198, 198));
    pLine1->EnableResize();
 
 
    // 读取面板宽
    CString strIniFile;
    strIniFile.Format(_T("%s\\%s.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir, (LPTSTR)(LPCTSTR)theApp.m_strAppFile);
    m_nPanelWidth = GetPrivateProfileInt(_T("App"), _T("MasterPanelWidth"),
        int((double)GetSystemMetrics(SM_CXSCREEN) * 0.25), (LPTSTR)(LPCTSTR)strIniFile);
 
 
    // treectrl
    m_treeCtrl.SetBkColor(PANEL_MASTER_BACKGROUND_COLOR);
    m_treeCtrl.SetItemHeight(28);
    SetTimer(1, 2000, nullptr);
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}
 
HBRUSH CPanelMaster::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        pDC->SetBkColor(m_crBkgnd);
        pDC->SetTextColor(RGB(0, 0, 0));
    }
 
    if (m_hbrBkgnd == nullptr) {
        m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
    }
 
    return m_hbrBkgnd;
}
 
void CPanelMaster::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    if (m_hbrBkgnd != nullptr) {
        ::DeleteObject(m_hbrBkgnd);
    }
}
 
void CPanelMaster::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if (GetDlgItem(IDC_LINE1) == nullptr) return;
 
    CWnd* pItem;
    CRect rcClient, rcItem;
 
    GetClientRect(&rcClient);
    pItem = GetDlgItem(IDC_LINE1);
    pItem->MoveWindow(rcClient.right - 3, 0, 3, rcClient.Height());
 
    pItem = GetDlgItem(IDC_LABEL_LOADING);
    pItem->GetWindowRect(&rcItem);
    pItem->MoveWindow(12, (rcClient.Height() - rcItem.Height()) / 2, rcClient.Width() - 24, rcItem.Height());
 
    m_treeCtrl.MoveWindow(5, 5, rcClient.Width() - 8, rcClient.Height() - 10);
}
 
 
#define MASTER_PANEL_MIN_WIDTH        88
#define MASTER_PANEL_MAX_WIDTH        588
void CPanelMaster::OnVLineMoveX(NMHDR* nmhdr, LRESULT* result)
{
    BYVERTICALLINE_NMHDR* pNmhdrex = (BYVERTICALLINE_NMHDR*)nmhdr;
    int x = pNmhdrex->dwData;
    m_nPanelWidth += x;
    m_nPanelWidth = max(m_nPanelWidth, MASTER_PANEL_MIN_WIDTH);
    m_nPanelWidth = min(m_nPanelWidth, MASTER_PANEL_MAX_WIDTH);
    GetParent()->SendMessage(ID_MSG_PANEL_RESIZE, m_nPanelWidth, 0);
 
    CString strIniFile, strValue;
    strIniFile.Format(_T("%s\\%s.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir, (LPTSTR)(LPCTSTR)theApp.m_strAppFile);
    strValue.Format(_T("%d"), m_nPanelWidth);
    WritePrivateProfileString(_T("App"), _T("MasterPanelWidth"),
        (LPTSTR)(LPCTSTR)strValue, (LPTSTR)(LPCTSTR)strIniFile);
    OnSize(0, 0, 0);
    
    * result = 0;
}
 
void CPanelMaster::OnTimer(UINT_PTR nIDEvent)
{
    if (1 == nIDEvent) {
        KillTimer(1);
        GetDlgItem(IDC_LABEL_LOADING)->ShowWindow(SW_HIDE);
        m_treeCtrl.ShowWindow(SW_SHOW);
        loadEquipmentList();
    }
 
    CDialogEx::OnTimer(nIDEvent);
}
 
void CPanelMaster::loadEquipmentList()
{
    HTREEITEM hItemMaster = m_treeCtrl.InsertItem("Master");
 
    std::list<SERVO::CEquipment*>& eqs = theApp.m_model.m_master.getEquipmentList();
    for (auto item : eqs) {
        HTREEITEM hItemEq = m_treeCtrl.InsertItem(item->getName().c_str(), hItemMaster);
        m_treeCtrl.SetItemData(hItemEq, (DWORD_PTR)item);
        loadSteps(item, hItemEq);
        m_treeCtrl.Expand(hItemEq, TVE_EXPAND);
    }
 
 
    m_treeCtrl.Expand(hItemMaster, TVE_EXPAND);
}
 
void CPanelMaster::loadSteps(SERVO::CEquipment* pEquipment, HTREEITEM hItemEq)
{
    std::map<unsigned int, SERVO::CStep*>& steps = pEquipment->getSteps();
 
    for (auto item : steps) {
        HTREEITEM hStep = m_treeCtrl.InsertItem(item.second->getName().c_str(), hItemEq);
        m_treeCtrl.SetItemData(hStep, (DWORD_PTR)item.second);
    }
}
 
void CPanelMaster::OnTvnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
    HTREEITEM hItem = pNMTreeView->itemNew.hItem;
    int nLevel = GetTreeItemLevel(hItem);
    if (nLevel == 2) {
        SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)m_treeCtrl.GetItemData(hItem);
        theApp.m_model.notifyPtr(RX_CODE_SELECT_EQUIPMENT, pEquipment);
    }
    else if (nLevel == 3) {
        SERVO::CStep* pStep = (SERVO::CStep*)m_treeCtrl.GetItemData(hItem);
        theApp.m_model.notifyPtr(RX_CODE_SELECT_STEP, pStep);
    }
 
 
 
    *pResult = 0;
}
 
int CPanelMaster::GetTreeItemLevel(HTREEITEM hItem)
{
    int nLevel = 0;
    HTREEITEM hTemp = hItem;
    while (hTemp != nullptr) {
        hTemp = m_treeCtrl.GetParentItem(hTemp);
        nLevel++;
    }
 
    return nLevel;
}
 
SERVO::CEquipment* CPanelMaster::GetActiveEquipment()
{
    HTREEITEM hItem = m_treeCtrl.GetSelectedItem();
    if (hItem == nullptr) return nullptr;
 
    int nLevel = GetTreeItemLevel(hItem);
    if (nLevel == 2) {
        return (SERVO::CEquipment*)m_treeCtrl.GetItemData(hItem);
    }
    else if (nLevel == 3) {
        SERVO::CStep* pStep = (SERVO::CStep*)m_treeCtrl.GetItemData(hItem);
        if (pStep != nullptr) {
            return pStep->getEquipment();
        }
    }
 
    return nullptr;
}