C# 结合Halcon软件视觉窗体滚轮放大/双击放大/滚轮缩小/双击缩小/移动/自适应功能

5 篇文章 1 订阅
1 篇文章 0 订阅

//窗体所需参数
private HTuple mouseDowmRowLT1, mouseDownColLT1, mouseDownRowLT2, mouseDownColLT2;
private HTuple currentRowLT1, currentColLT1, currentRowLT2, currentColLT2;
// 鼠标指针坐标值及对应像素点的灰度值
private HTuple mouseDownPosRowLT, mouseDownPosColLT;
private HTuple currentPosRowLT, currentPosColLT;//, currentGrayValLT;
private Point mouseDownPointLT;// 记录鼠标按下时的点
private bool isMouseDownLT = false; // 鼠标左键是否按下
public static HObject ho_ImageLT; // 当前图像
public static HObject ho_ImageLTLable;//带结果图
public bool MouseFalseLT = false;//鼠标是否按下标志
private bool HalconWindowbigLT = false;//窗体是否放大标志
private int countLT = 0;//点击鼠标计数
Stopwatch timeDoubleLT = new Stopwatch();//双击鼠标计时
//鼠标单击事件
private void hWindowControlLT_HMouseDown(object sender, HMouseEventArgs e)
{
HObject ho_emptyObject;
HTuple isEqualLT, hv_Width, hv_Height, button;
//判断是否有图片
HOperatorSet.GenEmptyObj(out ho_emptyObject);
HOperatorSet.TestEqualObj(ho_emptyObject, ho_ImageLT, out isEqualLT);
if (isEqualLT == true) return;

        //鼠标左键,记录当前位置及显示图像的部分
        if (e.Button == MouseButtons.Left)
        {
            if (countLT == 0) //鼠标第一次按下
            {
                countLT = 1;
                timeDoubleLT.Restart();
                timeDoubleLT.Start();

                HOperatorSet.GetPart(hWindowControlLT.HalconWindow, out mouseDowmRowLT1, out mouseDownColLT1, out mouseDownRowLT2, out mouseDownColLT2);
                HOperatorSet.GetMposition(hWindowControlLT.HalconWindow, out mouseDownPosRowLT, out mouseDownPosColLT, out button);

                mouseDownPointLT.X = Cursor.Position.X;
                mouseDownPointLT.Y = Cursor.Position.Y;

                isMouseDownLT = true;
            }
            else if (countLT == 1)//鼠标第二次按下
            {
                timeDoubleLT.Stop();
                if (timeDoubleLT.ElapsedMilliseconds < 300)//双击鼠标间隔
                {
                    if (HalconWindowbigLT == false)//准备放大窗体
                    {
                        tableLayoutPanel.Controls.Add(hWindowControlLT, 0, 0);
                        this.tableLayoutPanel.SetRowSpan(hWindowControlLT, 3);
                        this.tableLayoutPanel.SetColumnSpan(hWindowControlLT, 3);
                        HalconWindowbigLT = true;
                    }
                    else //准备缩小窗体
                    {
                        tableLayoutPanel.Controls.Add(hWindowControlLT, 0, 0);
                        this.tableLayoutPanel.SetRowSpan(hWindowControlLT, 1);
                        this.tableLayoutPanel.SetColumnSpan(hWindowControlLT, 1);
                        HalconWindowbigLT = false;
                    }
                }
                countLT = 0;
            }               
        }
        else if (e.Button == MouseButtons.Right) //右击--自适应窗体
        {
            HOperatorSet.GetImageSize(ho_ImageLT, out hv_Width, out hv_Height);
            DispImageLT(hWindowControlLT, hv_Width, hv_Height);
            HOperatorSet.DispObj(ho_ImageLT, hWindowControlLT.HalconWindow);
        }
    }
    
    //鼠标移动事件-----图片跟着鼠标移动
    private void hWindowControlLT_HMouseMove(object sender, HMouseEventArgs e)
    {
        HObject ho_emptyObject;
        HTuple isEqualLT, button;
        //判断是否有图片
        HOperatorSet.GenEmptyObj(out  ho_emptyObject);
        HOperatorSet.TestEqualObj(ho_emptyObject, ho_ImageLT, out  isEqualLT);
        if (isEqualLT == true) return;
        //if (ho_ImageLT == null) return;
        if (isMouseDownLT)  //鼠标按下拖动图像移动
        {              HOperatorSet.GetMposition(hWindowControlLT.HalconWindow, out currentPosRowLT, out currentPosColLT, out  button);
            double rowMove = currentPosRowLT - mouseDownPosRowLT;
            double colMove = currentPosColLT - mouseDownPosColLT;

            HOperatorSet.GetPart(hWindowControlLT.HalconWindow, out currentRowLT1, out currentColLT1, out currentRowLT2, out currentColLT2);
            HOperatorSet.SetPart(hWindowControlLT.HalconWindow, currentRowLT1 - rowMove, currentColLT1 - colMove, currentRowLT2 - rowMove, currentColLT2 - colMove);

            HOperatorSet.ClearWindow(hWindowControlLT.HalconWindow);
            HOperatorSet.DispObj(ho_ImageLT, hWindowControlLT.HalconWindow);
        }
        //else  //实时显示像素坐标及灰度值
        //{
        //    getPosAndGrayVal(out currentPosRow, out currentPosCol, out currentGrayVal);
        //    tsslMousePosition.Text = "坐标:(" + currentPosRow.ToString() + "," + currentPosCol.ToString() + ")";
        //    tsslGrayVal.Text = "值:" + currentGrayVal.ToString();
        //}
    }
    
   //鼠标松开事件
    private void hWindowControlLT_HMouseUp(object sender, HMouseEventArgs e)
    {
        isMouseDownLT = false;
    }
    
    // 图像以鼠标指针所指位置为中心缩放
    private void hWindowControlLT_HMouseWheel(object sender, HMouseEventArgs e)
    {
        HObject ho_emptyObject;
        HTuple isEqualLT, button;
        HOperatorSet.GenEmptyObj(out  ho_emptyObject);
        HOperatorSet.TestEqualObj(ho_emptyObject, ho_ImageLT, out  isEqualLT);
        if (isEqualLT == true) return;
        //if (ho_ImageLT == null) return;

        HOperatorSet.GetMposition(hWindowControlLT.HalconWindow, out currentPosRowLT, out currentPosColLT, out  button);
        HOperatorSet.GetPart(hWindowControlLT.HalconWindow, out currentRowLT1, out currentColLT1, out currentRowLT2, out currentColLT2);

        double rowMove1 = (currentRowLT1 - currentPosRowLT) * 0.2;
        double colMove1 = (currentColLT1 - currentPosColLT) * 0.2;
        double rowMove2 = (currentRowLT2 - currentPosRowLT) * 0.2;
        double colMove2 = (currentColLT2 - currentPosColLT) * 0.2;
        if (e.Delta > 0)
        {
            HOperatorSet.SetPart(hWindowControlLT.HalconWindow, currentRowLT1 + rowMove1, currentColLT1 + colMove1, currentRowLT2 + rowMove2, currentColLT2 + colMove2);
        }
        else if (e.Delta < 0)
        {
            HOperatorSet.SetPart(hWindowControlLT.HalconWindow, currentRowLT1 - rowMove1, currentColLT1 - colMove1, currentRowLT2 - rowMove2, currentColLT2 - colMove2);
        }

        HOperatorSet.ClearWindow(hWindowControlLT.HalconWindow);
        HOperatorSet.DispObj(ho_ImageLT, hWindowControlLT.HalconWindow);
    }
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值