using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace AutoFocusTool
{
///
/// 自动标定独立窗口:4x4 显示16个well,标定完一个就在对应格显示该well照片,
/// 顶部大字提示"正在标定第几个well",底部显示当前实时画面+参数。可全屏。
///
public partial class CalibWindow : Window
{
public event Action StopRequested;
private readonly Border[] _cells = new Border[17]; // 1-16
private readonly Image[] _cellImgs = new Image[17];
private readonly TextBlock[] _cellCaps = new TextBlock[17];
private int _activeWell = 0; // 当前正在标定的well,实时帧画到这一格
public CalibWindow()
{
InitializeComponent();
BuildGrid();
}
private void BuildGrid()
{
for (int i = 1; i <= 16; i++)
{
var img = new Image { Stretch = Stretch.Uniform };
var cap = new TextBlock
{
Text = $"well {i}",
Foreground = Brushes.White,
HorizontalAlignment = HorizontalAlignment.Center,
TextAlignment = TextAlignment.Center,
TextWrapping = TextWrapping.Wrap,
FontSize = 15,
FontWeight = FontWeights.Bold
};
var grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Grid.SetRow(img, 0); Grid.SetRow(cap, 1);
grid.Children.Add(img); grid.Children.Add(cap);
var cell = new Border
{
BorderBrush = new SolidColorBrush(Color.FromRgb(0x55, 0x55, 0x55)),
BorderThickness = new Thickness(1),
Margin = new Thickness(3),
Background = new SolidColorBrush(Color.FromRgb(0x1A, 0x1A, 0x1A)),
Child = grid
};
_cells[i] = cell; _cellImgs[i] = img; _cellCaps[i] = cap;
WellGrid.Children.Add(cell);
}
}
/// 顶部大进度提示。
public void SetProgress(string big, string sub = "")
{
Dispatcher.Invoke(() => { TxtBigProgress.Text = big; if (sub != null) TxtSubProgress.Text = sub; });
}
/// 实时画面:直接画到当前正在标定的well格子里。
public void SetCurrentFrame(BitmapSource bmp)
{
Dispatcher.Invoke(() =>
{
if (_activeWell >= 1 && _activeWell <= 16)
_cellImgs[_activeWell].Source = bmp;
});
}
/// 底部当前参数/步骤文字,同时显示在当前活动well格标题上(大字)。
public void SetCurrentInfo(string param, string step)
{
Dispatcher.Invoke(() =>
{
if (param != null) TxtCurParams.Text = param;
if (step != null) TxtCurStep.Text = step;
// 当前well格标题实时显示步骤+参数
if (_activeWell >= 1 && _activeWell <= 16)
{
_cellCaps[_activeWell].Text = $"well {_activeWell} 标定中\n{step}\n{param}";
_cellCaps[_activeWell].Foreground = Brushes.Yellow;
}
});
}
/// 高亮"正在标定"的well格(黄框)。
public void MarkActive(int well)
{
_activeWell = well;
Dispatcher.Invoke(() =>
{
for (int i = 1; i <= 16; i++)
_cells[i].BorderBrush = new SolidColorBrush(Color.FromRgb(0x55, 0x55, 0x55));
if (well >= 1 && well <= 16)
{
_cells[well].BorderBrush = Brushes.Orange;
_cells[well].BorderThickness = new Thickness(3);
_cellCaps[well].Text = $"well {well} 标定中...";
}
});
}
/// 某well标定完成,显示其照片+结果。
public void SetWellResult(int well, BitmapSource bmp, string caption, bool ok)
{
Dispatcher.Invoke(() =>
{
if (well < 1 || well > 16) return;
_cellImgs[well].Source = bmp;
_cellCaps[well].Text = caption;
_cellCaps[well].Foreground = ok ? Brushes.LightGreen : Brushes.Orange;
_cells[well].BorderBrush = ok ? Brushes.LimeGreen : Brushes.Orange;
_cells[well].BorderThickness = new Thickness(2);
});
}
private void BtnCalStop_Click(object sender, RoutedEventArgs e)
{
StopRequested?.Invoke();
TxtBigProgress.Text = "已请求停止...";
}
}
}