halcon联合c#编程 窗口控件源码 可在窗口平移、缩放、适应窗口、设置窗口显示字体并调整字体颜色、大小等

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;
using HalconDotNet;
using System.Threading;

namespace 模板控件
{
    public partial class Form1 : Form
    {

        HTuple  ImageWidth, ImageHeight;
        private double RowDown;//鼠标按下时的行坐标
        private double ColDown;//鼠标按下时的列坐标
        HObject ho_image;      //图像变量
        public Form1()
        {
            InitializeComponent();
        }


        //创建一个线程
        Thread new_thread;
        private void button1_Click(object sender, EventArgs e)
        {
            new_thread = new Thread(action);
            new_thread.Start();
        }

        private void action()
        {
            //读入图片
            HOperatorSet.ReadImage(out ho_image, @"F:\picture\cat.jpg");

            HOperatorSet.GetImageSize(ho_image, out ImageWidth, out ImageHeight);
            //将图片适应窗口
            adapt_window(ImageWidth, ImageHeight);
            ho_image.DispObj(hWindowControl1.HalconWindow);


            //在窗口显示文字
            hWindowControl1.HalconWindow.SetColor("blue");//设置窗口显示颜色
            HTuple font = hWindowControl1.HalconWindow.QueryFont();
            hWindowControl1.HalconWindow.SetFont(font.TupleSelect(0) + "-50");//设置文字大小
            hWindowControl1.HalconWindow.SetTposition(20, 20);//设置文字显示位置
            hWindowControl1.HalconWindow.WriteString("你个大帅比!!");//显示文本
        }

        private void button2_Click(object sender, EventArgs e)
        {
            hWindowControl1.HalconWindow.ClearWindow();//清除当前窗口
            new_thread.Abort();//结束进程
        }

        private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
        {

        }


        //调整窗口
        public void adapt_window(int width, int height)
        {
            #region 设置窗体
            double ratioWidth = (1.0) * width / hWindowControl1.Width;
            double ratioHeight = (1.0) * height / hWindowControl1.Height;
            HTuple row1, column1, row2, column2;
            if (ratioWidth >= ratioHeight)
            {
                row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
                column1 = 0;
                row2 = row1 + hWindowControl1.Height * ratioWidth;
                column2 = column1 + hWindowControl1.Width * ratioWidth;
            }
            else
            {
                row1 = 0;
                column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
                row2 = row1 + hWindowControl1.Height * ratioHeight;
                column2 = column1 + hWindowControl1.Width * ratioHeight;
            }
            hWindowControl1.HalconWindow.SetPart(row1, column1, row2, column2);

            #endregion
        }

        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            
            HTuple Zoom, Row, Col, Button;
            HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
            if (e.Delta > 0)
            {
                Zoom = 1.5;
            }
            else
            {
                Zoom = 0.5;
            }
            HOperatorSet.GetMposition(hWindowControl1.HalconWindow, out Row, out Col, out Button);
            HOperatorSet.GetPart(hWindowControl1.HalconWindow, out Row0, out Column0, out Row00, out Column00);
            Ht = Row00 - Row0;
            Wt = Column00 - Column0;
            if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
            {
                r1 = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0)));
                c1 = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0)));
                r2 = r1 + (Ht / Zoom);
                c2 = c1 + (Wt / Zoom);
                HOperatorSet.SetPart(hWindowControl1.HalconWindow, r1, c1, r2, c2);
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.DispObj(ho_image, hWindowControl1.HalconWindow);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            adapt_window(ImageWidth, ImageHeight);
            hWindowControl1.HalconWindow.DispObj(ho_image);
            
        }

        private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
        {
            HTuple Row, Column, Button;
            HOperatorSet.GetMposition(hWindowControl1.HalconWindow, out Row, out Column, out Button);
            RowDown = Row;    //鼠标按下时的行坐标
            ColDown = Column; //鼠标按下时的列坐标
        }

        private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
        {
            HTuple row1, col1, row2, col2, Row, Column, Button;
            HOperatorSet.GetMposition(hWindowControl1.HalconWindow, out Row, out Column, out Button);
            double RowMove = Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
            double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
            HOperatorSet.GetPart(hWindowControl1.HalconWindow, out row1, out col1, out row2, out col2);//得到当前的窗口坐标
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);//这里可能有些不好理解。以左上角原点为参考点
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            if (ImageHeight != null)
            {
                HOperatorSet.DispObj(ho_image, hWindowControl1.HalconWindow);
            }
            else
            {
                MessageBox.Show("请加载一张图片");
            }      
        }
    }
    }

效果图:

 

使用VS2019+halcon21.05联合编程 控件下载地址:

https://download.csdn.net/download/qq_43222917/45621548

  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值