VppWindow.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ImageFile;
  3. using Cognex.VisionPro.Interop;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Windows.Threading;
  18. namespace Plugins.Vpp.Views
  19. {
  20. public delegate void MouseDoubleDelegate(VppWindow window, int VppNumber);
  21. [Serializable]
  22. /// <summary>
  23. /// UserControl1.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class VppWindow
  26. {
  27. public MouseDoubleDelegate MouseDouble { get; set; }
  28. public bool IsOpen = false;
  29. /// <summary>画面框编号(构造时固定;标题栏文案变化不影响编号)</summary>
  30. public int Number { get; }
  31. /// <summary>画面框 ViewModel(标题/状态圆点颜色/圆点显隐,MVVM 绑定)</summary>
  32. private readonly ViewModels.VppWindowViewModel _vm;
  33. public VppWindow(int Number)
  34. {
  35. this.Number = Number;
  36. InitializeComponent();
  37. _vm = new ViewModels.VppWindowViewModel(Number);
  38. Init();
  39. // MVVM:DataContext = 画面框 ViewModel,View 只做绑定与 Cognex 控件交互
  40. DataContext = _vm;
  41. SetStatusDot(VppResultType.Null);
  42. // 窗口最大化/还原、双击放大切换时尺寸变化后,Cognex 显示不会自动重新适配图像,
  43. // 导致图像保持旧尺寸、四周留白——这里强制重新适配一次。
  44. // (Cognex API:CogDisplay.Fit(bool graphicsToo),false=仅按图像适配不含图层)
  45. SizeChanged += (s, e) =>
  46. {
  47. if (e.NewSize.Width <= 0 || e.NewSize.Height <= 0) return;
  48. try { CogDisp.Fit(false); }
  49. catch
  50. {
  51. try { CogDisp.AutoFit = true; } catch { }
  52. }
  53. };
  54. }
  55. /// <summary>设置画面框的用户可见名称(显示在顶部标题栏),如 "内窥镜检测"</summary>
  56. public void SetCaption(string name)
  57. {
  58. Application.Current?.Dispatcher.Invoke(() => _vm.Title = name);
  59. }
  60. /// <summary>设置右上角状态圆点的颜色(根据 OK/NG/None 状态)</summary>
  61. public void SetStatusDot(VppResultType status)
  62. {
  63. Application.Current?.Dispatcher.Invoke(() =>
  64. {
  65. switch (status)
  66. {
  67. case VppResultType.OK:
  68. _vm.StatusBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.LimeGreen);
  69. break;
  70. case VppResultType.NG:
  71. _vm.StatusBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
  72. break;
  73. default:
  74. _vm.StatusBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Gray);
  75. break;
  76. }
  77. });
  78. }
  79. /// <summary>设置状态圆点是否显示(由产品属性里的开关配置驱动,主页面加载时应用)</summary>
  80. public void SetStatusDotVisible(bool visible)
  81. {
  82. Application.Current?.Dispatcher.Invoke(() => _vm.ShowDot = visible);
  83. }
  84. /// <summary>
  85. /// 坐标状态条显隐:网格预览时隐藏(节省空间),双击放大后显示。
  86. /// </summary>
  87. public void SetStatusBarVisible(bool visible)
  88. {
  89. StatusBarGrid.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  90. }
  91. #region 实例
  92. //懒汉式单例
  93. private static VppWindow _instance;
  94. public static VppWindow Instance
  95. {
  96. get
  97. {
  98. if (_instance == null)
  99. _instance = new VppWindow(1);
  100. return _instance;
  101. }
  102. }
  103. #endregion
  104. #region Init
  105. public void Init()
  106. {
  107. try
  108. {
  109. var type = CogDisp.GetType();
  110. var property = type.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  111. if (property != null)
  112. {
  113. property.SetValue(CogDisp, true, null);
  114. }
  115. // 渲染引擎修复:默认硬件加速渲染在向日葵远程/部分驱动环境整块白屏,统一切 GDI 渲染
  116. CogGdiRender.Apply(CogDisp);
  117. WFH.Child = CogDisp;
  118. CogStayusBar.Display = CogDisp;
  119. // CogBar.Display = CogDisp; // CogBar 不存在,已移除或注释
  120. CogDisp.AutoFit = true;
  121. WFH.Child = CogDisp;
  122. CogDisp.HorizontalScrollBar = false;
  123. CogDisp.VerticalScrollBar = false;
  124. // 图像显示区底色改黑色(原 Cognex 默认蓝色底)
  125. CogDisp.BackColor = System.Drawing.Color.Black;
  126. Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle,
  127. new Action(() => IsOpen = true));
  128. SetStatusDot(VppResultType.Null);
  129. }
  130. catch (Exception ex)
  131. {
  132. }
  133. }
  134. /// <summary>
  135. /// 设置流程执行结果状态(保留方法名兼容既有调用方)。
  136. /// 界面改造后不再显示 OK/NG 大标签,状态统一由右上角圆点呈现。
  137. /// </summary>
  138. public void SetLable(VppResultType ResultType)
  139. {
  140. SetStatusDot(ResultType);
  141. }
  142. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  143. {
  144. }
  145. public enum VppResultType
  146. {
  147. OK,
  148. NG,
  149. Null
  150. }
  151. #endregion
  152. #region Method
  153. /// <summary>
  154. /// 显示指定视觉图像
  155. /// </summary>
  156. /// <param name="cogdisplay">显示控件</param>
  157. /// <param name="cogImage">显示图像</param>
  158. public void ShowDisplayByImage(string Path)
  159. {
  160. try
  161. {
  162. CogImageFile file = new CogImageFile();
  163. file.Open(Path, CogImageFileModeConstants.Read);
  164. CogDisp.Image = null;
  165. CogDisp.StaticGraphics.Clear();
  166. CogDisp.InteractiveGraphics.Clear();
  167. CogDisp.Image = file[0];
  168. CogDisp.AutoFit = true;
  169. //CogDisp.BackColor = Color.Black;
  170. CogDisp.Invalidate();
  171. CogDisp.Update();
  172. }
  173. catch (Exception ex)
  174. {
  175. throw new Exception("图像显示失败!", ex);
  176. }
  177. }
  178. /// <summary>
  179. /// ICogRecord图像刷新
  180. /// </summary>
  181. /// <param name="R"></param>
  182. public void RefreshRecordDisplay(Cognex.VisionPro.ICogRecord R)
  183. {
  184. if (R != null)
  185. {
  186. CogDisp?.StaticGraphics?.Clear();
  187. CogDisp?.InteractiveGraphics?.Clear();
  188. CogDisp.Record = R;
  189. CogDisp.BackColor = Color.Black;
  190. // 注:不做 GC.Collect —— 连续运行时每帧强推 GC 会造成明显卡顿
  191. }
  192. }
  193. /// <summary>
  194. /// ICogRecord图像刷新
  195. /// </summary>
  196. /// <param name="R"></param>
  197. public void RefreshImageDisplay(Cognex.VisionPro.ICogImage I)
  198. {
  199. if (I != null)
  200. {
  201. CogDisp?.StaticGraphics?.Clear();
  202. CogDisp?.InteractiveGraphics?.Clear();
  203. CogDisp.Image = I;
  204. CogDisp.BackColor = Color.Black;
  205. // 注:不要在这里 GC.Collect —— 流程连续运行时每帧强推 GC 会造成明显卡顿;
  206. // 旧图像由 CogDisp 释放引用后交给常规 GC 回收即可。
  207. }
  208. }
  209. #endregion
  210. private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  211. {
  212. }
  213. private void CogDisp_DoubleClick(object sender, EventArgs e)
  214. {
  215. MouseDouble?.Invoke(this, Number);
  216. }
  217. }
  218. }