| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using Cognex.VisionPro;
- using Cognex.VisionPro.ImageFile;
- using Cognex.VisionPro.Interop;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace Plugins.Vpp.Views
- {
- public delegate void MouseDoubleDelegate(VppWindow window, int VppNumber);
- [Serializable]
- /// <summary>
- /// UserControl1.xaml 的交互逻辑
- /// </summary>
- public partial class VppWindow
- {
- public MouseDoubleDelegate MouseDouble { get; set; }
- public bool IsOpen = false;
- /// <summary>画面框编号(构造时固定;标题栏文案变化不影响编号)</summary>
- public int Number { get; }
- /// <summary>画面框 ViewModel(标题/状态圆点颜色/圆点显隐,MVVM 绑定)</summary>
- private readonly ViewModels.VppWindowViewModel _vm;
- public VppWindow(int Number)
- {
- this.Number = Number;
- InitializeComponent();
- _vm = new ViewModels.VppWindowViewModel(Number);
- Init();
- // MVVM:DataContext = 画面框 ViewModel,View 只做绑定与 Cognex 控件交互
-
- DataContext = _vm;
- SetStatusDot(VppResultType.Null);
- // 窗口最大化/还原、双击放大切换时尺寸变化后,Cognex 显示不会自动重新适配图像,
- // 导致图像保持旧尺寸、四周留白——这里强制重新适配一次。
- // (Cognex API:CogDisplay.Fit(bool graphicsToo),false=仅按图像适配不含图层)
- SizeChanged += (s, e) =>
- {
- if (e.NewSize.Width <= 0 || e.NewSize.Height <= 0) return;
- try { CogDisp.Fit(false); }
- catch
- {
- try { CogDisp.AutoFit = true; } catch { }
- }
- };
- }
- /// <summary>设置画面框的用户可见名称(显示在顶部标题栏),如 "内窥镜检测"</summary>
- public void SetCaption(string name)
- {
- Application.Current?.Dispatcher.Invoke(() => _vm.Title = name);
- }
- /// <summary>设置右上角状态圆点的颜色(根据 OK/NG/None 状态)</summary>
- public void SetStatusDot(VppResultType status)
- {
- Application.Current?.Dispatcher.Invoke(() =>
- {
- switch (status)
- {
- case VppResultType.OK:
- _vm.StatusBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.LimeGreen);
- break;
- case VppResultType.NG:
- _vm.StatusBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
- break;
- default:
- _vm.StatusBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Gray);
- break;
- }
- });
- }
- /// <summary>设置状态圆点是否显示(由产品属性里的开关配置驱动,主页面加载时应用)</summary>
- public void SetStatusDotVisible(bool visible)
- {
- Application.Current?.Dispatcher.Invoke(() => _vm.ShowDot = visible);
- }
- /// <summary>
- /// 坐标状态条显隐:网格预览时隐藏(节省空间),双击放大后显示。
- /// </summary>
- public void SetStatusBarVisible(bool visible)
- {
- StatusBarGrid.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
- }
- #region 实例
- //懒汉式单例
- private static VppWindow _instance;
- public static VppWindow Instance
- {
- get
- {
- if (_instance == null)
- _instance = new VppWindow(1);
- return _instance;
- }
- }
- #endregion
- #region Init
- public void Init()
- {
- try
- {
- var type = CogDisp.GetType();
- var property = type.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
- if (property != null)
- {
- property.SetValue(CogDisp, true, null);
- }
- // 渲染引擎修复:默认硬件加速渲染在向日葵远程/部分驱动环境整块白屏,统一切 GDI 渲染
- CogGdiRender.Apply(CogDisp);
- WFH.Child = CogDisp;
- CogStayusBar.Display = CogDisp;
- // CogBar.Display = CogDisp; // CogBar 不存在,已移除或注释
- CogDisp.AutoFit = true;
- WFH.Child = CogDisp;
- CogDisp.HorizontalScrollBar = false;
- CogDisp.VerticalScrollBar = false;
- // 图像显示区底色改黑色(原 Cognex 默认蓝色底)
- CogDisp.BackColor = System.Drawing.Color.Black;
- Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle,
- new Action(() => IsOpen = true));
- SetStatusDot(VppResultType.Null);
- }
- catch (Exception ex)
- {
- }
- }
- /// <summary>
- /// 设置流程执行结果状态(保留方法名兼容既有调用方)。
- /// 界面改造后不再显示 OK/NG 大标签,状态统一由右上角圆点呈现。
- /// </summary>
- public void SetLable(VppResultType ResultType)
- {
- SetStatusDot(ResultType);
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- }
- public enum VppResultType
- {
- OK,
- NG,
- Null
- }
- #endregion
- #region Method
- /// <summary>
- /// 显示指定视觉图像
- /// </summary>
- /// <param name="cogdisplay">显示控件</param>
- /// <param name="cogImage">显示图像</param>
- public void ShowDisplayByImage(string Path)
- {
- try
- {
- CogImageFile file = new CogImageFile();
- file.Open(Path, CogImageFileModeConstants.Read);
- CogDisp.Image = null;
- CogDisp.StaticGraphics.Clear();
- CogDisp.InteractiveGraphics.Clear();
- CogDisp.Image = file[0];
- CogDisp.AutoFit = true;
- //CogDisp.BackColor = Color.Black;
- CogDisp.Invalidate();
- CogDisp.Update();
- }
- catch (Exception ex)
- {
- throw new Exception("图像显示失败!", ex);
- }
- }
- /// <summary>
- /// ICogRecord图像刷新
- /// </summary>
- /// <param name="R"></param>
- public void RefreshRecordDisplay(Cognex.VisionPro.ICogRecord R)
- {
- if (R != null)
- {
- CogDisp?.StaticGraphics?.Clear();
- CogDisp?.InteractiveGraphics?.Clear();
- CogDisp.Record = R;
- CogDisp.BackColor = Color.Black;
- // 注:不做 GC.Collect —— 连续运行时每帧强推 GC 会造成明显卡顿
- }
- }
- /// <summary>
- /// ICogRecord图像刷新
- /// </summary>
- /// <param name="R"></param>
- public void RefreshImageDisplay(Cognex.VisionPro.ICogImage I)
- {
- if (I != null)
- {
- CogDisp?.StaticGraphics?.Clear();
- CogDisp?.InteractiveGraphics?.Clear();
- CogDisp.Image = I;
- CogDisp.BackColor = Color.Black;
- // 注:不要在这里 GC.Collect —— 流程连续运行时每帧强推 GC 会造成明显卡顿;
- // 旧图像由 CogDisp 释放引用后交给常规 GC 回收即可。
- }
- }
- #endregion
- private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- }
- private void CogDisp_DoubleClick(object sender, EventArgs e)
- {
- MouseDouble?.Invoke(this, Number);
- }
- }
- }
|