一、新建Windows类库

1、新建类库

在这里插入图片描述

2、类库里面添加类(CogTool)和用户控件(CogToolEditV2)

在这里插入图片描述

2.1、对类(CogTool)的代码编写。需要Cognex.VisionPro 和 Cognex.VisionPro.Core 的引用
2.2、代码理解如下:
(1)先定义,但是这不清楚是什么意思
	[Serializable]
    [Editor(typeof(CogToolEditV2), typeof(Control))]   //CogToolEditV2为用户控件名称
    [CogDefaultToolInputTerminal(0, "InputImage", "InputImage")]
    [CogDefaultToolOutputTerminal(0, "OutputImage", "OutputImage")]
    [System.ComponentModel.DesignerCategory("")]
(2)本类继承Cognex.VisionPro.Implementation.CogToolBase,应该是VisionPro的工具基类,并实现重写该基类的方法
public class CogTool : Cognex.VisionPro.Implementation.CogToolBase
(3)新定义私有变量,_InputImage、_OutputImage和_ImageProcessingType,其中_ImageProcessingType为图像算法类型-默认为0不处理图像
#region 私有变量
        // 输入图像(可以不序列化输入图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.InputImages)]
        private ICogImage _InputImage;
        // 输出图像(可以不序列化输出图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.OutputImages)]
        private ICogImage _OutputImage;
        //------------新增私有变量-------------
        /// <summary>
        /// 图像算法类型-默认为0不处理图像
        /// </summary>
        private int _ImageProcessingType = 0;
        #endregion
(4)构造函数和clone,其中构造函数中有三个无参、一个参数、两个参数,主要是一个参数的构造函数,其内容就是初始化,将输入的图片保持输入图片,输出的图片,如果参数的输入和参数的输出的图片相同则输出参数的输入图片,反之,输出参数的输出图片。

