PictureBox中的圖片實現鼠標滑輪控制縮放和平移
阿新 • • 發佈:2019-01-19
bsp 容器 select drawing oid ane box move style
昨天剛學會了如何實現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的大小而改變圖像的大小,平移也是。
這個比較簡單的一種方法。
PictureBox中的圖片實現鼠標滑輪控制縮放和平移