chenluhua1980
2026-01-06 18f05a37d19e0e20db266a4e32e8263847e94a76
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// CPageLinkSignal.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "CPageLinkSignal.h"
#include "afxdialogex.h"
 
 
#define FACECOLOR_ON        RGB(34, 177, 76)
#define FACECOLOR_OFF        RGB(195, 195, 195)
 
#define TIMER_ID_SIGNAL_UPDATE  1001
#define TIMER_INTERVAL_MS       1000  // 每 1 秒更新一次
 
// CPageLinkSignal 对话框
 
IMPLEMENT_DYNAMIC(CPageLinkSignal, CHMPropertyPage)
 
CPageLinkSignal::CPageLinkSignal(CWnd* pParent /*=nullptr*/)
    : CHMPropertyPage(IDD_PAGE_LINK_SIGNAL, pParent)
    , m_bEnable(TRUE)
{
    m_pEquipment = nullptr;
}
 
CPageLinkSignal::~CPageLinkSignal()
{
}
 
void CPageLinkSignal::DoDataExchange(CDataExchange* pDX)
{
    CHMPropertyPage::DoDataExchange(pDX);
    DDX_Check(pDX, IDC_CHECK_ENABLE, m_bEnable);
}
 
 
BEGIN_MESSAGE_MAP(CPageLinkSignal, CHMPropertyPage)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_WM_TIMER()
    ON_BN_CLICKED(IDC_CHECK_ENABLE, &CPageLinkSignal::OnClickedCheckEnable)
END_MESSAGE_MAP()
 
 
// CPageLinkSignal 消息处理程序
 
void CPageLinkSignal::setEquipment(SERVO::CEquipment* pEquipment)
{
    m_pEquipment = pEquipment;
}
 
BOOL CPageLinkSignal::OnInitDialog()
{
    CHMPropertyPage::OnInitDialog();
    CreateSignalButtons();
 
 
    KillTimer(TIMER_ID_SIGNAL_UPDATE);
    SetTimer(TIMER_ID_SIGNAL_UPDATE, TIMER_INTERVAL_MS, nullptr);
 
 
    if (m_pEquipment) {
        m_bEnable = m_pEquipment->IsEnabled();
        UpdateData(FALSE);
    }
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}
 
 
HBRUSH CPageLinkSignal::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CHMPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
 
    // TODO:  在此更改 DC 的任何特性
 
    // TODO:  如果默认的不是所需画笔,则返回另一个画笔
    return hbr;
}
 
 
void CPageLinkSignal::OnDestroy()
{
    CHMPropertyPage::OnDestroy();
 
    // TODO: 在此处添加消息处理程序代码
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (m_pBtnUpstream[i][j] != nullptr) {
                m_pBtnUpstream[i][j]->DestroyWindow();
                delete m_pBtnUpstream[i][j];
                m_pBtnUpstream[i][j] = nullptr;
            }
        }
    }
 
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 4; j++) {
            if (m_pBtnDwonstream[i][j] != nullptr) {
                m_pBtnDwonstream[i][j]->DestroyWindow();
                delete m_pBtnDwonstream[i][j];
                m_pBtnDwonstream[i][j] = nullptr;
            }
        }
    }
}
 
 
void CPageLinkSignal::OnSize(UINT nType, int cx, int cy)
{
    CHMPropertyPage::OnSize(nType, cx, cy);
 
    // TODO: 在此处添加消息处理程序代码
}
 
