方法千千萬,我只是其中一筆[通過控制PictureBox來控制圖片,圖片完全施展在控件中]...幾久不做,還真有點陌生!
窗體構造中添加鼠標滾動:
1 /// <summary> 2 /// 窗體構造方法 3 /// </summary> 4 public CandidateForm() 5 { 6 InitializeComponent(); 7 this.MouseWheel += new MouseEventHandler(CandidateForm_MouseWheel); 8 }
滾動監聽:並且保持圖片正中
1 /// <summary> 2 /// 鼠標滾動 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void CandidateForm_MouseWheel(object sender, MouseEventArgs e) 7 { 8 this.pic.Dock = DockStyle.None; 9 this.pic.BorderStyle = BorderStyle.FixedSingle; 10 Size size = this.pic.Size; 11 size.Width += e.Delta; 12 if (size.Width > pic.Image.Width) 13 { 14 pic.Width = pic.Image.Width; 15 pic.Height = pic.Image.Height; 16 } 17 else if (size.Width * pic.Image.Height / pic.Image.Width < pic.Parent.Height - 200) 18 { 19 return; 20 } 21 else 22 { 23 pic.Width = size.Width; 24 pic.Height = size.Width * pic.Image.Height / pic.Image.Width; 25 } 26 pic.Left = (pic.Parent.Width - pic.Width) / 2; 27 pic.Top = (pic.Parent.Height - pic.Height) / 2; 28 }
旋轉:
1 private void 左旋ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 if (basicBt != null) 4 { 5 basicBt = Tools.Rotate(basicBt, 90); 6 height = this.pic.Width; 7 width = this.pic.Height; 8 setBasicPb(basicBt); 9 } 10 } 11 12 private void 右旋ToolStripMenuItem_Click(object sender, EventArgs e) 13 { 14 if (basicBt != null) 15 { 16 basicBt = Tools.Rotate(basicBt, 270); 17 height = this.pic.Width; 18 width = this.pic.Height; 19 setBasicPb(basicBt); 20 } 21 }
拖動直接在鼠標點擊事件中,對PictureBox位置根據鼠標拖動進行處理就好.關鍵代碼:
1 //拖動 2 if (canDrag) 3 { 4 pic.Location = 5 new Point(pic.Left + e.X - p0.X, pic.Top + e.Y - p0.Y); 6 }
如果不是特意做圖片查看,一般還會有對圖片進行處理的功能,及需要用到鼠標拖動啊,針對原圖片的定位等等~~ 則要用到圖片的相對位置定位.
因為使用的是通過改變PictureBox的方法進行處理,所以只需算出比例即可.
1 rate = (double)pic.Width / (double)pic.Image.Width;