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/AxisSettingsDlg.cpp |    2 +
 SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp |   74 ++++++++++++++++++++++++++++++++++++-
 SourceCode/Bond/BondEq/View/IOMonitoringDlg.h   |    7 +++
 SourceCode/Bond/BLControlsSDK/include/BLLabel.h |    5 ++
 4 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/SourceCode/Bond/BLControlsSDK/include/BLLabel.h b/SourceCode/Bond/BLControlsSDK/include/BLLabel.h
index 45bc4de..e8f35f8 100644
--- a/SourceCode/Bond/BLControlsSDK/include/BLLabel.h
+++ b/SourceCode/Bond/BLControlsSDK/include/BLLabel.h
@@ -5,6 +5,8 @@
 #pragma once
 #endif // _MSC_VER >= 1000
 
+#include <functional> 
+
 /////////////////////////////////////////////////////////////////////////////
 // CBLLabel window
 enum FlashType { None, Text, Background };
@@ -18,6 +20,8 @@
     // 鏋勯�犱笌鏋愭瀯
     CBLLabel();
     virtual ~CBLLabel();
+
+    void SetClickCallback(std::function<void()> callback); // 璁剧疆鐐瑰嚮浜嬩欢鐨勫洖璋冨嚱鏁�
 
     // 灞炴�ц缃帴鍙�
     CBLLabel& SetBkColor(COLORREF crBkgnd);             // 璁剧疆鑳屾櫙棰滆壊
@@ -55,6 +59,7 @@
     TextAlign m_alignment;        // 鏂囨湰瀵归綈鏂瑰紡
     FlashType m_Type;             // 闂儊绫诲瀷
     HCURSOR  m_hCursor;           // 瓒呴摼鎺ュ厜鏍�
+    std::function<void()> m_clickCallback; // 鐐瑰嚮浜嬩欢鐨勫洖璋冨嚱鏁�
 
 protected:
     // MFC 娑堟伅鏄犲皠
diff --git a/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp b/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
index 96a53f7..dcdc219 100644
--- a/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
+++ b/SourceCode/Bond/BondEq/View/AxisSettingsDlg.cpp
@@ -952,10 +952,12 @@
 	else if (nIDEvent == TIMER_JOG_ADD && m_bJogAddPressed) {
 		TRACE("鎸佺画鍙戦�� JOG+\n");
 		handleAxisOperation(AxisOperationType::JOG_ADD, true); // 鎸佺画鍙戦�� JOG+
+		Sleep(20);
 	}
 	else if (nIDEvent == TIMER_JOG_SUB && m_bJogSubPressed) {
 		TRACE("鎸佺画鍙戦�� JOG-\n");
 		handleAxisOperation(AxisOperationType::JOG_SUB, true); // 鎸佺画鍙戦�� JOG-
+		Sleep(20);
 	}
 
 	CDialogEx::OnTimer(nIDEvent);
diff --git a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
index 2897dfb..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 瀵硅瘽妗�
 
@@ -177,7 +179,21 @@
 		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;
@@ -185,7 +201,7 @@
 	}
 }
 
-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();
@@ -205,6 +221,11 @@
 	pStatic->SetFontSize(nSize);
 	pStatic->SetDynamicFont(TRUE);
 
+	// 璁剧疆鍥炶皟
+	if (clickCallback) {
+		pStatic->SetClickCallback(clickCallback);
+	}
+
 	// 瀛樺偍鎺т欢鎸囬拡
 	m_staticControls.push_back(pStatic);
 }
@@ -214,11 +235,18 @@
 	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"));
@@ -263,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()
 
 
@@ -331,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
 }
@@ -374,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();
+}
diff --git a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.h b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.h
index 10c22c2..c8addf7 100644
--- a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.h
+++ b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.h
@@ -28,9 +28,10 @@
 
 	void UpdatePageInfo();			// 鏇存柊鍒嗛〉淇℃伅
 	void CreateDynamicControls();	// 鍔ㄦ�佸垱寤烘帶浠�
-	void CreateStaticControl(int x, int y, int width, int height, const CString& text, bool hasBorder = false, TextAlign alignment = AlignLeft); // 鍒涘缓闈欐�佹帶浠�
+	void CreateStaticControl(int x, int y, int width, int height, const CString& text, bool hasBorder = false, TextAlign alignment = AlignLeft, std::function<void()> clickCallback = nullptr); // 鍒涘缓闈欐�佹帶浠�
 	void DisplayCurrentPage();		// 鏄剧ず褰撳墠椤垫暟鎹�
 	void ClearDynamicControls();	// 娓呴櫎鍔ㄦ�佸垱寤虹殑鎺т欢
+	void UpdatePLCStates();			// 瀹氭椂鍣ㄦ洿鏂扮姸鎬佺殑鏂规硶
 
 private:
 	int m_nInitialWidth;
@@ -43,6 +44,8 @@
 	std::map<int, CFont*> m_mapFonts;		// 瀛椾綋鏄犲皠
 	std::map<int, CRect> m_mapCtrlLayouts;	// 鎺т欢甯冨眬鏄犲皠
 	std::vector<IOData> m_displayData;		// 褰撳墠鏄剧ず鐨勬暟鎹�
+	std::vector<CString> m_inputPLCAddresses;  // 瀛樺偍 1 鍒� PLC 鍦板潃
+	std::vector<CString> m_outputPLCAddresses; // 瀛樺偍 4 鍒� PLC 鍦板潃
 
 private:
 	CStatic m_staticPageNum;
@@ -57,4 +60,6 @@
 	afx_msg void OnSize(UINT nType, int cx, int cy);
 	afx_msg void OnBnClickedButtonPrevPage();
 	afx_msg void OnBnClickedButtonNextPage();
+	afx_msg void OnTimer(UINT_PTR nIDEvent);
+	afx_msg void OnClose();
 };

--
Gitblit v1.9.3