重写clone函数,主要就是实现构造函数(这里是一个参数的构造函数)

	#region 构造函数和clone
        //无参构造函数
        public CogTool()
        {
        }
        public CogTool(CogTool otherTool)
            : base(otherTool)
        {
            // 输入图像
            if (otherTool._InputImage != null)
                _InputImage = otherTool._InputImage;
            // 输出图像
            if (otherTool._OutputImage != null)
            {
                //输入图像和输出图像相同
                if (Object.ReferenceEquals(otherTool._InputImage,
                                           otherTool._OutputImage))
                    _OutputImage = _InputImage;
                //输入图像和输出图像不同
                else
                    _OutputImage = otherTool._OutputImage;
            }
            _ImageProcessingType = otherTool._ImageProcessingType;
        }
        private CogTool(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
        protected override object Clone() { return new CogTool(this); }
        #endregion
(5)状态标志,标志图片输入、图片输出、图片加工(SfImageProcessing)和下一哥的标定。
#region 状态标志
        // 为返回可变数据的每个属性或方法定义一个 64 位状态标志常量。
        // 常量的名称必须是“Sf”,后跟属性或方法的名称。
        // 第一个状态标志应设置为“CogToolBase.SfNextSf”。
        private const long Sf0 = CogToolBase.SfNextSf;//CogToolBase.SfNextSf=32
        public const long SfInputImage = Sf0 << 0;
        public const long SfOutputImage = Sf0 << 1;
        public const long SfImageProcessing = Sf0 << 2;
        protected new const long SfNextSf = Sf0 << 3;
        #endregion
(6)get和set,主要是由InputImage、OutputImage和ImageProcessingType构成的,其中InputImage是获取图片输入,并有值输入时,调用OnChanged更改SfCreateCurrentRecord的状态。OutputImage没有进行set操作,因为本次操作是有不同的输出图片,要进行分类输出。ImageProcessingType,与之后的通过控件转换对应的状态有关,获取值后调用OnChanged更改状态。
#region GetSet
        public ICogImage InputImage
        {
            get { return _InputImage; }
            set
            {
                if (!Object.ReferenceEquals(_InputImage, value))
                {
                    _InputImage = value;
                    // 有值输入时,调用OnChanged更改状态
                    OnChanged(SfInputImage | SfCreateCurrentRecord);
                }
            }
        }

        public ICogImage OutputImage
        {
            get { return _OutputImage; }
        }

        public int ImageProcessingType
        {
            get { return _ImageProcessingType; }
            set
            {
                if (_ImageProcessingType != value)
                {
                    _ImageProcessingType = value;
                    OnChanged(SfImageProcessing);
                }
            }
        }
        #endregion
(7)对InternalRun重写,这个是以编程方式或从编辑控件调用工具时执行的。首先对输入的图片进行检查,在通过之前的_ImageProcessingType的值对图片进行对应的操作。比如值为0时,是输出默认图,将_OutputImage的值等于输入的图片值。值为1时,输出的是灰度图,将_OutputImage的值等于灰度后的图片等等。每次结束后都要将SfCreateLastRunRecord的状态进行更新。最后返回工具成功的状态。
protected override CogToolResultConstants InternalRun(ref string message)
        {
            // 输入图像为空抛异常
            if (_InputImage == null)
                throw new Cognex.VisionPro.Exceptions.CogOperatorNoInputImageException();

            ICogImage cogImage = _InputImage;
            Bitmap bitmap = cogImage.ToBitmap();

            switch (_ImageProcessingType)
            {
                case 0:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    message = "默认";
                    break;
                case 1: //灰度化图像

                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                //设置指定像素的颜色  参数:坐标x,坐标y,颜色
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap);

                        _OutputImage = cogImage;
                        message = "图像灰度化成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 2: //反转

                    try
                    {
                        for (int i = 0; i < bitmap.Width - 1; i++)
                        {
                            for (int j = 0; j < bitmap.Height - 1; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)((255 - color.R + 255 - color.G + 255 - color.B) / 3);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap);

                        _OutputImage = cogImage;
                        message = "图像反转成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 3: //二值化
                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                //获取该点的像素的RGB的颜色
                                Color color = bitmap.GetPixel(i, j);
                                int value = 255 - color.B;
                                Color newColor = value > 125 ? Color.FromArgb(0, 0, 0) : Color.FromArgb(255, 255, 255);
                                bitmap.SetPixel(i, j, newColor);
                            }
                            //将Bitmap转为8位灰度
                            cogImage = new CogImage8Grey(bitmap);

                            _OutputImage = cogImage;
                            message = "图像二值化成功";
                        }
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                default:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    break;
            }


            // 输出图像状态改变
            OnChanged(SfOutputImage | SfCreateLastRunRecord);

            // 返回工具运行成功状态
            return CogToolResultConstants.Accept;
        }
(8)输入图像框的重写,设置要在工具运行前显示在编辑控件中的图像;通常是输入图像。如果工具使用某个区域,通常也会包含该区域。
protected override void InternalCreateCurrentRecord(ICogRecord newRecord,
                                                            int currentRecordEnable)
        {
            // newRecord里添加记录
            newRecord.SubRecords.Add(
              new CogRecord(
                "InputImage",                  // 用于标识记录的键
                typeof(ICogImage),         // 定义类型 
                CogRecordUsageConstants.Input, // 用途输入
                false,                         // 不可以更改记录内容
                _InputImage,                   // 数据
                "InputImage"));                // 注释(用于显示)
        }
(9)输出图像框的重写,设置要在工具运行后显示的图像。通常包括输出图像。如果该工具提供了结果图形,它们也包括在这里。
protected override void InternalCreateLastRunRecord(ICogRecord newRecord,
                                                            int lastRunRecordEnable,
                                                            int lastRunRecordDiagEnable)
        {
            // newRecord里添加记录
            newRecord.SubRecords.Add(
              new CogRecord(
                "OutputImage",                  // 用于标识记录的键
                typeof(ICogImage),          // 定义类型 
                CogRecordUsageConstants.Result, // 用途输出结果
                false,                          // 不可以更改记录内容
                _OutputImage,                   // 数据
                "OutputImage"));                // 注释(用于显示)
        }
