gf zhou 233737bc31 添加接口null值检查
打开螺丝引导页面逻辑修改
程序重复打开命名修改
添加心跳线程
2025-04-16 11:31:16 +08:00

550 lines
20 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using WindowsFormsApp1.mod;
using WindowsFormsApp1.db;
using System.Collections.Generic;
using System.Linq;
namespace WindowsFormsApp1
{
public partial class ImageDisplayForm : Form
{
#region 控件字段
protected PictureBox pictureBox;
#endregion
#region 私有字段
private IContainer components = null;
private Bitmap originalImage;
private Bitmap markedImage;
private Timer blinkTimer;
private Timer autoUpdateTimer;
private string currentName = "";
private string currentPosition = "";
private bool isBlinking = false;
// 圆形标记相关参数
private float currentRadius = 20f;
private const float MIN_RADIUS = 30f;
private const float MAX_RADIUS = 55f;
private bool isExpanding = true;
private const float RADIUS_CHANGE_SPEED = 2f;
// 添加上一个位置的坐标
private int previousX = -1;
private int previousY = -1;
private bool hasPreviousPosition = false;
// 添加所有点位的列表
private List<t_imageposition> allPositions = new List<t_imageposition>();
private bool showAllPositions = true;
// 添加历史位置列表
private List<PositionHistory> previousPositions = new List<PositionHistory>();
// 定义历史位置类
private class PositionHistory
{
public int X { get; set; }
public int Y { get; set; }
public string Position { get; set; }
}
#endregion
#region 构造函数和初始化
public ImageDisplayForm()
{
InitializeComponent();
SetCurrentName(Init.ImageName);
// 初始化自动更新定时器
autoUpdateTimer = new Timer();
autoUpdateTimer.Interval = 1000;
autoUpdateTimer.Tick += new EventHandler(AutoUpdateTimer_Tick);
autoUpdateTimer.Start();
// 添加窗体加载事件
this.Load += ImageDisplayForm_Load;
}
private void ImageDisplayForm_Load(object sender, EventArgs e)
{
// 设置窗体大小为屏幕工作区的80%
Screen screen = Screen.FromControl(this);
this.Size = new Size(
(int)(screen.WorkingArea.Width * 0.8),
(int)(screen.WorkingArea.Height * 0.8)
);
this.CenterToScreen();
// 或者使用以下代码设置为全屏但非最大化
// this.WindowState = FormWindowState.Normal;
// this.FormBorderStyle = FormBorderStyle.None;
// this.Bounds = Screen.FromControl(this).Bounds;
}
#endregion
#region 公共方法
// 设置当前名称的方法,可以从外部调用
public void SetCurrentName(string name)
{
currentName = name;
LoadImageByName(name);
}
#endregion
#region 图像加载和处理
// 根据名称加载图片
private void LoadImageByName(string name)
{
if (string.IsNullOrEmpty(name))
return;
string imagesFolder = Path.Combine(Application.StartupPath, "images");
// 检查目录是否存在
if (!Directory.Exists(imagesFolder))
{
MessageBox.Show($"图片目录不存在: {imagesFolder}");
return;
}
// 获取目录中所有文件
string[] files = Directory.GetFiles(imagesFolder);
// 查找文件名以指定名称开头的文件
string imagePath = null;
foreach (string file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
if (fileName.Equals(name, StringComparison.OrdinalIgnoreCase))
{
imagePath = file;
break;
}
}
if (!string.IsNullOrEmpty(imagePath))
{
try
{
// 停止闪烁定时器并重置状态
blinkTimer?.Stop();
isBlinking = false;
// 加载新图片
originalImage = new Bitmap(imagePath);
markedImage = new Bitmap(originalImage);
pictureBox.Image = markedImage;
// 加载所有点位并标注
LoadAllPositions(name);
}
catch (Exception ex)
{
MessageBox.Show($"加载图片失败:{ex.Message}");
}
}
else
{
MessageBox.Show($"未找到名称为 {name} 的图片");
}
}
// 加载所有点位并标注
private void LoadAllPositions(string name)
{
try
{
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
allPositions = sQLiteHelper_position.Query(x => x.name == name).Data;
if (allPositions != null && allPositions.Count > 0)
{
// 重新绘制图像,标注所有点位
RedrawImage();
}
}
catch (Exception ex)
{
Console.WriteLine($"加载所有点位失败:{ex.Message}");
}
}
// 重新绘制图像,包括所有标记
private void RedrawImage()
{
if (originalImage == null)
return;
// 创建新的标记图像
if (markedImage != null)
markedImage.Dispose();
markedImage = new Bitmap(originalImage);
using (Graphics g = Graphics.FromImage(markedImage))
{
// 设置高质量绘图模式
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// 绘制所有点位(白色实心圆和序号)
if (showAllPositions && allPositions != null)
{
for (int i = 0; i < allPositions.Count; i++)
{
var pos = allPositions[i];
// 绘制白色实心圆
using (Brush whiteBrush = new SolidBrush(Color.White))
{
g.FillEllipse(whiteBrush,
pos.x - MIN_RADIUS,
pos.y - MIN_RADIUS,
MIN_RADIUS * 2,
MIN_RADIUS * 2);
}
// 绘制白色序号
using (Font font = new Font("Arial", 16, FontStyle.Bold))
using (Brush textBrush = new SolidBrush(Color.Black))
{
string text = pos.position; // 使用position属性
SizeF textSize = g.MeasureString(text, font);
g.DrawString(text, font, textBrush,
pos.x - textSize.Width / 2,
pos.y - textSize.Height / 2);
}
}
}
// 绘制所有历史位置的绿色标记
foreach (var history in previousPositions)
{
using (Brush greenBrush = new SolidBrush(Color.Green))
{
g.FillEllipse(greenBrush,
history.X - MIN_RADIUS,
history.Y - MIN_RADIUS,
MIN_RADIUS * 2,
MIN_RADIUS * 2);
}
// 绘制绿色标记上的文字
using (Font font = new Font("Arial", 16, FontStyle.Bold))
using (Brush textBrush = new SolidBrush(Color.White))
{
string text = history.Position;
SizeF textSize = g.MeasureString(text, font);
g.DrawString(text, font, textBrush,
history.X - textSize.Width / 2,
history.Y - textSize.Height / 2);
}
}
}
pictureBox.Image = markedImage;
}
// 自动更新定时器事件
private void AutoUpdateTimer_Tick(object sender, EventArgs e)
{
if(Init.ScrewBegin != 1)
{
this.Close();
return;
}
// 获取当前位置变量
//string newPosition = GetCurrentPosition();
string newPosition = Init.ImageLocation.ToString();
if (newPosition != currentPosition || (string.IsNullOrEmpty(currentPosition) && !string.IsNullOrEmpty(newPosition)))
{
// 保存上一个位置
if (!string.IsNullOrEmpty(currentPosition))
{
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
var positions = sQLiteHelper_position.Query(x => x.name == currentName
&& x.position == currentPosition).Data;
if (positions != null && positions.Count > 0)
{
var pos = positions[0];
// 检查是否已经存在相同位置的历史记录
bool exists = false;
foreach (var history in previousPositions)
{
if (history.X == pos.x && history.Y == pos.y)
{
// 更新位置文本
history.Position = currentPosition;
exists = true;
break;
}
}
// 如果不存在,添加到历史记录
if (!exists)
{
previousPositions.Add(new PositionHistory
{
X = pos.x,
Y = pos.y,
Position = currentPosition
});
}
// 设置最近的历史位置
previousX = pos.x;
previousY = pos.y;
hasPreviousPosition = true;
}
}
currentPosition = newPosition;
// 如果名称和位置都有值,则查询坐标并标注
if (!string.IsNullOrEmpty(currentName) && !string.IsNullOrEmpty(currentPosition))
{
QueryAndMarkPosition(currentName, currentPosition);
}
}
}
// 获取当前位置的方法(需要根据实际情况实现)
private string GetCurrentPosition()
{
// 生成1-10的随机数
Random random = new Random();
int position = random.Next(1, 11); // 1到10之间的随机数
return position.ToString();
}
// 根据名称和位置查询坐标并标注
private void QueryAndMarkPosition(string name, string position)
{
try
{
// 从已加载的点位列表中查找,避免重复查询数据库
var pos = allPositions?.FirstOrDefault(x => x.name == name && x.position == position);
if (pos != null)
{
// 标记位置
if (originalImage != null)
{
isBlinking = true; // 设置为 true 后不再切换
currentRadius = MIN_RADIUS; // 重置半径
// 确保闪烁定时器已启动
if (blinkTimer != null && !blinkTimer.Enabled)
blinkTimer.Start();
// 在图像上绘制标记
DrawMark(pos.x, pos.y);
}
}
else
{
// 如果在内存中没有找到,尝试从数据库查询
SQLiteHelper<t_imageposition> sQLiteHelper_position = new db.SQLiteHelper<t_imageposition>();
var positions = sQLiteHelper_position.Query(x => x.name == name && x.position == position).Data;
if (positions != null && positions.Count > 0)
{
pos = positions[0];
// 添加到内存中的列表
if (!allPositions.Any(x => x.name == name && x.position == position))
{
allPositions.Add(pos);
}
// 标记位置
if (originalImage != null)
{
isBlinking = true;
currentRadius = MIN_RADIUS;
if (blinkTimer != null && !blinkTimer.Enabled)
blinkTimer.Start();
DrawMark(pos.x, pos.y);
}
}
else
{
// 如果没有找到对应的位置,停止闪烁
isBlinking = false;
blinkTimer?.Stop();
// 仍然重绘图像以显示所有点位和之前的绿色标记
RedrawImage();
Console.WriteLine($"未找到名称为 {name} 位置为 {position} 的坐标");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查询坐标失败:{ex.Message}");
// 发生异常时也重绘图像
RedrawImage();
}
}
// 在图像上绘制标记
private void DrawMark(int x, int y)
{
if (originalImage == null)
return;
// 先重新绘制基础图像(包含所有点位标记)
RedrawImage();
using (Graphics g = Graphics.FromImage(markedImage))
{
// 设置高质量绘图模式
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
if (isBlinking)
{
// 绘制当前位置的黄色实心圆
using (Brush brush = new SolidBrush(Color.Yellow))
{
g.FillEllipse(brush,
x - MIN_RADIUS,
y - MIN_RADIUS,
MIN_RADIUS * 2,
MIN_RADIUS * 2);
}
// 绘制白色数字标记
using (Font font = new Font("Arial", 16, FontStyle.Bold))
using (Brush textBrush = new SolidBrush(Color.Black))
{
string text = currentPosition;
SizeF textSize = g.MeasureString(text, font);
g.DrawString(text, font, textBrush,
x - textSize.Width / 2,
y - textSize.Height / 2);
}
// 绘制黄色虚线空心圆闪烁效果
using (Pen yellowPen = new Pen(Color.Yellow, 2))
{
yellowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawEllipse(yellowPen,
x - currentRadius,
y - currentRadius,
currentRadius * 2,
currentRadius * 2);
}
}
}
pictureBox.Image = markedImage;
}
// 闪烁定时器事件
private void BlinkTimer_Tick(object sender, EventArgs e)
{
// 从已加载的点位列表中查找当前位置
var pos = allPositions?.FirstOrDefault(x => x.name == currentName && x.position == currentPosition);
if (pos != null)
{
// 直接调用DrawMark方法绘制标记
DrawMark(pos.x, pos.y);
// 更新半径
if (isExpanding)
{
currentRadius += RADIUS_CHANGE_SPEED;
if (currentRadius >= MAX_RADIUS)
{
currentRadius = MAX_RADIUS;
isExpanding = false;
}
}
else
{
currentRadius -= RADIUS_CHANGE_SPEED;
if (currentRadius <= MIN_RADIUS)
{
currentRadius = MIN_RADIUS;
isExpanding = true;
}
}
}
}
#endregion
#region 资源释放
protected override void Dispose(bool disposing)
{
if (disposing)
{
blinkTimer?.Stop();
blinkTimer?.Dispose();
autoUpdateTimer?.Stop();
autoUpdateTimer?.Dispose();
originalImage?.Dispose();
markedImage?.Dispose();
components?.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.blinkTimer = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBox
//
this.pictureBox.BackColor = System.Drawing.Color.LightGray;
this.pictureBox.Cursor = System.Windows.Forms.Cursors.Cross;
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Margin = new System.Windows.Forms.Padding(6);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(1600, 1250);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// blinkTimer
//
this.blinkTimer.Interval = 50; // 修改为50毫秒
this.blinkTimer.Tick += new System.EventHandler(this.BlinkTimer_Tick);
//
// ImageDisplayForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1600, 1250);
this.Controls.Add(this.pictureBox);
this.Margin = new System.Windows.Forms.Padding(6);
this.Name = "ImageDisplayForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "螺丝引导";
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
}
}