CalibWindow.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. namespace AutoFocusTool
  8. {
  9. /// <summary>
  10. /// 自动标定独立窗口:4x4 显示16个well,标定完一个就在对应格显示该well照片,
  11. /// 顶部大字提示"正在标定第几个well",底部显示当前实时画面+参数。可全屏。
  12. /// </summary>
  13. public partial class CalibWindow : Window
  14. {
  15. public event Action StopRequested;
  16. private readonly Border[] _cells = new Border[17]; // 1-16
  17. private readonly Image[] _cellImgs = new Image[17];
  18. private readonly TextBlock[] _cellCaps = new TextBlock[17];
  19. private int _activeWell = 0; // 当前正在标定的well,实时帧画到这一格
  20. public CalibWindow()
  21. {
  22. InitializeComponent();
  23. BuildGrid();
  24. }
  25. private void BuildGrid()
  26. {
  27. for (int i = 1; i <= 16; i++)
  28. {
  29. var img = new Image { Stretch = Stretch.Uniform };
  30. var cap = new TextBlock
  31. {
  32. Text = $"well {i}",
  33. Foreground = Brushes.White,
  34. HorizontalAlignment = HorizontalAlignment.Center,
  35. TextAlignment = TextAlignment.Center,
  36. TextWrapping = TextWrapping.Wrap,
  37. FontSize = 15,
  38. FontWeight = FontWeights.Bold
  39. };
  40. var grid = new Grid();
  41. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  42. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  43. Grid.SetRow(img, 0); Grid.SetRow(cap, 1);
  44. grid.Children.Add(img); grid.Children.Add(cap);
  45. var cell = new Border
  46. {
  47. BorderBrush = new SolidColorBrush(Color.FromRgb(0x55, 0x55, 0x55)),
  48. BorderThickness = new Thickness(1),
  49. Margin = new Thickness(3),
  50. Background = new SolidColorBrush(Color.FromRgb(0x1A, 0x1A, 0x1A)),
  51. Child = grid
  52. };
  53. _cells[i] = cell; _cellImgs[i] = img; _cellCaps[i] = cap;
  54. WellGrid.Children.Add(cell);
  55. }
  56. }
  57. /// <summary>顶部大进度提示。</summary>
  58. public void SetProgress(string big, string sub = "")
  59. {
  60. Dispatcher.Invoke(() => { TxtBigProgress.Text = big; if (sub != null) TxtSubProgress.Text = sub; });
  61. }
  62. /// <summary>实时画面:直接画到当前正在标定的well格子里。</summary>
  63. public void SetCurrentFrame(BitmapSource bmp)
  64. {
  65. Dispatcher.Invoke(() =>
  66. {
  67. if (_activeWell >= 1 && _activeWell <= 16)
  68. _cellImgs[_activeWell].Source = bmp;
  69. });
  70. }
  71. /// <summary>底部当前参数/步骤文字,同时显示在当前活动well格标题上(大字)。</summary>
  72. public void SetCurrentInfo(string param, string step)
  73. {
  74. Dispatcher.Invoke(() =>
  75. {
  76. if (param != null) TxtCurParams.Text = param;
  77. if (step != null) TxtCurStep.Text = step;
  78. // 当前well格标题实时显示步骤+参数
  79. if (_activeWell >= 1 && _activeWell <= 16)
  80. {
  81. _cellCaps[_activeWell].Text = $"well {_activeWell} 标定中\n{step}\n{param}";
  82. _cellCaps[_activeWell].Foreground = Brushes.Yellow;
  83. }
  84. });
  85. }
  86. /// <summary>高亮"正在标定"的well格(黄框)。</summary>
  87. public void MarkActive(int well)
  88. {
  89. _activeWell = well;
  90. Dispatcher.Invoke(() =>
  91. {
  92. for (int i = 1; i <= 16; i++)
  93. _cells[i].BorderBrush = new SolidColorBrush(Color.FromRgb(0x55, 0x55, 0x55));
  94. if (well >= 1 && well <= 16)
  95. {
  96. _cells[well].BorderBrush = Brushes.Orange;
  97. _cells[well].BorderThickness = new Thickness(3);
  98. _cellCaps[well].Text = $"well {well} 标定中...";
  99. }
  100. });
  101. }
  102. /// <summary>某well标定完成,显示其照片+结果。</summary>
  103. public void SetWellResult(int well, BitmapSource bmp, string caption, bool ok)
  104. {
  105. Dispatcher.Invoke(() =>
  106. {
  107. if (well < 1 || well > 16) return;
  108. _cellImgs[well].Source = bmp;
  109. _cellCaps[well].Text = caption;
  110. _cellCaps[well].Foreground = ok ? Brushes.LightGreen : Brushes.Orange;
  111. _cells[well].BorderBrush = ok ? Brushes.LimeGreen : Brushes.Orange;
  112. _cells[well].BorderThickness = new Thickness(2);
  113. });
  114. }
  115. private void BtnCalStop_Click(object sender, RoutedEventArgs e)
  116. {
  117. StopRequested?.Invoke();
  118. TxtBigProgress.Text = "已请求停止...";
  119. }
  120. }
  121. }