From e0b180a728b4cb091fbf76172658409806121b6f Mon Sep 17 00:00:00 2001
From: darker <mr.darker@163.com>
Date: 星期四, 16 一月 2025 17:46:31 +0800
Subject: [PATCH] 1. 添加Robot移动标识 2. 添加Gdiplus模块 3. 添加Robot旋转的函数(未完成)

---
 SourceCode/Bond/Servo/Servo.cpp    |   26 +++++++++++++
 SourceCode/Bond/Servo/stdafx.h     |    3 +
 SourceCode/Bond/Servo/Servo.h      |    2 +
 SourceCode/Bond/Servo/ServoDlg.cpp |   49 +++++++++++++++++++++++-
 SourceCode/Bond/Servo/ServoDlg.h   |    2 +
 5 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/SourceCode/Bond/Servo/Servo.cpp b/SourceCode/Bond/Servo/Servo.cpp
index b422966..c1a7e0b 100644
--- a/SourceCode/Bond/Servo/Servo.cpp
+++ b/SourceCode/Bond/Servo/Servo.cpp
@@ -7,6 +7,11 @@
 #include "ServoDlg.h"
 #include "ServoGraph.h"
 
+using namespace Gdiplus;
+// 声明全局变量,用于管理 GDI+ 初始化
+ULONG_PTR g_diplusToken;
+GdiplusStartupInput g_diplusStartupInput;
+
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #endif
@@ -93,6 +98,10 @@
 	HSMS_Initialize();
 
 
+	// 初始化 GDI+
+	InitGDIPlus();
+
+
 	CServoDlg dlg;
 	m_pMainWnd = &dlg;
 	INT_PTR nResponse = dlg.DoModal();
@@ -129,5 +138,22 @@
 	HSMS_Term();
 	RX_Term();
 
+	// 清理 GDI+
+	TermGDIPlus();
+
 	return CWinApp::ExitInstance();
 }
+
+// 初始化 GDI+
+void CServoApp::InitGDIPlus()
+{
+	// 初始化 GDI+ 图形库
+	GdiplusStartup(&g_diplusToken, &g_diplusStartupInput, NULL);
+}
+
+// 清理 GDI+
+void CServoApp::TermGDIPlus()
+{
+	// 清理 GDI+ 图形库
+	GdiplusShutdown(g_diplusToken);
+}
\ No newline at end of file
diff --git a/SourceCode/Bond/Servo/Servo.h b/SourceCode/Bond/Servo/Servo.h
index bb306cd..04d5fb2 100644
--- a/SourceCode/Bond/Servo/Servo.h
+++ b/SourceCode/Bond/Servo/Servo.h
@@ -20,6 +20,8 @@
 {
 public:
 	CServoApp();
+	void InitGDIPlus();
+	void TermGDIPlus();
 
 
 public:
diff --git a/SourceCode/Bond/Servo/ServoDlg.cpp b/SourceCode/Bond/Servo/ServoDlg.cpp
index e0156b3..77b85f5 100644
--- a/SourceCode/Bond/Servo/ServoDlg.cpp
+++ b/SourceCode/Bond/Servo/ServoDlg.cpp
@@ -11,6 +11,7 @@
 #include "SecsTestDlg.h"
 #include <chrono>
 #include <thread>
+#include <cmath>
 
 
 #ifdef _DEBUG
@@ -79,6 +80,7 @@
 	m_crBkgnd = APPDLG_BACKGROUND_COLOR;
 	m_hbrBkgnd = nullptr;
 	m_bShowLogWnd = FALSE;
+	m_bIsRobotMoving = FALSE;
 	m_pLogDlg = nullptr;
 }
 
@@ -513,6 +515,9 @@
 	auto startTime = std::chrono::steady_clock::now();
 	auto endTime = startTime + std::chrono::milliseconds(duration);
 
+	// 开始移动,设置标记
+	m_bIsRobotMoving = TRUE;
+
 	// 开始平滑移动
 	while (std::chrono::steady_clock::now() < endTime) {
 		auto currentTime = std::chrono::steady_clock::now();
@@ -539,6 +544,41 @@
 	m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, endX + arm2Offset, 294);
 
 	// 界面重绘
