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 multiAreaPoint=new List();
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方法。

 

若有不同见解或其他实现方法,欢迎与我交流。

 

转载于:https://www.cnblogs.com/wangzhiying/p/10100847.html

你可能感兴趣的:(c#在pictureBox控件上绘制多个矩形框及删除绘制的矩形框)