Image 鼠标拖拽与鼠标中键的缩放()
作者:互联网
一、Image在窗体上拖拽,势必会用到鼠标的三个事件(MouseDown,MouseUp,MouseMove),以左键为例,PictureBox为载体
Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置 bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下) private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseDownPoint.X = Cursor.Position.X; mouseDownPoint.Y = Cursor.Position.Y; isMove = true; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { pictureBox1.Dock = DockStyle.None; if (e.Button == MouseButtons.Left) { isMove = false; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (isMove) { int x, y; int moveX, moveY; moveX = Cursor.Position.X - mouseDownPoint.X; moveY = Cursor.Position.Y - mouseDownPoint.Y; x = pictureBox1.Location.X + moveX; y = pictureBox1.Location.Y + moveY; pictureBox1.Location = new Point(x, y); mouseDownPoint.X = Cursor.Position.X; mouseDownPoint.Y = Cursor.Position.Y; } }View Code
二、(1)鼠标滚轮------Image缩放(实现思路,靠设置PictureBox里SizeModel属性为Zoom,改变PictureBox大小进行放缩,大图片有卡顿,慢等问题,后续优化)
Bitmap myBmp; int zoomStep = 40; //缩放步长 //窗体加载事件 private void Form_Load(object sender, EventArgs e) { string filename = "图片路径+文件名(包括扩展名)" myBmp = new Bitmap(filename);//多种重载,基于自己的方式,将Image赋值与画布 pictureBox1.Image = myBmp; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式 } //PictureBox控件绑定上鼠标中键事件 private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { int x = e.Location.X; int y = e.Location.Y; int ow = pictureBox1.Width; int oh = pictureBox1.Height; int VX, VY; 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 < myBmp.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); }View Code
标签:鼠标,缩放,int,Image,Height,Width,pictureBox1,Location 来源: https://www.cnblogs.com/Bull-Rush/p/12061777.html