自定义控件实现图像的显示和放大缩小,平移,右击菜单选择“自适应窗口”“保存图像”
前面介绍了2个控件组合在一起实现,对PictureBox控件的操作,今天我们自己新建一个控件来自己用。
新建一个Windows窗体解决方案,添加“用户控件”,把PictureBox控件拖到指定的区域,给我们要自己定义的控件改下名字UserControl_Display
界面就完成了,现在我们要自定义的控件添加事件和右击菜单(contextMenuStrip1)控件
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace _自定义控件显示图片 12 { 13 public partial class UserControl_Display : UserControl 14 { 15 16 public System.Drawing.Point mouseDownPoint;//存储鼠标焦点的全局变量 17 public bool isSelected = false; 18 public int width; 19 public int height; 20 21 22 public UserControl_Display() 23 { 24 InitializeComponent(); 25 } 26 27 //这个是显示图片用的 28 public void Display(Image image) 29 { 30 pictureBox1.Image = image; 31 32 width = pictureBox1.Width;//控件的原始大小保存一下,恢复的时候用 33 height = pictureBox1.Height; 36 } 37 38 // 在MouseUp处获知鼠标是否松开,终止拖动操作; 39 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 40 { 41 isSelected = false; 42 } 43 44 private void pictureBox1_MouseEnter(object sender, EventArgs e) 45 { 46 pictureBox1.Focus(); 47 pictureBox1.Cursor = Cursors.SizeAll; 48 } 49 50 //在MouseDown处获知鼠标是否按下,并记录下此时的鼠标坐标值; 51 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 52 { 53 if (e.Button == MouseButtons.Left) 54 { 55 mouseDownPoint.X = Cursor.Position.X; //注:全局变量mouseDownPoint前面已定义为Point类型 56 mouseDownPoint.Y = Cursor.Position.Y; 57 isSelected = true; 58 } 61 } 62 63 //图片平移,在MouseMove处添加拖动函数操作 64 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 65 { 66 if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件,和鼠标在picturebox的范围内 67 { 68 this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X); 69 this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y); 70 mouseDownPoint.X = Cursor.Position.X; 71 mouseDownPoint.Y = Cursor.Position.Y; 72 } 73 } 74 75 private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) 76 { 77 double scale = 1; 78 if (pictureBox1.Height > 0) 79 { 80 scale = (double)pictureBox1.Width / (double)pictureBox1.Height; 81 } 82 pictureBox1.Width += (int)(e.Delta * scale); 83 pictureBox1.Height += e.Delta; 84 } 85 86 87 private bool IsMouseInPanel() 88 { 89 if (this.pictureBox1.Left < PointToClient(Cursor.Position).X 90 && PointToClient(Cursor.Position).X < this.pictureBox1.Left 91 + this.pictureBox1.Width && this.pictureBox1.Top 92 < PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y 93 < this.pictureBox1.Top + this.pictureBox1.Height) 94 { 95 return true; 96 } 97 else 98 { 99 return false; 100 } 101 102 } 103 104 105 /// <summary> 106 /// 右击菜单的保存图像 107 /// </summary> 108 /// <param name="sender"></param> 109 /// <param name="e"></param> 110 private void SaveImage_Click(object sender, EventArgs e) 111 { 112 Image image = this.pictureBox1.Image; 113 string imageName = DateTime.Now.ToString("HH时MM分SS秒") + ".bmp"; 114 image.Save(@imageName); 115 } 116 117 //右击菜单的图像适应控件的大小 118 private void ImageFitPictureBox_Click(object sender, EventArgs e) 119 { 120 pictureBox1.Width = width;//控件的大小恢复 121 pictureBox1.Height = height; 122 123 //PictureBox控件的位置 124 pictureBox1.Left = 1; 125 pictureBox1.Top = 1; 126 } 127 } 128 }
本人从事苦逼的自动化行业,作为一名机器视觉工程师,希望多与同行交流,一些开发和调试经验。
对于新入行的,尽量的帮助你们,也请你们觉得作者的博文不错,可以打赏一下,也算对于作者的支持。
希望大家一起进步。。。