From 3463c77d73bca0a97c89e7a61da7a97ec2c23e6c Mon Sep 17 00:00:00 2001
From: darker <mr.darker@163.com>
Date: 星期五, 17 一月 2025 11:12:32 +0800
Subject: [PATCH] 1.消除编译产生的警告 2.添加Rotate角度的旋转 3.提取绘画图像,并封装成函数
---
SourceCode/Bond/Servo/ServoGraph.cpp | 74 ++++++++++++++++++++++++++-----------
SourceCode/Bond/Servo/Servo.cpp | 1
SourceCode/Bond/Servo/stdafx.h | 1
SourceCode/Bond/Servo/ServoGraph.h | 5 ++
SourceCode/Bond/Servo/ServoDlg.cpp | 37 ++++++++++++------
5 files changed, 83 insertions(+), 35 deletions(-)
diff --git a/SourceCode/Bond/Servo/Servo.cpp b/SourceCode/Bond/Servo/Servo.cpp
index c1a7e0b..550391a 100644
--- a/SourceCode/Bond/Servo/Servo.cpp
+++ b/SourceCode/Bond/Servo/Servo.cpp
@@ -7,7 +7,6 @@
#include "ServoDlg.h"
#include "ServoGraph.h"
-using namespace Gdiplus;
// 声明全局变量,用于管理 GDI+ 初始化
ULONG_PTR g_diplusToken;
GdiplusStartupInput g_diplusStartupInput;
diff --git a/SourceCode/Bond/Servo/ServoDlg.cpp b/SourceCode/Bond/Servo/ServoDlg.cpp
index 77b85f5..07cb989 100644
--- a/SourceCode/Bond/Servo/ServoDlg.cpp
+++ b/SourceCode/Bond/Servo/ServoDlg.cpp
@@ -552,33 +552,46 @@
void CServoDlg::RotateRobot(float angleInDegrees)
{
- return;
// 将角度转换为弧度
- float angleInRadians = angleInDegrees * (std::acos(-1) / 180.0f);
+ float angleInRadians = static_cast<float>(std::acos(-1)) / 180.0f * angleInDegrees;
// 获取机器人图片的当前坐标和中心
auto* pImage = m_pGraph->GetImage(IMAGE_ROBOT);
if (!pImage) return;
+ // 更新 Rotate 图片的角度,确保角度保持在 [0, 360) 范围内
+ m_pGraph->UpdateImageAngle(IMAGE_ROBOT, static_cast<float>(fmod(pImage->angle + angleInDegrees + 360, 360)));
+
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));
+ if (pRobot1 && pRobot2) {
+ int newArmX1 = pImage->x + 20;
+ int newArmY1 = 294;
- 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));
+ int newArmX2 = pImage->x + 73;
+ int newArmY2 = 294;
- // 更新指示框的位置
- m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM1, newArmX1, newArmY1);
- m_pGraph->UpdateIndicateBoxCoordinates(INDICATE_ROBOT_ARM2, newArmX2, newArmY2);
+ if (angleInDegrees != 0.0f) {
+ // 计算指示框1的新坐标
+ newArmX1 = static_cast<int>(cx + (pRobot1->x - cx) * cos(angleInRadians) - (pRobot1->y - cy) * sin(angleInRadians));
+ newArmY1 = static_cast<int>(cy + (pRobot1->x - cx) * sin(angleInRadians) + (pRobot1->y - cy) * cos(angleInRadians));
+ // 计算指示框2的新坐标
+ newArmX2 = static_cast<int>(cx + (pRobot2->x - cx) * cos(angleInRadians) - (pRobot2->y - cy) * sin(angleInRadians));
+ 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();
}
diff --git a/SourceCode/Bond/Servo/ServoGraph.cpp b/SourceCode/Bond/Servo/ServoGraph.cpp
index 32d4dfa..3e815c8 100644
--- a/SourceCode/Bond/Servo/ServoGraph.cpp
+++ b/SourceCode/Bond/Servo/ServoGraph.cpp
@@ -306,29 +306,9 @@
// 画IMAGE
- HDC hDCTemp = ::CreateCompatibleDC(hMemDC);
for (auto& item : m_images) {
- // 载入BMP
- if (item.hBitmap == nullptr) {
- item.hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),
- item.szPath, IMAGE_BITMAP, 0, 0,
- LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
- if (item.hBitmap != nullptr) {
- BITMAP bitmap;
- ::GetObject(item.hBitmap, sizeof(BITMAP), &bitmap);
- item.bmWidth = bitmap.bmWidth;
- item.bmHeight = bitmap.bmHeight;
- }
-
- }
-
- if (item.hBitmap != nullptr) {
- ::SelectObject(hDCTemp, item.hBitmap);
- ::BitBlt(hMemDC, item.x, item.y, item.bmWidth, item.bmHeight,
- hDCTemp, 0, 0, SRCCOPY);
- }
+ DrawImage(hMemDC, item);
}
- ::DeleteDC(hDCTemp);
// 画背景指示
@@ -368,7 +348,7 @@
}
// text
- ::DrawText(hMemDC, item.szText, strlen(item.szText),
+ ::DrawText(hMemDC, item.szText, static_cast<int>(strlen(item.szText)),
&rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
}
@@ -644,4 +624,54 @@
pIndicateBox->x = newX;
pIndicateBox->y = newY;
}
+}
+
+void CServoGraph::UpdateImageAngle(int id, float angle)
+{
+ IMAGE* pImage = GetImage(id);
+ if (pImage != nullptr) {
+ pImage->angle = angle;
+ }
+}
+
+void CServoGraph::DrawImage(HDC hMemDC, IMAGE& item)
+{
+ // 载入BMP
+ if (item.hBitmap == nullptr) {
+ item.hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),
+ item.szPath, IMAGE_BITMAP, 0, 0,
+ LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
+ if (item.hBitmap != nullptr) {
+ BITMAP bitmap;
+ ::GetObject(item.hBitmap, sizeof(BITMAP), &bitmap);
+ item.bmWidth = bitmap.bmWidth;
+ item.bmHeight = bitmap.bmHeight;
+ }
+ }
+
+ if (item.hBitmap != nullptr) {
+ // 使用 GDI+ 加载位图,并创建GDI+ Graphics 对象
+ Bitmap bitmap(item.hBitmap, nullptr);
+ Graphics graphics(hMemDC);
+
+ // 如果图像需要旋转
+ if (item.angle != 0.0f) {
+ // 角度转换为弧度
+ float angleInRadians = item.angle;
+
+ // 获取图像中心
+ REAL cx = static_cast<REAL>(item.x + item.bmWidth / 2); // 将中心X转换为REAL类型
+ REAL cy = static_cast<REAL>(item.y + item.bmHeight / 2); // 将中心Y转换为REAL类型
+
+ // 创建旋转矩阵
+ Matrix rotateMatrix;
+ rotateMatrix.RotateAt(angleInRadians, PointF(cx, cy));
+
+ // 应用旋转矩阵到图形
+ graphics.SetTransform(&rotateMatrix);
+ }
+
+ graphics.DrawImage(&bitmap, item.x, item.y);
+ graphics.ResetTransform();
+ }
}
\ No newline at end of file
diff --git a/SourceCode/Bond/Servo/ServoGraph.h b/SourceCode/Bond/Servo/ServoGraph.h
index 445d676..0a58675 100644
--- a/SourceCode/Bond/Servo/ServoGraph.h
+++ b/SourceCode/Bond/Servo/ServoGraph.h
@@ -75,6 +75,7 @@
HBITMAP hBitmap;
int bmWidth;
int bmHeight;
+ float angle;
} IMAGE;
@@ -168,6 +169,10 @@
HWND GetSafeWnd();
void UpdateImageCoordinates(int id, int newX, int newY);
void UpdateIndicateBoxCoordinates(int id, int newX, int newY);
+ void UpdateImageAngle(int id, float angle);
+
+private:
+ void DrawImage(HDC hMemDC, IMAGE& item);
private:
HWND m_hWnd;
diff --git a/SourceCode/Bond/Servo/stdafx.h b/SourceCode/Bond/Servo/stdafx.h
index 2240fc8..9b9a811 100644
--- a/SourceCode/Bond/Servo/stdafx.h
+++ b/SourceCode/Bond/Servo/stdafx.h
@@ -35,6 +35,7 @@
// GDI+
#include <gdiplus.h>
+using namespace Gdiplus;
#include "..\RxWindows1.0\include\RxWindowsLib.h"
#include "..\HSMSSDK\Include\HSMSSDK.h"
--
Gitblit v1.9.3