c#在pictureBox控件上繪制多個矩形框及刪除繪制的矩形框


  在pictureBox上每次只繪制一個矩形框,繪制下一個矩形框時上次繪制的矩形框取消,代碼如鏈接:https://www.cnblogs.com/luxiao/p/5625196.html

 

  在繪制矩形框時要在程序中添加鼠標相應的事件MouseDown、MouseUp、MouseMove、Paint。

  繪制矩形框的代碼如下:

 1 Point start, end;//繪制矩形框的起點和終點
 2 bool blnDraw;//在MouseMove事件中判斷是否繪制矩形框
 3 public struct StartAndEndPoint
 4 {
 5     Point Start;
 6     Point End;
 7 }
 8 //繪制矩形起始點和終止點的集合
 9 List<StartAndEndPoint> multiAreaPoint=new List<StartAndEndPoint>();
10 
11 private void pic_MouseDown(object sender, MouseEventArgs e)
12         {
13             if (e.Button == MouseButtons.Left)
14             {
15                 start = e.Location;
16                 blnDraw = true;
17             }
18             // 點擊鼠標右鍵繪制整個pictureBox區域
19             if (e.Button == MouseButtons.Right)
20             {
21                 start = new Point(0, 0);
22             }
23         }    
24 
25 private void pic_MouseUp(object sender, MouseEventArgs e)
26         {
27             PictureBox pic = sender as PictureBox;
28 
29             if (e.Button == MouseButtons.Left)
30             {
31                 end = e.Location;
32                 blnDraw = false;
33             }
34             if (e.Button == MouseButtons.Right)
35             {
36                 end = new Point(pic.Width, pic.Height);
37             }
38             
39 
40             if (pic.Image != null)
41             {
42                 if (start != end)
43                 {
44                     StartAndEndPoint onepoint = new StartAndEndPoint();
45                     onepoint.start = start;
46                     onepoint.end = end;
47 
48                     if ((!multiAreaPoint.Contains(onepoint)))
49                     {
50                         multiAreaPoint.Add(onepoint);//添加不同區域起始和終止點的結構體數組
51                     }
52                 }
53             }
54 
55             //pic.Refresh();
56         }
57 
58 private void pic_MouseMove(object sender, MouseEventArgs e)
59         {
60             if (blnDraw)
61             {
62                 if (e.Button != MouseButtons.Left)//判斷是否按下左鍵
63                     return;
64                 end = e.Location;
65                 pic.Invalidate();
66             }
67         }

 1 private void pic_Paint(object sender, PaintEventArgs e)
 2         {
 3             PictureBox pic = sender as PictureBox;
 4 
 5             Pen pen = new Pen(Color.Red, 1);
 6             pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//繪制線的格式  7             if (blnDraw)
 8             {
 9                 if (pic.Image != null)
10                 {
11                     //此處是為了在繪制時可以由上向下繪制,也可以由下向上繪制
12                     e.Graphics.DrawRectangle(pen, Math.Min(start.X, end.X), Math.Min(start.Y, end.Y), Math.Abs(start.X - end.X), Math.Abs(start.Y - end.Y));
13                 }
14             }
15 
16             //將之前繪制的矩形框再次繪制一遍
17             foreach (StartAndEndPoint points in multiAreaPoint)
18             {
19                 Point p1 = points.start;
20                 Point p2 = points.end;
21                 e.Graphics.DrawRectangle(pen, Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y), Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
22             }
23 
24             pen.Dispose();
25         } 

 

   多個矩形框繪制相對於繪制單個矩形框而言不能自動取消掉上一次的繪制了,那么如何取消繪制在pictureBox上的矩形框呢?

  繪制多個矩形框的原理是將多個起始點與終止點放到數組中,在調用Paint方法時,將這些點重新繪制一遍,那么取消繪制這些矩形就可以將數組中的點刪除掉,在調用Paint方法就可以刪除這些矩形啦!

  代碼如下:

1    multiAreaPoint.Clear();
2    pic.Refresh();

  pic.Refresh();是為了調用Paint方法。

 

若有不同見解或其他實現方法,歡迎與我交流。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM