添加断开连接和重连状态的InfoImage显示功能

This commit is contained in:
zqm
2025-10-30 10:30:55 +08:00
parent ccc100f049
commit 8820857d8a

View File

@@ -808,6 +808,24 @@ namespace JoyD.Windows.CS.Toprie
case ConnectionStatus.Connected:
Console.WriteLine("设备已连接");
// 清除InfoImage和显示标志
lock (_infoImageLock)
{
if (_infoImage != null)
{
try
{
_infoImage.Dispose();
_infoImage = null;
}
catch (Exception ex)
{
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
}
}
}
_isDisplayingInfo = false;
// 清除错误信息
ShowError(string.Empty);
@@ -856,6 +874,48 @@ namespace JoyD.Windows.CS.Toprie
}
}
// 在InfoImage中显示连接断开信息
lock (_infoImageLock)
{
// 释放之前的InfoImage资源
if (_infoImage != null)
{
try
{
_infoImage.Dispose();
}
catch (Exception ex)
{
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
}
}
// 创建新的InfoImage并绘制断开连接字样
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
using (Graphics g = Graphics.FromImage(_infoImage))
{
// 设置半透明背景
using (SolidBrush brush = new SolidBrush(Color.FromArgb(128, Color.Black)))
{
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
}
// 绘制断开连接字样
using (Font font = new Font("Arial", 36, FontStyle.Bold))
using (SolidBrush textBrush = new SolidBrush(Color.Red))
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
g.DrawString("连接断开", font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
}
}
}
// 设置显示信息标志并更新UI
_isDisplayingInfo = true;
UpdateImageOnUI();
if (!string.IsNullOrEmpty(e.DeviceInfo))
{
ShowError(e.DeviceInfo);
@@ -866,10 +926,55 @@ namespace JoyD.Windows.CS.Toprie
}
break;
case ConnectionStatus.Connecting:
case ConnectionStatus.Reconnecting:
Console.WriteLine($"正在连接设备...{(!string.IsNullOrEmpty(e.DeviceInfo) ? " " + e.DeviceInfo : "")}");
ShowError(string.Empty); // 清除之前的错误信息
break;
case ConnectionStatus.Reconnecting:
Console.WriteLine($"正在重新连接设备...{(!string.IsNullOrEmpty(e.DeviceInfo) ? " " + e.DeviceInfo : "")}");
ShowError(string.Empty); // 清除之前的错误信息
// 在InfoImage中显示正在重连信息
lock (_infoImageLock)
{
// 释放之前的InfoImage资源
if (_infoImage != null)
{
try
{
_infoImage.Dispose();
}
catch (Exception ex)
{
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
}
}
// 创建新的InfoImage并绘制正在重连字样
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
using (Graphics g = Graphics.FromImage(_infoImage))
{
// 设置半透明背景
using (SolidBrush brush = new SolidBrush(Color.FromArgb(128, Color.Black)))
{
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
}
// 绘制正在重连字样
using (Font font = new Font("Arial", 36, FontStyle.Bold))
using (SolidBrush textBrush = new SolidBrush(Color.Yellow))
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
g.DrawString("正在重连...", font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
}
}
}
// 设置显示信息标志并更新UI
_isDisplayingInfo = true;
UpdateImageOnUI();
break;
}
}
catch (Exception ex)