话不多说,上关键代码!
关键代码一:
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)