From fc8a07f476648f82abf9934483b04cfee0161e4e Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期四, 28 十一月 2024 10:44:27 +0800
Subject: [PATCH] 1.扩展静态文本实现回调函数点击事件 2.模拟监听PLC

---
 SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp |  162 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 126 insertions(+), 36 deletions(-)

diff --git a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
index 1185f82..39e90a4 100644
--- a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
+++ b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
@@ -6,6 +6,8 @@
 #include "afxdialogex.h"
 #include "IOMonitoringDlg.h"
 
+#define TIMER_INIT				1
+#define TIMER_READ_PLC_DATA		2
 
 // CIOMonitoringDlg 瀵硅瘽妗�
 
@@ -88,8 +90,7 @@
 	CWnd* pWnd = GetWindow(GW_CHILD);
 	while (pWnd) {
 		int nCtrlID = pWnd->GetDlgCtrlID();
-		if (nCtrlID != -1 && m_mapCtrlLayouts.find(nCtrlID) != m_mapCtrlLayouts.end())
-		{
+		if (nCtrlID != -1 && m_mapCtrlLayouts.find(nCtrlID) != m_mapCtrlLayouts.end()) {
 			CRect originalRect = m_mapCtrlLayouts[nCtrlID];
 			CRect newRect(
 				static_cast<int>(originalRect.left * dScaleX),
@@ -112,6 +113,7 @@
 	// 鏍规嵁鎺т欢楂樺害鍔ㄦ�佽皟鏁村瓧浣撳ぇ灏�
 	int fontSize = nHeight / 2;
 	if (fontSize < 8) fontSize = 8;
+	if (fontSize < 24) fontSize = 24;
 
 	// 鑾峰彇鎴栧垱寤哄瓧浣�
 	CFont* pFont = GetOrCreateFont(fontSize);
@@ -161,50 +163,68 @@
 	int colWidthLarge = availableWidth * 4 / 14;		// 澶у搴﹀垪姣斾緥
 	int groupWidth = colWidthSmall * 2 + colWidthLarge; // 姣忕粍鎬诲搴�
 
-	for (int i = 0; i < m_nRowsPerPage; ++i)
-	{
+	for (int i = 0; i < m_nRowsPerPage; ++i) {
 		// 姣忎竴琛岀殑璧峰 Y 鍧愭爣
 		int y = topMargin + i * (rowHeight + verticalSpacing);
 
 		// 鍒涘缓绗� 1 缁� (0, 1, 2)
 		int x = sideMargin; // 浠庡乏杈硅窛寮�濮�
-		CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("OFF"), true);
+		CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("OFF"), true, AlignCenter);
 		x += colWidthSmall;
-		CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("X1000"));
+		CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("X1000"), false, AlignCenter);
 		x += colWidthSmall;
-		CreateStaticControl(x, y, colWidthLarge, rowHeight, _T("鎻忚堪鏂囨湰"), false, true);
+		CreateStaticControl(x, y, colWidthLarge, rowHeight, _T("鎻忚堪鏂囨湰"), false);
 
 		// 绗� 2 缁勮捣濮嬩綅缃紝鍔犱笂缁勯棿璺�
 		x += colWidthLarge + groupSpacing;
 
 		// 鍒涘缓绗� 2 缁� (3, 4, 5)
-		CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("OFF"), true);
+		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"));
+		CreateStaticControl(x, y, colWidthSmall, rowHeight, _T("Y1010"), false, AlignCenter);
 		x += colWidthSmall;
-		CreateStaticControl(x, y, colWidthLarge, rowHeight, _T("鎻忚堪鏂囨湰"), false, true);
+		CreateStaticControl(x, y, colWidthLarge, rowHeight, _T("鎻忚堪鏂囨湰"), false);
 	}
 }
 
-void CIOMonitoringDlg::CreateStaticControl(int x, int y, int width, int height, const CString& text, bool hasBorder, bool alignLeft)
+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();
 	DWORD style = WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE; // 纭繚鍨傜洿灞呬腑
 	if (hasBorder) {
-		style |= WS_BORDER;		// 娣诲姞杈规
+		style |= WS_BORDER; // 娣诲姞杈规
 	}
-	if (alignLeft) {
-		style |= SS_LEFT;		// 宸﹀榻愭枃鏈�
-	}
-	else {
-		style |= SS_CENTER;		// 灞呬腑鏂囨湰
-	}
-
-	CStatic* pStatic = new CStatic();
 	pStatic->Create(text, style, CRect(x, y, x + width, y + height), this);
 
-	// 鍔ㄦ�佽缃瓧浣撳ぇ灏�
-	CFont* pFont = GetOrCreateFont(height / 3);
+	// 璁剧疆鏂囨湰瀵归綈鏂瑰紡
+	pStatic->SetAlignment(alignment);
+
+	// 璁剧疆鍔ㄦ�佸瓧浣撹皟鏁达紝骞惰缃瓧浣撳ぇ灏忥紙鍔ㄦ�佸瓧浣撲細鏍规嵁鎺т欢澶у皬璋冩暣锛�
+	int nSize = height / 3;
+	CFont* pFont = GetOrCreateFont(nSize);
 	pStatic->SetFont(pFont);