+	Invalidate();
+
+	// 动画结束,设置标记
+	m_bIsRobotMoving = FALSE;
+}
+
+void CServoDlg::RotateRobot(float angleInDegrees)
+{
+	return;
+	// 将角度转换为弧度
+	float angleInRadians = angleInDegrees * (std::acos(-1) / 180.0f);
+
+	// 获取机器人图片的当前坐标和中心
+	auto* pImage = m_pGraph->GetImage(IMAGE_ROBOT);
+	if (!pImage) return;
+
+	int cx = pImage->x + pImage->bmWidth / 2;  // 图片中心 X
+	int cy = pImage->y + pImage->bmHeight / 2; // 图片中心 Y
+
+	// m_pGraph->UpdateImageAngle(IMAGE_ROBOT, angleInDegrees);
+
+	auto* pRobot1 = m_pGraph->GetIndicateBox(INDICATE_ROBOT_ARM1);
+	auto* pRobot2 = m_pGraph->GetIndicateBox(INDICATE_ROBOT_ARM2);
+
+	// 旋转指示框的坐标
+	int newArmX1 = static_cast<int>(cx + (pRobot1->x - cx) * cos(angleInRadians) - (pRobot1->y - cy) * sin(angleInRadians));
+	int newArmY1 = static_cast<int>(cy + (pRobot1->x - cx) * sin(angleInRadians) + (pRobot1->y - cy) * cos(angleInRadians));
+
+	int newArmX2 = static_cast<int>(cx + (pRobot2->x - cx) * cos(angleInRadians) - (pRobot2->y - cy) * sin(angleInRadians));
+	int newArmY2 = static_cast<int>(cy + (pRobot2->x - cx) * sin(angleInRadians) + (pRobot2->y - cy) * cos(angleInRadians));
+
+	// 更新指示框的位置
+	m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM1, newArmX1, newArmY1);
+	m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, newArmX2, newArmY2);
+
 	Invalidate();
 }
 
@@ -605,7 +645,10 @@
 BOOL CServoDlg::OnEraseBkgnd(CDC* pDC)
 {
 	// TODO: 在此添加消息处理程序代码和/或调用默认值
-
-	//return CDialogEx::OnEraseBkgnd(pDC);
-	return TRUE;
+	if (m_bIsRobotMoving) {
+		// 禁止刷新背景,避免闪烁
+		return TRUE;
+	}
+	
+	return CDialogEx::OnEraseBkgnd(pDC);
 }
diff --git a/SourceCode/Bond/Servo/ServoDlg.h b/SourceCode/Bond/Servo/ServoDlg.h
index f0a270e..c3842e6 100644
--- a/SourceCode/Bond/Servo/ServoDlg.h
+++ b/SourceCode/Bond/Servo/ServoDlg.h
@@ -20,6 +20,7 @@
 	void Resize();
 	void UpdateLogBtn();
 	void UpdateRobotPosition(float percentage);
+	void RotateRobot(float angleInDegrees);
 
 
 // 对话框数据
@@ -32,6 +33,7 @@
 
 
 private:
+	BOOL m_bIsRobotMoving;
 	BOOL m_bShowLogWnd;
 	CLogDlg* m_pLogDlg;
 
diff --git a/SourceCode/Bond/Servo/stdafx.h b/SourceCode/Bond/Servo/stdafx.h
index ea7a70f..2240fc8 100644
--- a/SourceCode/Bond/Servo/stdafx.h
+++ b/SourceCode/Bond/Servo/stdafx.h
@@ -33,7 +33,8 @@
 
 #include <afxcontrolbars.h>     // 功能区和控件条的 MFC 支持
 
-
+// GDI+
+#include <gdiplus.h>
 
 #include "..\RxWindows1.0\include\RxWindowsLib.h"
 #include "..\HSMSSDK\Include\HSMSSDK.h"

--
Gitblit v1.9.3