** 1、 引用中添加halcon动态链接库;工具箱Windows所有窗口中添加halcon动态链接库;
** 2、 添加halcon命名空间 :using HalconDotNet;
** 3、 添加按钮、文本及halcon窗口; HSmartWindowControl对比HWindowControl的优越性在于内部已经实现好了图像的拖拽缩放,拖拽功能是不需要改什么就能实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace day1
{
public partial class Form1 : Form
{
int camnum = 2;//相机个数
int CurrentCam = 1;//当前相机
string path = Application.StartupPath;//获取当前执行的exe路径
string curpath;//当前图片路径
int curindex = 0;//当前图片序列(角标)
List<string> allim;//所有图片的列表
ImageTool mytool;//创建mytool实例
HObject curimage;//创建curimage实例
class ImageTool
{
public HWindow hwin;//halcon窗口对象
public HObject image;//halcon图片对象
public virtual void draw()//绘画泛函
{ }
public virtual void init()//整形泛函
{ }
public virtual void method()//方法泛函
{ }
}
class areatool : ImageTool
{
HTuple r1, c1, r2, c2, r, c, area;//halcon数据元组
HObject roi, imreduce, result;//halcon参数物体
public override void draw()
{ //halcon导出C#代码
HOperatorSet.SetColor(hwin, "red");//设置边框显示颜色
HOperatorSet.GenEmptyObj(out roi);//获取空物体
HOperatorSet.DrawRectangle1(hwin, out r1, out c1, out r2, out c2);//绘制矩形
HOperatorSet.GenRectangle1(out roi, r1, c1, r2, c2);//获取矩形参数并输出
}
public override void init()
{
HOperatorSet.GenEmptyObj(out roi);//获取空物体
HOperatorSet.GenRectangle1(out roi, r1, c1, r2, c2);//获取矩形参数并输出
HOperatorSet.GenEmptyObj(out imreduce);//获取空物体
HOperatorSet.GenEmptyObj(out result);//获取空物体
}
public override void method()
{
HOperatorSet.ReduceDomain(image, roi, out imreduce);//缩小图形区域
HOperatorSet.Threshold(imreduce, out result, 0, 128);//二值化
HOperatorSet.AreaCenter(result, out area, out r, out c);//图形中心
HOperatorSet.DispObj(result, hwin);//显示物体
disp_message(hwin, area, "window", 12, 12, "black", "true");//展示信息
}
//halcon中函数导出
public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
// 创建变量
HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null;
HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null;
HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple();
HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null;
HTuple hv_ShadowColor = null, hv_Exception = new HTuple();
HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
HTuple hv_CurrentColor = new HTuple();
HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
HTuple hv_String_COPY_INP_TMP = hv_String.Clone();
// Initialize local and output iconic variables
//This procedure displays text in a graphics window.
//
//Input parameters:
//WindowHandle: The WindowHandle of the graphics window, where
// the message should be displayed
//String: A tuple of strings containing the text message to be displayed
//CoordSystem: If set to 'window', the text position is given
// with respect to the window coordinate system.
// If set to 'image', image coordinates are used.
// (This may be useful in zoomed images.)
//Row: The row coordinate of the desired text position
// If set to -1, a default value of 12 is used.
//Column: The column coordinate of the desired text position
// If set to -1, a default value of 12 is used.
//Color: defines the color of the text as string.
// If set to [], '' or 'auto' the currently set color is used.
// If a tuple of strings is passed, the colors are used cyclically
// for each new textline.
//Box: If Box[0] is set to 'true', the text is written within an orange box.
// If set to' false', no box is displayed.
// If set to a color string (e.g. 'white', '#FF00CC', etc.),
// the text is written in a box of that color.
// An optional second value for Box (Box[1]) controls if a shadow is displayed:
// 'true' -> display a shadow in a default color
// 'false' -> display no shadow (same as if no second value is given)
// otherwise -> use given string as color string for the shadow color
//
//Prepare window 准备大小合适的窗口
HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,out hv_Column2Part);
HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,out hv_WidthWin, out hv_HeightWin);
HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);//
//
//default settings 默认参数设置
if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Row_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Column_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
{
hv_Color_COPY_INP_TMP = "";
}
//
hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
//
//Estimate extentions of text depending on font size. 根据字体大小估计文本的扩展
HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,out hv_MaxWidth, out hv_MaxHeight);
if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
{
hv_R1 = hv_Row_COPY_INP_TMP.Clone();
hv_C1 = hv_Column_COPY_INP_TMP.Clone();
}
else
{
//Transform image to window coordinates 将图像转换为窗口坐标
hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
}
//
//Display text box depending on text size 根据文本大小显示文本框
hv_UseShadow = 1;
hv_ShadowColor = "gray";
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
{
if (hv_Box_COPY_INP_TMP == null)
hv_Box_COPY_INP_TMP = new HTuple();
hv_Box_COPY_INP_TMP[0] = "#fce9d4";
hv_ShadowColor = "#f28d26";
}
if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
1))) != 0)
{
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
{
//Use default ShadowColor set above 使用默认阴影颜色
}
else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
"false"))) != 0)
{
hv_UseShadow = 0;
}
else
{
hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
//Valid color? 颜色是否无效
try
{
HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(1));
}
// catch (Exception) 捕获异常
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
throw new HalconException(hv_Exception);
}
}
}
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
{
//Valid color?
try
{
HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
}
// catch (Exception)
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
throw new HalconException(hv_Exception);
}
//Calculate box extents 计算范围
hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
hv_Width = new HTuple();
for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
)) - 1); hv_Index = (int)hv_Index + 1)
{
HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
hv_Width = hv_Width.TupleConcat(hv_W);
}
hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
));
hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
hv_R2 = hv_R1 + hv_FrameHeight;
hv_C2 = hv_C1 + hv_FrameWidth;
//Display rectangles 展示矩形
HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
HOperatorSet.SetDraw(hv_WindowHandle, "fill");
//Set shadow color 设置阴影颜色
HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
if ((int)(hv_UseShadow) != 0)
{
HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1);
}
//Set box color 设置盒子颜色
HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
}
//Write text. 写文本
for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
)) - 1); hv_Index = (int)hv_Index + 1)
{
hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
)));
if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
"auto")))) != 0)
{
HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
}
else
{
HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
}
hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index));
}
//Reset changed window settings 重置窗口属性
HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
hv_Column2Part);
return;
}
}
public Form1()
{
InitializeComponent();//系统生成的对于窗体界面的定义方法,用于窗体对象初始化
HOperatorSet.GenEmptyObj(out curimage);//获取当前图像
}
private void Form1_Load(object sender, EventArgs e)
{
curpath = path + @"\CCD1";//初始化选择相机
allim = new List<string>();//实例化所有图片对象
curpath = path + @"\CCD" + CurrentCam.ToString();//效果等同 curpath = path + @"\CCD1";
getallim();//调用函数读取所有图片
}
void getallim()
{
//获取当前文件夹下bmp的数量和路径
allim.Clear();//清空列表
DirectoryInfo Dir = new DirectoryInfo(curpath);//创建指定路径上的实例
foreach (FileInfo FI in Dir.GetFiles())//遍历当前目录的文件列表
{
// 这里写文件格式
if (System.IO.Path.GetExtension(FI.Name) == ".bmp")
{
allim.Add(FI.DirectoryName + @"\" + FI.Name);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (CurrentCam > 1)
{
CurrentCam--;
label1.Text = "相机" + CurrentCam.ToString();
curpath = path + @"\CCD" + CurrentCam.ToString();
getallim();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (CurrentCam < camnum)
{
CurrentCam++;
label1.Text = "相机" + CurrentCam.ToString();
curpath = path + @"\CCD" + CurrentCam.ToString();
getallim();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (curindex - 1 > 0)
{
HOperatorSet.ReadImage(out curimage, allim[curindex - 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex--;
button4.Enabled = true;
}
else if (curindex - 1 == 0)
{
HOperatorSet.ReadImage(out curimage, allim[curindex - 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex--;
button3.Enabled = false;
}
else
{
}
if (mytool != null && curimage != null)
{
mytool.image = curimage;//更新图像
mytool.method();//执行方法
}
}
private void button4_Click(object sender, EventArgs e)
{
if (curindex + 1 < allim.Count - 1)
{
HOperatorSet.ReadImage(out curimage, allim[curindex + 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex++;
button3.Enabled = true;
}
else if (curindex + 1 == allim.Count - 1)
{
HOperatorSet.ReadImage(out curimage, allim[curindex + 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex++;
button4.Enabled = false;
}
else
{
}
if (mytool != null && curimage != null)
{
mytool.image = curimage;//更新图像
mytool.method();//执行方法
}
}
private void button5_Click(object sender, EventArgs e)
{
hWindowControl1.Focus();//光标聚集
//imtool = new tool1();
//imtool.hwin = hWindowControl1.HalconWindow;
//imtool.image = curimage;
//imtool.draw();
//imtool.init();
//imtool.method();
//通过虚函数实现同一接口不同函数的调用,配合foreach实现不同工具的灵活配合应用;
areatool tool = new areatool();
tool.hwin = hWindowControl1.HalconWindow;
tool.image = curimage;
tool.draw();
tool.init();
tool.method();
mytool = tool;
}
}
}
** 1、 在命名空间上右键点击添加新建项,创建用于调用的新类;
//代码同上,只是将tools类放在同一命名空间下的另一个CS文件中; internal class
// 代码同上,imtools类与areatools类建在同一命名空间下
// 通过virtual和override的配合使用,创建父类虚函数,通过子类重写函数,以达到方便同一接口调用不同函数的目的;与foreach配合使用,可灵活调用工具;
** 1、 解决方案的命名空间下右键添加\新建项\用户控件 ;控件上添加 label 、trackbar (更改阈值最大最小范围,防止 halcon 函数报错)、textbox 以实现与程序内参数进行交互的目的 ;控件通过窗口上的 flowLayoutPanel 容器进行显示(更改FlowDirection 控制控件显示方式 ) ;
** 2、 areacon 控件与 areatool 之间相互作用 :(areatool.cs)
** 3、 将 areacon 的 trackbar 事件中 scroll 进行数据关联 ;textbox 中 textchanged 事件与数据关联 :(涉及委托)(areacon.cs)
** 委托 :(可以将方法当参数传递)
//1.声明一个委托
//我在类A中声明一个委托 ChangeHandler();
public delegate void ChangeHandler(); //相当于一个装方法的盒子
//2.声明事件
//用ChangeHandler这个委托声明一个使用ChangHandler签名的事件
public event ChangeHandler changed;
//3.绑定
//我在B中声明了一个A的对象a,并对对其进行绑定事件响应代码,Change是B类里的一个函数
a.changed += new A.ChangeHandler( this .Change);
private void Change()
{
Console.WriteLine( " Some thing have been changed! " );
}
//4.使用
//在A中声明一个方法
public void activation()
{
if (changed != null )
changed();
}
//声明假如changed事件已经绑定至少一个响应,则执行该事件
//详细代码如下:
using System;
class class1
{
static void Main()
{
B b = new B();
b.DoChange();
}
public class A
{
public delegate void ChangeHandler(); //******
public event ChangeHandler changed; //******
public A()
{
}
public void activation()
{
if (changed != null )
changed(); //四个****的内容完成了委托,使A中的changed可以实现B中Change的功能 ;
}
}
public class B
{
private A a;
public B()
{
a = new A(); //******
a.changed += new A.ChangeHandler( this .Change); //******
}
private void Change()
{
Console.WriteLine( " Some thing have been changed! " );
}
public void DoChange()
{
a.activation();
}
}
}
*** 代码过程 :
// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HalconDotNet;
using System.IO;
namespace vision
{
public partial class Form1 : Form
{
int camnum = 2;
int CurrentCam = 1;
string path = Application.StartupPath;
string curpath;
int curindex = 0;
List<string> allim;
HObject curimage;
public Form1()
{
InitializeComponent();
HOperatorSet.GenEmptyObj(out curimage);
}
private void button1_Click(object sender, EventArgs e)
{
if (CurrentCam > 1)
{
CurrentCam--;
label1.Text = "相机" + CurrentCam.ToString();
curpath = path + @"\CCD" + CurrentCam.ToString();
getallim();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (CurrentCam < camnum)
{
CurrentCam++;
label1.Text = "相机" + CurrentCam.ToString();
curpath = path + @"\CCD" + CurrentCam.ToString();
getallim();
}
}
private void Form1_Load(object sender, EventArgs e)
{
curpath = path + @"\CCD1";
allim = new List<string>();
curpath = path + @"\CCD" + CurrentCam.ToString();
getallim();
}
void getallim()
{
//获取当前文件夹下bmp的数量和路径
allim.Clear();
DirectoryInfo Dir = new DirectoryInfo(curpath);
foreach (FileInfo FI in Dir.GetFiles())
{
// 这里写文件格式
if (System.IO.Path.GetExtension(FI.Name) == ".bmp")
{
allim.Add(FI.DirectoryName + @"\" + FI.Name);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (curindex - 1 > 0)
{
HOperatorSet.ReadImage(out curimage, allim[curindex - 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex--;
button4.Enabled = true;
}
else if (curindex - 1 == 0)
{
HOperatorSet.ReadImage(out curimage, allim[curindex - 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex--;
button3.Enabled = false;
}
else
{
}
if (mytool != null && curimage != null)
{
mytool.image = curimage;
mytool.method();
mytool.afterdraw();
}
}
private void button4_Click(object sender, EventArgs e)
{
if (curindex + 1 < allim.Count - 1)
{
HOperatorSet.ReadImage(out curimage, allim[curindex + 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex++;
button3.Enabled = true;
}
else if (curindex + 1 == allim.Count - 1)
{
HOperatorSet.ReadImage(out curimage, allim[curindex + 1]);
HTuple width, height;
HOperatorSet.GetImageSize(curimage, out width, out height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height - 1, width - 1);
hWindowControl1.HalconWindow.DispObj(curimage);
curindex++;
button4.Enabled = false;
}
else
{
}
if (mytool != null && curimage != null)
{
mytool.image = curimage;
mytool.method();
mytool.afterdraw();
}
}
ImageTool mytool;
private void button5_Click(object sender, EventArgs e)
{
hWindowControl1.Focus();
areatool areat = new areatool();
areat.hwin = hWindowControl1.HalconWindow;
areat.image = curimage;
areat.pannel = flowLayoutPanel1;//将FlowLayoutPanel容器与工具1按钮关联
areat.draw();
areat.init();
areat.method();
mytool = areat;
}
}
}
// ImageTool.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
namespace vision
{
class ImageTool
{
public HWindow hwin;
public HObject image;
public virtual void draw()
{ }
public virtual void init()
{ }
public virtual void method()
{ }
public virtual void dispresult()
{ }
public virtual void afterdraw()
{ }
}
}
// areatool.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.Windows.Forms;//控件属于该命名空间,调用相关函数需引用;
namespace vision
{
class areatool:ImageTool
{
public HObject imreduce, result;
HTuple r1, c1, r2, c2, r, c, area;
HObject roi;
public areacon con;//添加areacon控件的实例
public FlowLayoutPanel pannel;//添加FlowLayoutPanel容器的实例
double lowthresh, hightresh, larea, harea;//定义高低阈值和最大最小面积;用于读取
public bool logicresult;//定义检测结果逻辑值
public override void draw()
{
HOperatorSet.SetColor(hwin, "red");
HOperatorSet.GenEmptyObj(out roi);
HOperatorSet.DrawRectangle1(hwin, out r1, out c1, out r2, out c2);
HOperatorSet.GenRectangle1(out roi, r1, c1, r2, c2);
con = new areacon();//新建一个areacon;该句同下一句为B类中的委托定义,以实现B类中实现A类中的某个方法;
con.SetChangeEvent += new areacon.SetChangeHandler(afterdraw);//将afterdraw与SetChangeEvent事件进行绑定;
pannel.Controls.Add(con);//将控件传入FlowLayoutPanel容器中
afterdraw();
}
public override void init()
{
HOperatorSet.GenEmptyObj(out roi);
HOperatorSet.GenRectangle1(out roi, r1, c1, r2, c2);
HOperatorSet.GenEmptyObj(out imreduce);
HOperatorSet.GenEmptyObj(out result);
}
public override void method()
{
HOperatorSet.ReduceDomain(image, roi, out imreduce);
HOperatorSet.Threshold(imreduce, out result, lowthresh, hightresh);
HOperatorSet.AreaCenter(result, out area, out r, out c);
if (area > larea && area < harea)
{
logicresult = true;
}
else
{
logicresult = false;
}
}
public override void dispresult()//根据面积合格与否显示不同颜色
{
if (logicresult)
{
HOperatorSet.SetColor(hwin, "green");
HOperatorSet.DispObj(result, hwin);
}
else
{
HOperatorSet.SetColor(hwin, "red");
HOperatorSet.DispObj(result, hwin);
}
}
public override void afterdraw()//定义相应用户控件刷新的函数
{
HOperatorSet.DispObj(image, hwin);
lowthresh = con.Lowthresh;//通常把个控件的名字写成与bean中定义的各属性名字一样,这样有一个好处就是可提交表单的时候可以实现自动赋值
hightresh = con.Highthresh;
larea = con.Larea;
harea = con.Harea;
method();
con.areaval = area;
dispresult();
}
public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null;
HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null;
HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple();
HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null;
HTuple hv_ShadowColor = null, hv_Exception = new HTuple();
HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
HTuple hv_CurrentColor = new HTuple();
HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
HTuple hv_String_COPY_INP_TMP = hv_String.Clone();
// Initialize local and output iconic variables
//This procedure displays text in a graphics window.
//
//Input parameters:
//WindowHandle: The WindowHandle of the graphics window, where
// the message should be displayed
//String: A tuple of strings containing the text message to be displayed
//CoordSystem: If set to 'window', the text position is given
// with respect to the window coordinate system.
// If set to 'image', image coordinates are used.
// (This may be useful in zoomed images.)
//Row: The row coordinate of the desired text position
// If set to -1, a default value of 12 is used.
//Column: The column coordinate of the desired text position
// If set to -1, a default value of 12 is used.
//Color: defines the color of the text as string.
// If set to [], '' or 'auto' the currently set color is used.
// If a tuple of strings is passed, the colors are used cyclically
// for each new textline.
//Box: If Box[0] is set to 'true', the text is written within an orange box.
// If set to' false', no box is displayed.
// If set to a color string (e.g. 'white', '#FF00CC', etc.),
// the text is written in a box of that color.
// An optional second value for Box (Box[1]) controls if a shadow is displayed:
// 'true' -> display a shadow in a default color
// 'false' -> display no shadow (same as if no second value is given)
// otherwise -> use given string as color string for the shadow color
//
//Prepare window
HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
out hv_Column2Part);
HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
out hv_WidthWin, out hv_HeightWin);
HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
//
//default settings
if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Row_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Column_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
{
hv_Color_COPY_INP_TMP = "";
}
//
hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
//
//Estimate extentions of text depending on font size.
HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
out hv_MaxWidth, out hv_MaxHeight);
if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
{
hv_R1 = hv_Row_COPY_INP_TMP.Clone();
hv_C1 = hv_Column_COPY_INP_TMP.Clone();
}
else
{
//Transform image to window coordinates
hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
}
//
//Display text box depending on text size
hv_UseShadow = 1;
hv_ShadowColor = "gray";
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
{
if (hv_Box_COPY_INP_TMP == null)
hv_Box_COPY_INP_TMP = new HTuple();
hv_Box_COPY_INP_TMP[0] = "#fce9d4";
hv_ShadowColor = "#f28d26";
}
if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
1))) != 0)
{
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
{
//Use default ShadowColor set above
}
else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
"false"))) != 0)
{
hv_UseShadow = 0;
}
else
{
hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
//Valid color?
try
{
HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
1));
}
// catch (Exception)
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
throw new HalconException(hv_Exception);
}
}
}
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
{
//Valid color?
try
{
HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
}
// catch (Exception)
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
throw new HalconException(hv_Exception);
}
//Calculate box extents
hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
hv_Width = new HTuple();
for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
)) - 1); hv_Index = (int)hv_Index + 1)
{
HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
hv_Width = hv_Width.TupleConcat(hv_W);
}
hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
));
hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
hv_R2 = hv_R1 + hv_FrameHeight;
hv_C2 = hv_C1 + hv_FrameWidth;
//Display rectangles
HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
HOperatorSet.SetDraw(hv_WindowHandle, "fill");
//Set shadow color
HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
if ((int)(hv_UseShadow) != 0)
{
HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1);
}
//Set box color
HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
}
//Write text.
for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
)) - 1); hv_Index = (int)hv_Index + 1)
{
hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
)));
if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
"auto")))) != 0)
{
HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
}
else
{
HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
}
hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index));
}
//Reset changed window settings
HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
hv_Column2Part);
return;
}
}
}
// areacon.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vision;
namespace vision
{
public partial class areacon : UserControl
{
public areacon()
{
InitializeComponent();
}
public delegate void SetChangeHandler();//该句同下一句为委托在A类中的写法
public event SetChangeHandler SetChangeEvent;//定义一个事件,发生了变化事件
int lowthresh, highthresh, larea, harea;//
public int Lowthresh//通常把个控件的名字写成与bean中定义的各属性名字一样,这样有一个好处就是可提交表单的时候可以实现自动赋值
{
set { lowthresh = value; trackBar1.Value = value; }//设置相应数值
get { return trackBar1.Value; }//获取相应数值
}
public int Highthresh
{
set { highthresh = value; trackBar2.Value = value; }
get { return trackBar2.Value; }
}
public int Larea
{
set { larea = value; textBox1.Text = value.ToString(); }
get { return Convert.ToInt32(textBox1.Text); }
}
public int Harea
{
set { harea = value; textBox2.Text = value.ToString(); }
get { return Convert.ToInt32(textBox2.Text); }
}
public int areaval
{
set { label6.Text = value.ToString(); }
}
public void trackBar1_Scroll(object sender, EventArgs e)
{
SetChangeEvent();//发生改变
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
SetChangeEvent();//发生改变
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SetChangeEvent();//发生改变
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
SetChangeEvent();//发生改变
}
}
}