+	pStatic->SetFontSize(nSize);
+	pStatic->SetDynamicFont(TRUE);
+
+	// 璁剧疆鍥炶皟
+	if (clickCallback) {
+		pStatic->SetClickCallback(clickCallback);
+	}
 
 	// 瀛樺偍鎺т欢鎸囬拡
 	m_staticControls.push_back(pStatic);
@@ -215,27 +235,55 @@
 	int startIndex = (m_nCurrentPage - 1) * m_nRowsPerPage;
 	int endIndex = min(startIndex + m_nRowsPerPage, static_cast<int>(m_displayData.size()));
 
-	for (int i = startIndex; i < endIndex; ++i)
-	{
-		const auto& data = m_displayData[i];
-		int row = i - startIndex;
+	m_inputPLCAddresses.clear();
+	m_outputPLCAddresses.clear();
 
-		m_staticControls[row * m_nCols + 0]->SetWindowText(_T("OFF"));
-		m_staticControls[row * m_nCols + 1]->SetWindowText(CString(data.inputAddress.c_str()));
-		m_staticControls[row * m_nCols + 2]->SetWindowText(CString(data.inputDescription.c_str()));
-		m_staticControls[row * m_nCols + 3]->SetWindowText(_T("OFF"));
-		m_staticControls[row * m_nCols + 4]->SetWindowText(CString(data.outputAddress.c_str()));
-		m_staticControls[row * m_nCols + 5]->SetWindowText(CString(data.outputDescription.c_str()));
+	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[row * m_nCols + 0]->ShowWindow(SW_SHOW);
+			m_staticControls[row * m_nCols + 0]->SetBkColor(RGB(255, 0, 0));
+
+			m_staticControls[row * m_nCols + 1]->SetWindowText(CString(data.inputAddress.c_str()));
+			m_staticControls[row * m_nCols + 1]->ShowWindow(SW_SHOW);
+
+			m_staticControls[row * m_nCols + 2]->SetWindowText(CString(data.inputDescription.c_str()));
+			m_staticControls[row * m_nCols + 2]->ShowWindow(SW_SHOW);
+
+			m_staticControls[row * m_nCols + 3]->SetWindowText(_T("OFF"));
+			m_staticControls[row * m_nCols + 3]->ShowWindow(SW_SHOW);
+			m_staticControls[row * m_nCols + 3]->SetBkColor(RGB(255, 0, 0));
+
+			m_staticControls[row * m_nCols + 4]->SetWindowText(CString(data.outputAddress.c_str()));
+			m_staticControls[row * m_nCols + 4]->ShowWindow(SW_SHOW);
+
+			m_staticControls[row * m_nCols + 5]->SetWindowText(CString(data.outputDescription.c_str()));
+			m_staticControls[row * m_nCols + 5]->ShowWindow(SW_SHOW);
+		}
+		else {
+			// 闅愯棌杩欎竴琛岀殑鎵�鏈夋帶浠�
+			for (int col = 0; col < m_nCols; ++col) {
+				m_staticControls[row * m_nCols + col]->ShowWindow(SW_HIDE);
+			}
+		}
 	}
+
 	UpdatePageInfo();
 }
 
 void CIOMonitoringDlg::ClearDynamicControls()
 {
-	for (auto* pStatic : m_staticControls)
-	{
-		if (pStatic)
-		{
+	for (auto* pStatic : m_staticControls) {
+		if (pStatic) {
 			pStatic->DestroyWindow();
 			delete pStatic;
 		}
@@ -243,10 +291,32 @@
 	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()
 
 
@@ -311,6 +381,8 @@
 	CreateDynamicControls();
 	DisplayCurrentPage();
 
+	SetTimer(TIMER_READ_PLC_DATA, 500, nullptr);
+
 	return TRUE;  // return TRUE unless you set the focus to a control
 	// 寮傚父: OCX 灞炴�ч〉搴旇繑鍥� FALSE
 }
@@ -354,3 +426,21 @@
 		AfxMessageBox(_T("宸茬粡鏄渶鍚庝竴椤碉紒"));
 	}
 }
+
+void CIOMonitoringDlg::OnTimer(UINT_PTR nIDEvent)
+{
+	// TODO: 鍦ㄦ娣诲姞娑堟伅澶勭悊绋嬪簭浠g爜鍜�/鎴栬皟鐢ㄩ粯璁ゅ��
+	if (TIMER_READ_PLC_DATA == nIDEvent) {
+		//ASSERT(m_pPLC);
+		UpdatePLCStates();
+		Sleep(100);
+	}
+	CDialogEx::OnTimer(nIDEvent);
+}
+
+void CIOMonitoringDlg::OnClose()
+{
+	// TODO: 鍦ㄦ娣诲姞娑堟伅澶勭悊绋嬪簭浠g爜鍜�/鎴栬皟鐢ㄩ粯璁ゅ��
+	KillTimer(TIMER_READ_PLC_DATA);
+	CDialogEx::OnClose();
+}

--
Gitblit v1.9.3