放大缩小时里面的元素保持位置(比列不变)
注册滚轮事件:panel1.MouseWheel += PictureBox1OnMouseWheel;
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
private void PictureBox1OnMouseWheel(object sender, MouseEventArgs e)
{
double step = 1.2; //缩放倍率
if (e.Delta > 0)
{
pictureBox1.Height = (int) (pictureBox1.Height * step);
pictureBox1.Width = (int) (pictureBox1.Width * step);
int px = e.X - pictureBox1.Location.X;
int py = e.Y - pictureBox1.Location.Y;
int pxAdd = (int) (px * (step - 1.0));
int pyAdd = (int) (py * (step - 1.0));
pictureBox1.Location = new Point(pictureBox1.Location.X - pxAdd, pictureBox1.Location.Y - pyAdd);
}
else
{
pictureBox1.Height = (int) (pictureBox1.Height / step);
pictureBox1.Width = (int) (pictureBox1.Width / step);
int px = e.X - pictureBox1.Location.X;
int py = e.Y - pictureBox1.Location.Y;
int pxAdd = (int) (px * (1.0 - 1.0 / step));
int pyAdd = (int) (py * (1.0 - 1.0 / step));
pictureBox1.Location = new Point(pictureBox1.Location.X + pxAdd, pictureBox1.Location.Y + pyAdd);
}
}
如果这张图片为图层,我在这上面绘制了很多东西,可以选中移动得那种,如何缩放得时候保持位置不变
panel里放个picturebox,加上滚轮事件