Halcon(C#) ------ HSmartWindowControl 鼠标滚轮缩放图片

话不多说,上关键代码!

关键代码一:

public class HSmartWindowEdit:HSmartWindowControl
    {
        public HSmartWindowEdit()
        {
            base.MouseWheel += HSmartWindowEdit_MouseWheel;
        }
        #region
        public void SetHImage (HObject himage)
        {
            _image = himage;
            HOperatorSet.SetSystem("flush_graphic", "false");
            HOperatorSet.ClearWindow(base.HalconWindow);
            if (_image != null)
            {
                HOperatorSet.GetImageSize(_image, out HTuple iwidth, out HTuple iheight);
                Size TempSize = new Size(iwidth, iheight);
                if (_imageSize != TempSize)
                {
                    _imageSize = TempSize;
                    _imagePart = base.HImagePart;
                    InitImagePart(_image, ref _imagePart);
                    base.HImagePart = _imagePart;
                }
                HOperatorSet.SetSystem("flush_graphic", "true");
                HOperatorSet.DispObj(_image, base.HalconWindow);
            }
        }
        public void ReShow()
        {
            bool a = _image.IsInitialized();
            if (!_image.IsInitialized())
                return;
            HOperatorSet.SetSystem("flush_graphic", "false");
            HOperatorSet.ClearWindow(base.HalconWindow);
            HOperatorSet.SetSystem("flush_graphic", "true");
            HOperatorSet.DispObj(_image, base.HalconWindow);
        }
        public HObject GetHImage ()
        {
            return _image;
        }
        #endregion
        private void InitImagePart(HObject himage, ref Rectangle imagepart)
        {
            if (himage == null)
                return;
            float P;
            float P_Width = (float)base.Width / (float)_imageSize.Width;
            float P_Height = (float)base.Height / (float)_imageSize.Height;
            if (P_Width >= P_Height)
                P = P_Height;
            else
                P = P_Width;
            imagepart.Size = new Size((int)Math.Round(base.Width * (1 / P)), (int)Math.Round(base.Height * (1 / P)));
            imagepart.Location = new Point((int)Math.Round((float)(_imageSize.Width - imagepart.Size.Width) / 2), (int)Math.Round((float)(_imageSize.Height - imagepart.Size.Height) / 2));
        }
        private HObject _image = null;

        private Size _imageSize = new Size();

        private Rectangle _imagePart;

        public Size ImageSize
        {
            get { return _imageSize; }
        }





        private void HSmartWindowEdit_MouseWheel(object sender, MouseEventArgs e)
        {
            Point pt = base.Location;
            MouseEventArgs newe = new MouseEventArgs(e.Button,e.Clicks, e.X - pt.X,e.Y - pt.Y,e.Delta);
            base.HSmartWindowControl_MouseWheel(sender,newe);


        }

    }

解释:建立了一个继承类,对缩放功能进行封装。设置图像时,只需要调用下面的方法即可

public void SetHImage (HObject himage)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值