#define BTN_X        98
#define BTN_Y        80
#define BTN_WIDTH    36
void CPageLinkSignal::CreateSignalButtons()
{
    int x = 100;
    int y = 100;
    int marginx = 32;
    int marginy = 12;
    for (int row = 0; row < 8; row++) {
        y = BTN_Y + (BTN_WIDTH + marginy) * row;
        for (int col = 0; col < 8; col++) {
            x = BTN_X + (BTN_WIDTH + marginx) * col;
            m_pBtnUpstream[row][col] = new CBlButton();
            m_pBtnUpstream[row][col]->Create("", WS_VISIBLE, { x, y, x + 36, y + 36 }, this, 1000);
            m_pBtnUpstream[row][col]->SetFaceColor(FACECOLOR_OFF);
            m_pBtnUpstream[row][col]->SetFrameColor(RGB(28, 28, 28));
            m_pBtnUpstream[row][col]->SetTextColor(RGB(238, 238, 238));
        }
    }
 
    CRect rcLine;
    GetDlgItem(IDC_LINE1)->GetWindowRect(&rcLine);
    GetDlgItem(IDC_LINE1)->MoveWindow(BTN_X + (BTN_WIDTH + marginx) * 8 - marginx / 2, BTN_Y,
        rcLine.Width(), (BTN_WIDTH + marginy) * 7 + BTN_WIDTH);
 
    for (int row = 0; row < 8; row++) {
        y = BTN_Y + (BTN_WIDTH + marginy) * row;
        for (int col = 0; col < 4; col++) {
            x = BTN_X + (BTN_WIDTH + marginx) * (col + 8);
            m_pBtnDwonstream[row][col] = new CBlButton();
            m_pBtnDwonstream[row][col]->Create("", WS_VISIBLE, { x, y, x + 36, y + 36 }, this, 1000);
            m_pBtnDwonstream[row][col]->SetFaceColor(FACECOLOR_OFF);
            m_pBtnDwonstream[row][col]->SetFrameColor(RGB(28, 28, 28));
            m_pBtnDwonstream[row][col]->SetTextColor(RGB(238, 238, 238));
        }
    }
 
    CWnd* pItem;
    CRect rcItem;
    int idx[] = { IDC_LABEL_PATH1, IDC_LABEL_PATH2, IDC_LABEL_PATH3, IDC_LABEL_PATH4, IDC_LABEL_PATH5,
        IDC_LABEL_PATH6, IDC_LABEL_PATH7, IDC_LABEL_PATH8};
    for (int i = 0; i < 8; i++) {
        pItem = GetDlgItem(idx[i]);
        pItem->GetWindowRect(&rcItem);
        pItem->MoveWindow(BTN_X - rcItem.Width() - 18,
            BTN_Y + (36 - rcItem.Height()) / 2 + (BTN_WIDTH + marginy) * i,
            rcItem.Width(), rcItem.Height());
    }
 
    int idy[] = { IDC_LABEL_NAME1, IDC_LABEL_NAME2, IDC_LABEL_NAME3, IDC_LABEL_NAME4, IDC_LABEL_NAME5,
        IDC_LABEL_NAME6, IDC_LABEL_NAME7, IDC_LABEL_NAME8, IDC_LABEL_NAME9, IDC_LABEL_NAME10, 
        IDC_LABEL_NAME11, IDC_LABEL_NAME12};
    for (int i = 0; i < sizeof(idy) / sizeof(int); i++) {
        pItem = GetDlgItem(idy[i]);
        pItem->GetWindowRect(&rcItem);
        pItem->MoveWindow(BTN_X + (BTN_WIDTH + marginx) * i,
            BTN_Y - 36,
            rcItem.Width(), rcItem.Height());
    }
 
    pItem = GetDlgItem(IDC_CHECK_ENABLE);
    if (pItem) {
        int nCheckboxX = max(0, BTN_X - 50);
        int nCheckboxY = max(0, BTN_Y - rcItem.Height() - 30);
        pItem->GetWindowRect(&rcItem);
        pItem->MoveWindow(nCheckboxX, nCheckboxY, rcItem.Width(), rcItem.Height());
    }
}
 
void CPageLinkSignal::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == TIMER_ID_SIGNAL_UPDATE) {
        if (m_pEquipment) {
            UpdateAllUpstreamSignalStatesFromDevice();
            UpdateAllDownstreamSignalStatesFromDevice();
        }
    }
 
    CHMPropertyPage::OnTimer(nIDEvent);
}
 
void CPageLinkSignal::UpdateAllUpstreamSignalStatesFromDevice()
{
    ASSERT(m_pEquipment);
 
    for (int nRow = 0; nRow < 8; ++nRow) {
        for (int nCol = 0; nCol < 8; ++nCol) {
            BOOL bCurrentState = m_pEquipment->isLinkSignalUpstreamOn(nRow, nCol) && m_pEquipment->IsEnabled();
            UpdateUpstreamSignalState(nRow, nCol, bCurrentState);
        }
    }
}
 
void CPageLinkSignal::UpdateAllDownstreamSignalStatesFromDevice()
{
    ASSERT(m_pEquipment);
 
    for (int nRow = 0; nRow < 8; ++nRow) {
        for (int nCol = 0; nCol < 4; ++nCol) {
            BOOL bCurrentState = m_pEquipment->isLinkSignalDownstreamOn(nRow, nCol) && m_pEquipment->IsEnabled();
            UpdateDownstreamSignalState(nRow, nCol, bCurrentState);
        }
    }
}
 
void CPageLinkSignal::UpdateUpstreamSignalState(int nRow, int nCol, bool bNewState)
{
    if (!::IsWindow(m_pBtnUpstream[nRow][nCol]->GetSafeHwnd())) {
        return;
    }
 
    bool bState = ::GetProp(m_pBtnUpstream[nRow][nCol]->GetSafeHwnd(), _T("State")) == (void*)1;
    if (bState != bNewState) {
        m_pBtnUpstream[nRow][nCol]->SetFaceColor(bNewState ? FACECOLOR_ON : FACECOLOR_OFF);
        ::SetProp(m_pBtnUpstream[nRow][nCol]->GetSafeHwnd(), _T("State"), bNewState ? (void*)1 : (void*)0);
    }
}
 
void CPageLinkSignal::UpdateDownstreamSignalState(int nRow, int nCol, bool bNewState)
{
    if (!::IsWindow(m_pBtnDwonstream[nRow][nCol]->GetSafeHwnd())) {
        return;
    }
 
    bool bState = ::GetProp(m_pBtnDwonstream[nRow][nCol]->GetSafeHwnd(), _T("State")) == (void*)1;
    if (bState != bNewState) {
        m_pBtnDwonstream[nRow][nCol]->SetFaceColor(bNewState ? FACECOLOR_ON : FACECOLOR_OFF);
        ::SetProp(m_pBtnDwonstream[nRow][nCol]->GetSafeHwnd(), _T("State"), bNewState ? (void*)1 : (void*)0);
    }
}
 
void CPageLinkSignal::OnClickedCheckEnable()
{
    // TODO: 在此添加控件通知处理程序代码
    UpdateData(TRUE);
    m_pEquipment->SetEnable(m_bEnable);
}