zoukankan      html  css  js  c++  java
  • PictureBox中的图片实现鼠标滑轮控制缩放和平移

    昨天刚学会了如何实现PictureBox控件中的图像自动缩放和平移,下面我把过程给大家说一下:

    参考了:https://www.cnblogs.com/jinqier/p/3497201.html

    1、首先要拖一个控件,Panel控件作为容器

    2、然后父容器里面拖一个PictureBox控件,设置属性为“”停靠在父容器“”

    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
    pictureBox1.Dock = DockStyle.None;
    

     3‘设置背景色了。Panel 和PictureBox的背景色要设置成一样的,我这里设置的都是为Blue;

    4、为PictureBox添加事件了,下面是代码

     1  public System.Drawing.Point mouseDownPoint;//存储鼠标焦点的全局变量
     2         public bool isSelected = false;
     3         private void Form1_Load(object sender, EventArgs e)
     4         {
     5             pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
     6             pictureBox1.Dock = DockStyle.None;
     7           
     8         }
     9       //这个事件是鼠标滑轮滚动的触发事件,可以在Designer.cs中注册。
          this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
          this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
          this.pictureBox1.MouseEnter += new System.EventHandler(this.pictureBox1_MouseEnter);
          this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
          this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
         
         10 private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) 11 { 12 double scale = 1; 13 if (pictureBox1.Height > 0) 14 { 15 scale = (double)pictureBox1.Width / (double)pictureBox1.Height; 16 } 17 pictureBox1.Width += (int)(e.Delta * scale); 18 pictureBox1.Height += e.Delta; 19 } 20 private void pictureBox1_MouseEnter(object sender, EventArgs e) 21 { 22 pictureBox1.Focus(); 23 pictureBox1.Cursor = Cursors.SizeAll; 24 } 25 26 //在MouseDown处获知鼠标是否按下,并记录下此时的鼠标坐标值; 27 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 28 { 29 if (e.Button == MouseButtons.Left) 30 { 31 mouseDownPoint.X = Cursor.Position.X; //注:全局变量mouseDownPoint前面已定义为Point类型 32 mouseDownPoint.Y = Cursor.Position.Y; 33 isSelected = true; 34 } 35 } 36 37 //在MouseUp处获知鼠标是否松开,终止拖动操作; 38 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 39 { 40 isSelected = false; 41 42 } 43 44 private bool IsMouseInPanel() 45 { 46 if (this.panel_Picture.Left < PointToClient(Cursor.Position).X 47 && PointToClient(Cursor.Position).X < this.panel_Picture.Left 48 + this.panel_Picture.Width && this.panel_Picture.Top 49 < PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y 50 < this.panel_Picture.Top + this.panel_Picture.Height) 51 { 52 return true; 53 } 54 else 55 { 56 return false; 57 } 60 } 61 62 //图片平移,在MouseMove处添加拖动函数操作 63 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 64 { 65 if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件,和鼠标在picturebox的范围内 66 { 67 this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X); 68 this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y); 69 mouseDownPoint.X = Cursor.Position.X; 70 mouseDownPoint.Y = Cursor.Position.Y; 71 } 72 73 }


    此方法实现的原理就是改变PictureBox的大小而改变图像的大小,平移也是。

    这个比较简单的一种方法。

  • 相关阅读:
    timeout in asp.net
    ASP.NET_SessionId vs .ASPXAUTH why do we need both of them?
    Visual paradigm软件介绍
    OJ网站程序员必备
    c++异常详解
    C++STL之双端队列容器
    GPU的线程模型和内存模型
    C++ 中memset 勿要对类使用
    trait与policy模板技术
    C++标准库
  • 原文地址:https://www.cnblogs.com/MaZai/p/10290452.html
Copyright © 2011-2022 走看看