一步一步实现自己机器视觉Megauging中工具的制作和运作(简单示范,抓矩形工具进入工具组,二)

我们拖动一下加载了gaugeRect位图的toolbox,如下图:

鼠标移动toolbox事件响应代码:

  PointF point = new PointF(e.X, e.Y);          
            bool status = false;
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                status = true;
            }                  
                mytoolbox.DrawRectFrame(status, point);//调用了drag函数
                this.Cursor = mytoolbox.m_cur;             
                this.Invalidate(false);

防止toolbox被拖出容器panel,出现异常,鼠标弹起时事件响应代码:

 private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 1)
            {      //toolbox出界即重置初始位置       
                if (mytoolbox.m_RoiBase1.rcWin.X < 0 || mytoolbox.m_RoiBase1.rcWin.Y <0
                    || mytoolbox.m_RoiBase1.rcWin.Right > panel1.Width
                    || mytoolbox.m_RoiBase1.rcWin.Bottom > panel1.Height)
                    mytoolbox.m_RoiBase1.ResetRc(new RectangleF(new PointF(0,0),

new SizeF(mytoolbox.m_RoiBase1.rcWin.Width,mytoolbox.m_RoiBase1.rcWin.Height)));
                    panel1.Invalidate();             
            }
        }

那么,我们应该把抓取矩形(gaugeRect)工具添加到toolgroup界面去,如下:

所以我们还得添加一个鼠标双击响应事件,代码如下:

     if (ispointInRect(new Point(e.X, e.Y), rcs[7]))//判断双击是gaugeRect工具
            {
                toolnum = 8;
                m_bAdd_Rc = true;//确认了添加gaugeRect工具
                toolNo.Add(toolnum);

                toolitemRect.m_RoiBase1.ResetRc(new RectangleF(0, 320, 100, 15));//在显示界面画出来
                mgvppview.Invalidate();//toolgroup显示工具界面
                return;
            }

我们看看toolitemRect是什么东东?

   mgToolitem toolitemRect;

出现了一个mgToolitem类,

 public class mgToolitem : ROIsimple
    {
        public bool 已存在图像输入 = false;
        public int 图像来自哪里 = -1;//0代表来自grab,1代表来自cali,还可以扩展202005281806
        public void DrawRectFrame(bool lbtn, PointF point)
        {
            if (lbtn)
            {
                switch (m_eHandle)
                {
                    case E_HANDLES.E_HANDLE_INSIDE:
                        {
                            if(!已存在图像输入)
                            m_RoiBase1.Drag(point);
                        }
                        break;          
                    default:
                        break;
                }
            }
            else
            {              
                m_eHandle = (E_HANDLES)IsPointInRect(point);
            }
            Insteadof((int)m_eHandle);

            return;
        }
        public int IsPointInRect(PointF point)
        {
            m_RoiBase1.IsPointInRect( point);         
        }
    }

原来和mgtoolbox基本一模一样,略有差别,再看看mgtoolbox

  public class mgToolBox : ROIsimple

{.........................}

再对比一下:

框变小了,里边增加了文字显示,仅此而已。我们看看文字是如何显示的,如下代码:

   if (m_bAdd_Rc)//如果添加了这个GaugeRect工具
            {
                toolitemRect.m_RoiBase1.Draw(g, p, "GaugeRect", false);
                float x = toolitemRect.m_RoiBase1.rcWin.Left + toolitemRect.m_RoiBase1.rcWin.Width;
                float y = toolitemRect.m_RoiBase1.rcWin.Y;
                g.DrawString("I", new Font("Arial", 6), Brushes.Red, x - 7, y);//"I"示意input img,"O"示意output img
                g.DrawString("O", new Font("Arial", 6), Brushes.Red, x - 8, y + toolitemRect.m_RoiBase1.rcWin.Height - 8);
            }

是的,我们在m_RoiBase1类增加了一个新函数

   public void Draw(Graphics g, Pen p, string labelstr, bool b_minRc)
        {//工具框中用来显示字符串,选择性屏蔽小矩形
            g.DrawRectangle(p, rcWin.Left, rcWin.Top, rcWin.Width, rcWin.Height);
            if (b_minRc)
            {
                g.DrawRectangle(p, (int)m_Center.X - 5, (int)m_Center.Y - 5, 10, 10);
                g.DrawRectangle(p, (int)rcWin.Left + rcWin.Width - 5, (int)rcWin.Top + rcWin.Height - 5, 10, 10);
            }         
            Font drawfont = new Font("Arial", 9);
            g.DrawString(labelstr, drawfont, Brushes.Brown, rcWin.Left, (float)(rcWin.Top / 2.0 + rcWin.Bottom / 2.0) - 10);
        }

其实,"I"示意input img,"O"示意output img也可以集成到新函数Draw(Graphics g, Pen p, string labelstr, bool b_minRc)中去,这样就更具类的完整性了。mgtoolitm类,当然也可以响应鼠标拖动事件,比mgtoolbox类代码更少,限制更多,当有图像输入后,就像钉子一样扎下去,不能拖动了。

其实我们在重复,重复使人进步,精炼。

未完,待续............

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值