using System; using System.Drawing; using HalconDotNet; using System.Windows.Forms; public class HImageBox : HWindowControl { private HObject roiObject; private HTuple startX, startY, endX, endY; private bool isROICreating = false; public HImageBox() : base() { roiObject = new HObject(); startX = new HTuple(); startY = new HTuple(); endX = new HTuple(); endY = new HTuple(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (isROICreating) { HOperatorSet.SetDraw(WindowHandle, "margin"); HOperatorSet.SetColor(WindowHandle, "red"); HOperatorSet.DrawLine(WindowHandle, startY, startX, endY, endX); } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button == MouseButtons.Left) { isROICreating = true; startX = e.X; startY = e.Y; } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (isROICreating) { endX = e.X; endY = e.Y; Refresh(); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (e.Button == MouseButtons.Left) { isROICreating = false; HOperatorSet.GenRectangle1(out roiObject, startY, startX, endY, endX); HOperatorSet.DispObj(roiObject, WindowHandle); } } public HObject GetROIObject() { return roiObject; } }怎么将上述的类,调价到form上?
时间: 2023-07-16 15:15:25 浏览: 213
halcondotnet.zip
要将该类添加到 Windows Form 上,可以按照以下步骤进行操作:
1. 打开 Visual Studio,并创建一个新的 Windows Forms 应用程序项目。
2. 打开 Form1.cs 文件,并在设计器中将 HImageBox 控件添加到表单上。
3. 双击 HImageBox 控件,打开 HImageBox.cs 文件。
4. 将 HImageBox 类的代码复制并粘贴到 HImageBox.cs 文件中。
5. 在 Form1.cs 文件中,实例化 HImageBox 控件并将其添加到表单上。可以使用以下代码:
```
public partial class Form1 : Form
{
private HImageBox hImageBox1;
public Form1()
{
InitializeComponent();
hImageBox1 = new HImageBox();
hImageBox1.Dock = DockStyle.Fill;
this.Controls.Add(hImageBox1);
}
}
```
6. 现在,您可以在 HImageBox 控件中使用上述代码中定义的方法和属性。例如,要获取当前选择的 ROI 对象,可以使用以下代码:
```
HObject roiObject = hImageBox1.GetROIObject();
```
阅读全文