优化图像更新流程:使用_isPaused标志管理暂停状态,统一Info显示逻辑,实现完整的三步绘制流程
This commit is contained in:
@@ -79,22 +79,17 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 暂停/恢复图像更新菜单项点击事件处理
|
/// 更新InfoImage显示
|
||||||
|
/// 1. 如果断开,显示重连信息
|
||||||
|
/// 2. 否则如果暂停,显示暂停信息
|
||||||
|
/// 3. 最后调用更新UI
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void PauseImageUpdateToolStripMenuItem_Click(object sender, EventArgs e)
|
private void UpdateInfo()
|
||||||
{
|
{
|
||||||
if (DesignMode) return;
|
if (DesignMode) return;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bool isPaused = pauseImageUpdateToolStripMenuItem.Text == "暂停图像更新";
|
|
||||||
|
|
||||||
if (isPaused)
|
|
||||||
{
|
|
||||||
pauseImageUpdateToolStripMenuItem.Text = "恢复图像更新";
|
|
||||||
Console.WriteLine("图像更新已暂停");
|
|
||||||
|
|
||||||
// 在InfoImage中绘制暂停字样
|
|
||||||
lock (_infoImageLock)
|
lock (_infoImageLock)
|
||||||
{
|
{
|
||||||
// 释放之前的InfoImage资源
|
// 释放之前的InfoImage资源
|
||||||
@@ -108,9 +103,17 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
{
|
{
|
||||||
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
|
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
_infoImage = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建新的InfoImage并绘制暂停字样
|
// 检查连接状态
|
||||||
|
bool isDisconnected = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Disconnected;
|
||||||
|
bool isReconnecting = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Reconnecting;
|
||||||
|
bool isPaused = _isPaused; // 使用_isPaused标志判断暂停状态
|
||||||
|
|
||||||
|
// 如果需要显示信息,创建InfoImage
|
||||||
|
if (isDisconnected || isReconnecting || isPaused)
|
||||||
|
{
|
||||||
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||||
using (Graphics g = Graphics.FromImage(_infoImage))
|
using (Graphics g = Graphics.FromImage(_infoImage))
|
||||||
{
|
{
|
||||||
@@ -120,55 +123,93 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
|
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制暂停字样
|
// 确定显示的文本和颜色
|
||||||
|
string text = "";
|
||||||
|
Color textColor = Color.White;
|
||||||
|
|
||||||
|
if (isDisconnected || isReconnecting)
|
||||||
|
{
|
||||||
|
// 断开或重连状态
|
||||||
|
text = isReconnecting ? "正在重连..." : "连接断开";
|
||||||
|
textColor = isReconnecting ? Color.Yellow : Color.Red;
|
||||||
|
}
|
||||||
|
else if (isPaused)
|
||||||
|
{
|
||||||
|
// 暂停状态
|
||||||
|
text = "暂停";
|
||||||
|
textColor = Color.Red;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绘制文本
|
||||||
using (Font font = new Font("Arial", 48, FontStyle.Bold))
|
using (Font font = new Font("Arial", 48, FontStyle.Bold))
|
||||||
using (SolidBrush textBrush = new SolidBrush(Color.Red))
|
using (SolidBrush textBrush = new SolidBrush(textColor))
|
||||||
{
|
{
|
||||||
StringFormat format = new StringFormat();
|
StringFormat format = new StringFormat();
|
||||||
format.Alignment = StringAlignment.Center;
|
format.Alignment = StringAlignment.Center;
|
||||||
format.LineAlignment = StringAlignment.Center;
|
format.LineAlignment = StringAlignment.Center;
|
||||||
g.DrawString("暂停", font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
g.DrawString(text, font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置显示信息标志并更新UI
|
// 设置显示标志
|
||||||
_isDisplayingInfo = true;
|
_isDisplayingInfo = (isDisconnected || isReconnecting || isPaused);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用更新UI
|
||||||
UpdateImageOnUI();
|
UpdateImageOnUI();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
pauseImageUpdateToolStripMenuItem.Text = "暂停图像更新";
|
|
||||||
Console.WriteLine("图像更新已恢复");
|
|
||||||
|
|
||||||
// 清除InfoImage和显示标志
|
|
||||||
lock (_infoImageLock)
|
|
||||||
{
|
|
||||||
if (_infoImage != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_infoImage.Dispose();
|
|
||||||
_infoImage = null;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
|
Console.WriteLine($"更新Info显示时出错: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
_isDisplayingInfo = false;
|
|
||||||
|
|
||||||
// 立即更新UI
|
/// <summary>
|
||||||
UpdateImageOnUI();
|
/// 暂停/恢复图像更新菜单项点击事件处理
|
||||||
|
/// 1、暂停或恢复时,设置暂停状态,调用更新Info
|
||||||
|
/// </summary>
|
||||||
|
private void PauseImageUpdateToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (DesignMode) return;
|
||||||
|
|
||||||
// 恢复时,立即停止并重新开始图像接收,确保获取最新图像
|
try
|
||||||
|
{
|
||||||
|
// 切换暂停状态
|
||||||
|
_isPaused = !_isPaused;
|
||||||
|
|
||||||
|
if (_isPaused)
|
||||||
|
{
|
||||||
|
// 设置暂停状态
|
||||||
|
pauseImageUpdateToolStripMenuItem.Text = "恢复图像更新";
|
||||||
|
|
||||||
|
// 暂停时停止图像接收
|
||||||
if (_isReceivingImage && _deviceManager != null)
|
if (_isReceivingImage && _deviceManager != null)
|
||||||
|
{
|
||||||
|
_deviceManager.StopImageReceiving();
|
||||||
|
_isReceivingImage = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("图像更新已暂停");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 设置恢复状态
|
||||||
|
pauseImageUpdateToolStripMenuItem.Text = "暂停图像更新";
|
||||||
|
|
||||||
|
// 恢复时,立即停止并重新开始图像接收,确保获取最新图像
|
||||||
|
if (_deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Connected)
|
||||||
{
|
{
|
||||||
_deviceManager.StopImageReceiving();
|
_deviceManager.StopImageReceiving();
|
||||||
_deviceManager.StartImageReceiving();
|
_deviceManager.StartImageReceiving();
|
||||||
|
_isReceivingImage = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("图像更新已恢复");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按照用户要求:暂停或恢复时,设置暂停状态,调用更新Info
|
||||||
|
UpdateInfo();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -428,15 +469,13 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool _isPaused = false; // 暂停状态标志
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备管理器图像接收事件处理
|
/// 设备管理器图像接收事件处理
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e)
|
private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e)
|
||||||
{
|
{
|
||||||
// 如果图像更新已暂停,则不更新UI
|
|
||||||
// 通过菜单项文本判断是否暂停
|
|
||||||
if (pauseImageUpdateToolStripMenuItem.Text == "恢复图像更新")
|
|
||||||
return;
|
|
||||||
if (DesignMode) return;
|
if (DesignMode) return;
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -475,7 +514,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 按照用户要求:收到图像数据后,将图像保存到LastImage中,调用更新
|
// 按照用户要求:收到图像数据后,将图像保存到LastImage中
|
||||||
lock (_lastImageLock)
|
lock (_lastImageLock)
|
||||||
{
|
{
|
||||||
// 释放旧的LastImage资源
|
// 释放旧的LastImage资源
|
||||||
@@ -487,7 +526,10 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
_lastImage = newImage;
|
_lastImage = newImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用更新UI
|
// 按照用户要求:调用更新到UI
|
||||||
|
// 只有当图像更新未暂停时才更新UI
|
||||||
|
if (!_isPaused)
|
||||||
|
{
|
||||||
this.BeginInvoke(new Action(() =>
|
this.BeginInvoke(new Action(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -504,6 +546,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"处理接收到的图像时出错: {ex.Message}");
|
Console.WriteLine($"处理接收到的图像时出错: {ex.Message}");
|
||||||
@@ -630,29 +673,6 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
g.DrawString("测试信息", font, textBrush, 10, 10);
|
g.DrawString("测试信息", font, textBrush, 10, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否需要绘制暂停状态的mask信息
|
|
||||||
bool isPaused = pauseImageUpdateToolStripMenuItem.Text == "恢复图像更新";
|
|
||||||
if (isPaused)
|
|
||||||
{
|
|
||||||
// 绘制半透明黑色背景
|
|
||||||
using (Brush brush = new SolidBrush(Color.FromArgb(100, Color.Black)))
|
|
||||||
{
|
|
||||||
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 绘制"暂停"文字
|
|
||||||
using (Font font = new Font("微软雅黑", 48, FontStyle.Bold))
|
|
||||||
using (Brush textBrush = new SolidBrush(Color.White))
|
|
||||||
{
|
|
||||||
StringFormat format = new StringFormat();
|
|
||||||
format.Alignment = StringAlignment.Center;
|
|
||||||
format.LineAlignment = StringAlignment.Center;
|
|
||||||
|
|
||||||
g.DrawString("暂停", font, textBrush,
|
|
||||||
new Rectangle(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -780,6 +800,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 处理连接状态变更
|
/// 处理连接状态变更
|
||||||
|
/// 2、断开或连接时,设置连接状态,调用更新Info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e)
|
private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e)
|
||||||
{
|
{
|
||||||
@@ -808,10 +829,6 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
case ConnectionStatus.Connected:
|
case ConnectionStatus.Connected:
|
||||||
Console.WriteLine("设备已连接");
|
Console.WriteLine("设备已连接");
|
||||||
|
|
||||||
// 清除显示标志并刷新UI
|
|
||||||
_isDisplayingInfo = false;
|
|
||||||
UpdateImageOnUI();
|
|
||||||
|
|
||||||
// 仅在首次连接时设置为热图模式,重连时保留之前的模式
|
// 仅在首次连接时设置为热图模式,重连时保留之前的模式
|
||||||
if (!_isReceivingImage) // 首次连接时_isReceivingImage为false
|
if (!_isReceivingImage) // 首次连接时_isReceivingImage为false
|
||||||
{
|
{
|
||||||
@@ -835,9 +852,19 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
|
|
||||||
// 开始接收图像(包含在try-catch中)
|
// 开始接收图像(包含在try-catch中)
|
||||||
if (!_isReceivingImage)
|
if (!_isReceivingImage)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
StartReceiveImage();
|
StartReceiveImage();
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"开始接收图像失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置连接状态后,调用更新Info
|
||||||
|
UpdateInfo();
|
||||||
break;
|
break;
|
||||||
case ConnectionStatus.Disconnected:
|
case ConnectionStatus.Disconnected:
|
||||||
Console.WriteLine("设备已断开连接");
|
Console.WriteLine("设备已断开连接");
|
||||||
@@ -857,39 +884,8 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在InfoImage中显示连接断开信息
|
// 设置连接状态后,调用更新Info
|
||||||
lock (_infoImageLock)
|
UpdateInfo();
|
||||||
{
|
|
||||||
// 如果_infoImage为null,则创建新对象
|
|
||||||
if (_infoImage == null)
|
|
||||||
{
|
|
||||||
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 直接在现有InfoImage上重绘,不需要Dispose
|
|
||||||
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))
|
if (!string.IsNullOrEmpty(e.DeviceInfo))
|
||||||
{
|
{
|
||||||
@@ -908,39 +904,8 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
Console.WriteLine($"正在重新连接设备...{(!string.IsNullOrEmpty(e.DeviceInfo) ? " " + e.DeviceInfo : "")}");
|
Console.WriteLine($"正在重新连接设备...{(!string.IsNullOrEmpty(e.DeviceInfo) ? " " + e.DeviceInfo : "")}");
|
||||||
ShowError(string.Empty); // 清除之前的错误信息
|
ShowError(string.Empty); // 清除之前的错误信息
|
||||||
|
|
||||||
// 在InfoImage中显示正在重连信息
|
// 设置重连状态后,调用更新Info
|
||||||
lock (_infoImageLock)
|
UpdateInfo();
|
||||||
{
|
|
||||||
// 如果_infoImage为null,则创建新对象
|
|
||||||
if (_infoImage == null)
|
|
||||||
{
|
|
||||||
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 直接在现有InfoImage上重绘,不需要Dispose
|
|
||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user