在控件中没有直接的鼠标滚轮事件,所以我们要手动添加鼠标滚轮事件。
窗口设置
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace TestPictureBox
{
public partial class Form1 : Form
{
HTuple window = new HTuple();
public Form1()
{
InitializeComponent();
MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
HOperatorSet.OpenWindow(0,0,pictureBox1.Width,pictureBox1.Height,pictureBox1.Handle,"","",out window);
HDevWindowStack.Push(window);
}
private void button1_Click(object sender, EventArgs e)
{
string imagepath = @"D:\Pictures\Camera Roll\01.jpg";
HOperatorSet.ReadImage(out HObject image,imagepath);
HOperatorSet.GetImageSize(image,out HTuple image_width,out HTuple image_height);
HOperatorSet.SetPart(window,0,0,image_height-1,image_width-1);
HOperatorSet.DispObj(image,window);
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)//鼠标滚轮事件
{
//double step = 1.2;//缩放倍率
//if (e.Delta > 0)
//{
// if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 100)
// return;
// pictureBox1.Height = (int)(pictureBox1.Height * step);
// pictureBox1.Width = (int)(pictureBox1.Width * step);
// int px = Cursor.Position.X - pictureBox1.Location.X;
// int py = Cursor.Position.Y - pictureBox1.Location.Y;
// int px_add = (int)(px * (step - 1.0));
// int py_add = (int)(py * (step - 1.0));
// pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
// Application.DoEvents();
//}
//else
//{
// if (pictureBox1.Height <= Screen.PrimaryScreen.Bounds.Height)
// return;
// pictureBox1.Height = (int)(pictureBox1.Height / step);
// pictureBox1.Width = (int)(pictureBox1.Width / step);
// int px = Cursor.Position.X - pictureBox1.Location.X;
// int py = Cursor.Position.Y - pictureBox1.Location.Y;
// int px_add = (int)(px * (1.0 - 1.0 / step));
// int py_add = (int)(py * (1.0 - 1.0 / step));
// pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
// Application.DoEvents();
//}
int x = e.Location.X;
int y = e.Location.Y;
int ow = pictureBox1.Width;
int oh = pictureBox1.Height;
int VX, VY; //因缩放产生的位移矢量
int zoomStep = 20;
if (e.Delta > 0) //放大
{
//第①步
pictureBox1.Width += zoomStep;
pictureBox1.Height += zoomStep;
//第②步
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
//第③步
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
if (e.Delta < 0) //缩小
{
//防止一直缩成负值
if (pictureBox1.Width < pictureBox1.Width / 10)
return;
pictureBox1.Width -= zoomStep;
pictureBox1.Height -= zoomStep;
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
//第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
}
}
}