(10)整体代码
using Cognex.VisionPro.Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Windows.Forms;
using Cognex.VisionPro;
using System.Drawing;

namespace CogToolDemo
{
    //需要Cognex.VisionPro 和 Cognex.VisionPro.Core 的引用

    [Serializable]
    [Editor(typeof(CogToolEditV2), typeof(Control))]   //CogToolEditV2为用户控件名称
    [CogDefaultToolInputTerminal(0, "InputImage", "InputImage")]
    [CogDefaultToolOutputTerminal(0, "OutputImage", "OutputImage")]
    [System.ComponentModel.DesignerCategory("")]
    public class CogTool : Cognex.VisionPro.Implementation.CogToolBase
    {
        #region 私有变量
        // 输入图像(可以不序列化输入图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.InputImages)]
        private ICogImage _InputImage;
        // 输出图像(可以不序列化输出图像)
        [CogSerializationOptionsAttribute(CogSerializationOptionsConstants.OutputImages)]
        private ICogImage _OutputImage;
        //------------新增私有变量-------------
        /// <summary>
        /// 图像算法类型-默认为0不处理图像
        /// </summary>
        private int _ImageProcessingType = 0;
        #endregion

        #region 构造函数和clone
        //无参构造函数
        public CogTool()
        {
        }
        public CogTool(CogTool otherTool)
            : base(otherTool)
        {
            // 输入图像
            if (otherTool._InputImage != null)
                _InputImage = otherTool._InputImage;
            // 输出图像
            if (otherTool._OutputImage != null)
            {
                //输入图像和输出图像相同
                if (Object.ReferenceEquals(otherTool._InputImage,
                                           otherTool._OutputImage))
                    _OutputImage = _InputImage;
                //输入图像和输出图像不同
                else
                    _OutputImage = otherTool._OutputImage;
            }
            _ImageProcessingType = otherTool._ImageProcessingType;
        }
        private CogTool(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
        protected override object Clone() { return new CogTool(this); }
        #endregion

        #region 状态标志
        // 为返回可变数据的每个属性或方法定义一个 64 位状态标志常量。
        // 常量的名称必须是“Sf”,后跟属性或方法的名称。
        // 第一个状态标志应设置为“CogToolBase.SfNextSf”。
        private const long Sf0 = CogToolBase.SfNextSf;
        public const long SfInputImage = Sf0 << 0;
        public const long SfOutputImage = Sf0 << 1;
        public const long SfImageProcessing = Sf0 << 2;
        protected new const long SfNextSf = Sf0 << 3;  
        #endregion

        #region GetSet
        public ICogImage InputImage
        {
            get { return _InputImage; }
            set
            {
                if (!Object.ReferenceEquals(_InputImage, value))
                {
                    _InputImage = value;
                    // 有值输入时,调用OnChanged更改状态
                    OnChanged(SfInputImage | SfCreateCurrentRecord);
                }
            }
        }
        public ICogImage OutputImage
        {
            get { return _OutputImage; }
        }
        public int ImageProcessingType
        {
            get { return _ImageProcessingType; }
            set
            {
                if (_ImageProcessingType != value)
                {
                    _ImageProcessingType = value;
                    OnChanged(SfImageProcessing);
                }
            }
        }
        #endregion

        #region CogToolBase overrides
        /// <summary>
        /// 以编程方式或从编辑控件调用工具时执行。
        /// </summary>
        /// <param name="message">
        /// 返回的信息(异常、自定义信息)
        /// </param>
        /// <returns></returns>
        protected override CogToolResultConstants InternalRun(ref string message)
        {
            // 输入图像为空抛异常
            if (_InputImage == null)
                throw new Cognex.VisionPro.Exceptions.CogOperatorNoInputImageException();

            ICogImage cogImage = _InputImage;
            Bitmap bitmap = cogImage.ToBitmap();

            switch (_ImageProcessingType)
            {
                case 0:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    message = "默认";
                    break;
                case 1: //灰度化图像

                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                //设置指定像素的颜色  参数:坐标x,坐标y,颜色
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap); 

                        _OutputImage = cogImage;
                        message = "图像灰度化成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" +e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 2: //反转

                    try
                    {
                        for (int i = 0; i < bitmap.Width - 1; i++)
                        {
                            for (int j = 0; j < bitmap.Height - 1; j++)
                            {
                                Color color = bitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                                int gray = (int)((255 - color.R + 255 - color.G + 255 - color.B) / 3);
                                Color newColor = Color.FromArgb(gray, gray, gray);
                                bitmap.SetPixel(i, j, newColor);
                            }
                        }
                        //将Bitmap转为8位灰度
                        cogImage = new CogImage8Grey(bitmap);

                        _OutputImage = cogImage;
                        message = "图像反转成功";
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                case 3: //二值化
                    try
                    {
                        for (int i = 0; i < bitmap.Width; i++)
                        {
                            for (int j = 0; j < bitmap.Height; j++)
                            {
                                //获取该点的像素的RGB的颜色
                                Color color = bitmap.GetPixel(i, j);
                                int value = 255 - color.B;
                                Color newColor = value > 125 ? Color.FromArgb(0, 0, 0) : Color.FromArgb(255, 255, 255);
                                bitmap.SetPixel(i, j, newColor);
                            }
                            //将Bitmap转为8位灰度
                            cogImage = new CogImage8Grey(bitmap);

                            _OutputImage = cogImage;
                            message = "图像二值化成功";
                        }
                    }
                    catch (Exception e)
                    {
                        message = "图像处理失败" + e.Message;
                        // 返回工具运行失败状态
                        return CogToolResultConstants.Error;
                    }
                    break;
                default:
                    _OutputImage = cogImage;  //将输入图片给到输出
                    break;
            }
            // 输出图像状态改变
            OnChanged(SfOutputImage | SfCreateLastRunRecord);
            // 返回工具运行成功状态
            return CogToolResultConstants.Accept;
        }


        /// <summary>
        /// 设置要在工具运行前显示在编辑控件中的图像;通常是输入图像。如果工具使用某个区域,通常也会包含该区域。
        /// </summary>
        /// <param name="newRecord">
        /// A newly-created record to which image sub-records may be added.
        /// </param>
        /// <param name="currentRecordEnable">
        /// An integer bit pattern indicating what images and graphics to add.
        /// </param>
        protected override void InternalCreateCurrentRecord(ICogRecord newRecord,
                                                            int currentRecordEnable)
        {
            // Add a new image record to the top-level "newRecord".
            newRecord.SubRecords.Add(
              new CogRecord(
                "InputImage",                  // Subrecord key for this record,
                typeof(ICogImage),         // Type of its content, 
                CogRecordUsageConstants.Input, // Record represents an input,
                false,                         // Record content is unchanging,
                _InputImage,                   // The record's content,
                "InputImage"));                // Annotation (for display)
        }

        /// <summary>
        /// 设置要在工具运行后显示的图像。通常包括输出图像。如果该工具提供了结果图形,它们也包括在这里。
        /// </summary>
        /// <param name="newRecord">
        /// A newly-created record to which image sub-records may be added.
        /// </param>
        /// <param name="lastRunRecordEnable">
        /// An integer bit pattern indicating what images and graphics to add.
        /// </param>
        /// <param name="lastRunRecordDiagEnable">
        /// An integer bit pattern indicating what 'Diag' images and graphics
        /// to add.  'Diag' images and graphics are specified using a separate
        /// set of bits because they make the tool run significantly slower
        /// (or use significantly more memory).  Users must re-run the tool
        /// to see these items.
        /// </param>
        protected override void InternalCreateLastRunRecord(ICogRecord newRecord,
                                                            int lastRunRecordEnable,
                                                            int lastRunRecordDiagEnable)
        {
            // Add a new image record to the top-level "newRecord".
            newRecord.SubRecords.Add(
              new CogRecord(
                "OutputImage",                  // Subrecord key for this record,
                typeof(ICogImage),          // Type of its content, 
                CogRecordUsageConstants.Result, // Record represents a result,
                false,                          // Record content is unchanging,
                _OutputImage,                   // The record's content,
                "OutputImage"));                // Annotation (for display)
        }
        #endregion
    }
}

3、CogToolEditV2用户控件

3.1先引用Cognex.VisionPro.Controls,并且继承CogToolEditControlBaseV2,它为控件提供了通用框架。
3.2、添加控件(按钮)如图:

在这里插入图片描述

3.3、代码理解:
(1)为编辑控件类创建构造函数,首次按钮是第一个。
 public CogToolEditV2()
          : base(false)
        {
            InitializeComponent();
            tbbElectric.Visible = false;
            //选中默认项
            this.radioButton1.Checked = true;
        }
(2)创建主题属性,包括getter和setter。对象是vision工具的实例,其状态将通过此控件公开
[Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CogTool Subject
        {
            get { return base.GetSubject() as CogTool; }

            set { base.SetSubject(value); }

        }
(3)重写基类的InitializeFromSubject方法。在主题被替换后的第一个线程安全机会调用此方法。控件应使用此方法使用来自新主题的值(重新)初始化自身,并调用SetupSettingsTab方法重新获取控件状态,和控件的交互响应重新更新状态。
protected override void InitializeFromSubject()
        {
            base.InitializeFromSubject();

            SetupSettingsTab();

        }
(4)重写基类的SubjectValuesChanged方法。每当主题引发已更改的事件时,都会调用此方法。此函数始终在GUI线程上执行。在这里,调用SetupSettingsTab方法(用途同上),如所提供的已更改事件参数的状态标志所示。
protected override void SubjectValuesChanged(
          object sender,
          CogChangedEventArgs e)
        {
            base.SubjectValuesChanged(sender, e);

            if ((e.StateFlags & CogTool.SfImageProcessing) != 0)
            {
                SetupSettingsTab();

            }

        }
(5)每当SubjectInUse属性发生更改时,就会调用此方法。这是启用或禁用子控制的好地方,并判断是否有主题属性,若有这将按钮的属性更改。
protected override void SubjectInUseChanged()
        {
            base.SubjectInUseChanged();

            bool bEnabled = false;
            if (Subject != null)  //Subject不为空
                if (!SubjectInUse)   //Subject没有运行
                    bEnabled = true;

            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        }
(6)SetupSettingsTab方法的理解:AssertSafe确保SubjectInUse是false。重新获取控件状态,并更新控件属性。
private void SetupSettingsTab()
        {
            AssertSafe();//确保SubjectInUse是false

            bool bChecked = false;
            if (Subject != null)
                bChecked = true;

            if (bChecked)
            {
                //工具窗体关闭后,重新获取控件状态
                switch (Subject.ImageProcessingType)
                {
                    case 0:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 1:
                        radioButton1.Checked = false;
                        radioButton2.Checked = true;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 2:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = true;
                        radioButton4.Checked = false;
                        break;
                    case 3:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = true;
                        break;
                    default:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                }
            }


            bool bEnabled = false;
            if (Subject != null)
                bEnabled = true;
            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        }
(7)按钮的功能。先判断主题是否为空或是否运行,再进行对ImageProcessingType的赋值,通过Subject.ImageProcessingType对图片进行相对于的操作。如之前的ImageProcessingType为0时,是原图;1时是灰度图等。
private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 2;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;
            Subject.ImageProcessingType = 3;
        }
(8)总代码:
using Cognex.VisionPro;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CogToolDemo
{
    //需要Cognex.VisionPro.Controls的引用
    [System.ComponentModel.ToolboxItem(true)]
    public partial class CogToolEditV2 : CogToolEditControlBaseV2  //继承后用户控件会改变
    {
        //构造函数
        public CogToolEditV2()
          : base(false)
        {
            InitializeComponent();
            tbbElectric.Visible = false;
            //选中默认项
            this.radioButton1.Checked = true;
        }

        //创建Subject属性,包括getter和setter。对象是vision工具的实例,其状态将通过此控件公开
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CogTool Subject
        {
            get { return base.GetSubject() as CogTool; }

            set { base.SetSubject(value); }

        }

        //在Subject被替换后的第一个线程安全机会调用此方法。控件应使用此方法使用来自新Subject的值(重新)初始化自身。
        protected override void InitializeFromSubject()
        {
            base.InitializeFromSubject();

            SetupSettingsTab();

        }

        //当Subject引发已更改的事件时,都会调用此方法。此函数始终在GUI线程上执行。
        //在这里,你可以更新其关联工具属性值可能已更改的任何子控件,如所提供的已更改事件参数的状态标志所示。
        protected override void SubjectValuesChanged(
          object sender,
          CogChangedEventArgs e)
        {
            base.SubjectValuesChanged(sender, e);

            if ((e.StateFlags & CogTool.SfImageProcessing) != 0)
            {
                SetupSettingsTab();

            }

        }


        // 每当SubjectInUse属性发生更改时,就会调用此方法。这是启用或禁用子控制的好地方。
        protected override void SubjectInUseChanged()
        {
            base.SubjectInUseChanged();

            bool bEnabled = false;
            if (Subject != null)  //Subject不为空
                if (!SubjectInUse)   //Subject没有运行
                    bEnabled = true;

            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        } // protected override void SubjectInUseChanged()


        // SetupSettingsTab() 访问 Subject.xxx,
        // 因此只能在安全时间调用(例如,当 SubjectInUse 为 false 时)。
        private void SetupSettingsTab()
        {
            AssertSafe();

            bool bChecked = false;
            if (Subject != null)
                bChecked = true;

            if (bChecked)
            {
                //工具窗体关闭后,重新获取控件状态
                switch (Subject.ImageProcessingType)
                {
                    case 0:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 1:
                        radioButton1.Checked = false;
                        radioButton2.Checked = true;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                    case 2:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = true;
                        radioButton4.Checked = false;
                        break;
                    case 3:
                        radioButton1.Checked = false;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = true;
                        break;
                    default:
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        radioButton3.Checked = false;
                        radioButton4.Checked = false;
                        break;
                }
            }


            bool bEnabled = false;
            if (Subject != null)
                bEnabled = true;
            //控制控件Enabled属性
            radioButton1.Enabled = bEnabled;
            radioButton2.Enabled = bEnabled;
            radioButton3.Enabled = bEnabled;
            radioButton4.Enabled = bEnabled;

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 2;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (Subject == null)
                return;
            if (SubjectInUse)
                return;

            Subject.ImageProcessingType = 3;
        }
    }
}

4、测试结果(这要新建窗口应用,用于测试),和保存生成vtt。

在这里插入图片描述
Form代码如下:

public Form1()
        {
            InitializeComponent();
            cogImageFileEditV21.Subject.Ran += new EventHandler(Subject_Ran);
            //生成VTT
            CogSerializer.SaveObjectToFile(cogTool1, Directory.GetCurrentDirectory() + "\\CogTool.vtt");

        }

        void Subject_Ran(object sender, EventArgs e)
        {
            //将cogImageFile里的图像输入进cogToolEdit里
            cogToolEditV21.Subject.InputImage = cogImageFileEditV21.Subject.OutputImage;
        }

测试结果:

在这里插入图片描述

5、将自定义工具加入到Vision pro中

5.1创建16*16的ico图标由于辨别区分
5.2将ico、dll:放在vision pro文件所在位置

在这里插入图片描述

5.3vtt:放在vision pro文件所在位置下的Templates \ Tools\自定义工具

在这里插入图片描述

5.4将自定义的工具添加输入输出图像的终端

在这里插入图片描述

5.5展示

在这里插入图片描述

更多推荐