| | |
| | | #include "afxdialogex.h" |
| | | #include "IOMonitoringDlg.h" |
| | | |
| | | #define TIMER_INIT 1 |
| | | #define TIMER_READ_PLC_DATA 2 |
| | | |
| | | // CIOMonitoringDlg 对话框 |
| | | |
| | |
| | | x += colWidthLarge + groupSpacing; |
| | | |
| | | // 创建第 2 组 (3, 4, 5) |
| | | CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("OFF"), true, AlignCenter); |
| | | CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("OFF"), true, AlignCenter, [this, i]() { |
| | | // 自定义点击事件的逻辑 |
| | | auto* pControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 3]); |
| | | CString currentText; |
| | | pControl->GetWindowText(currentText); |
| | | |
| | | if (currentText == _T("OFF")) { |
| | | pControl->SetBkColor(RGB(0, 255, 0)); // 绿色背景 |
| | | pControl->SetText(_T("ON")); // 更新文本为 ON |
| | | } |
| | | else { |
| | | pControl->SetBkColor(RGB(255, 0, 0)); // 红色背景 |
| | | pControl->SetText(_T("OFF")); // 更新文本为 OFF |
| | | } |
| | | }); |
| | | x += colWidthSmall; |
| | | CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("Y1010"), false, AlignCenter); |
| | | x += colWidthSmall; |
| | |
| | | } |
| | | } |
| | | |
| | | void CIOMonitoringDlg::CreateStaticControl(int x, int y, int width, int height, const CString& text, bool hasBorder, TextAlign alignment) |
| | | void CIOMonitoringDlg::CreateStaticControl(int x, int y, int width, int height, const CString& text, bool hasBorder, TextAlign alignment, std::function<void()> clickCallback) |
| | | { |
| | | // 创建动态控件 |
| | | CBLLabel* pStatic = new CBLLabel(); |
| | |
| | | pStatic->SetFontSize(nSize); |
| | | pStatic->SetDynamicFont(TRUE); |
| | | |
| | | // 设置回调 |
| | | if (clickCallback) { |
| | | pStatic->SetClickCallback(clickCallback); |
| | | } |
| | | |
| | | // 存储控件指针 |
| | | m_staticControls.push_back(pStatic); |
| | | } |
| | |
| | | int startIndex = (m_nCurrentPage - 1) * m_nRowsPerPage; |
| | | int endIndex = min(startIndex + m_nRowsPerPage, static_cast<int>(m_displayData.size())); |
| | | |
| | | m_inputPLCAddresses.clear(); |
| | | m_outputPLCAddresses.clear(); |
| | | |
| | | for (int i = 0; i < m_nRowsPerPage; ++i) { |
| | | int row = i; |
| | | |
| | | if (startIndex + i < endIndex) { |
| | | const auto& data = m_displayData[startIndex + i]; |
| | | |
| | | // 添加 PLC 地址到容器中 |
| | | m_inputPLCAddresses.push_back(CString(data.inputAddress.c_str())); // 1 列 |
| | | m_outputPLCAddresses.push_back(CString(data.outputAddress.c_str())); // 4 列 |
| | | |
| | | // 显示控件并设置内容 |
| | | m_staticControls[row * m_nCols + 0]->SetWindowText(_T("OFF")); |
| | |
| | | m_staticControls.clear(); |
| | | } |
| | | |
| | | void CIOMonitoringDlg::UpdatePLCStates() |
| | | { |
| | | // 随机值模拟 |
| | | for (size_t i = 0; i < m_inputPLCAddresses.size(); ++i) { |
| | | // 模拟获取输入状态 |
| | | bool inputState = (rand() % 2 == 0); // 偶尔为 true/false |
| | | auto* inputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 0]); |
| | | inputControl->SetBkColor(inputState ? RGB(0, 255, 0) : RGB(255, 0, 0)); |
| | | inputControl->SetText(inputState ? _T("ON") : _T("OFF")); |
| | | } |
| | | |
| | | for (size_t i = 0; i < m_outputPLCAddresses.size(); ++i) { |
| | | // 模拟获取输出状态 |
| | | bool outputState = (rand() % 2 == 0); // 偶尔为 true/false |
| | | auto* outputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 3]); |
| | | outputControl->SetBkColor(outputState ? RGB(0, 255, 0) : RGB(255, 0, 0)); |
| | | outputControl->SetText(outputState ? _T("ON") : _T("OFF")); |
| | | } |
| | | } |
| | | |
| | | BEGIN_MESSAGE_MAP(CIOMonitoringDlg, CDialogEx) |
| | | ON_BN_CLICKED(IDC_BUTTON_PREV_PAGE, &CIOMonitoringDlg::OnBnClickedButtonPrevPage) |
| | | ON_BN_CLICKED(IDC_BUTTON_NEXT_PAGE, &CIOMonitoringDlg::OnBnClickedButtonNextPage) |
| | | ON_WM_SIZE() |
| | | ON_WM_TIMER() |
| | | ON_WM_CLOSE() |
| | | END_MESSAGE_MAP() |
| | | |
| | | |
| | |
| | | CreateDynamicControls(); |
| | | DisplayCurrentPage(); |
| | | |
| | | SetTimer(TIMER_READ_PLC_DATA, 500, nullptr); |
| | | |
| | | return TRUE; // return TRUE unless you set the focus to a control |
| | | // 异常: OCX 属性页应返回 FALSE |
| | | } |
| | |
| | | AfxMessageBox(_T("已经是最后一页!")); |
| | | } |
| | | } |
| | | |
| | | void CIOMonitoringDlg::OnTimer(UINT_PTR nIDEvent) |
| | | { |
| | | // TODO: 在此添加消息处理程序代码和/或调用默认值 |
| | | if (TIMER_READ_PLC_DATA == nIDEvent) { |
| | | //ASSERT(m_pPLC); |
| | | UpdatePLCStates(); |
| | | Sleep(100); |
| | | } |
| | | CDialogEx::OnTimer(nIDEvent); |
| | | } |
| | | |
| | | void CIOMonitoringDlg::OnClose() |
| | | { |
| | | // TODO: 在此添加消息处理程序代码和/或调用默认值 |
| | | KillTimer(TIMER_READ_PLC_DATA); |
| | | CDialogEx::OnClose(); |
| | | } |