关于绘制直线,并且可以删除的程序

在很多论坛或者其他地方,有许多网友问,如何实现在指定位置绘制直线,并且可以编辑,修改,删除。源文件下载:http://share.weiyun.com/3226dabeaac2bbfcbf3df0142f251d7a

我是这样设想的。

1、绘制直线,建议不必在onPaint的方法中实现,可以使用一副兼容的bmp,先画好,然后再重新绘制上去,这很重要,否则难以实现。

2、然后,在兼容的BMP中,所画的,都进入List数据中。于是,需要创建一个线的类,方便使用。List<myLine>存放。

3、关于选定直线问题,关注GraphicsPath这个类。表示一系列相互连接的直线和曲线。无法继承此类。(命名空间:System.Drawing.Drawing2D,程序集:System.Drawing(在 system.drawing.dll 中))其中有一个方法IsVisible(point p),可以判断点P是否在它之上。这样,选定后,标示选定项,然后可以通过修改List<myLine>中被选定的直线,例如删除等等,然后重新绘制,这样实现了修改直线。

4、cs代码如下:(一个PictureBox PB,其中单击按下绘制直线,右击选定,按下del键盘删除)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Drawing.Drawing2D;
 10 
 11 namespace WindowsFormsApplication1
 12 {
 13     public partial class Form1 : Form
 14     {
 15 
 16         class Line
 17         {
 18             public Point pfs, pfe;
 19             public Color Color;
 20             public Region areaRegion;
 21             public Pen areaPen;
 22             public GraphicsPath areaPath;
 23             public bool selected = false;
 24             public Line(Point P1, Point P2, Color Color)
 25             {
 26                 try
 27                 {
 28                     this.pfs = P1;
 29                     this.pfe = P2;
 30                     this.Color = Color;
 31                     areaPen = new Pen(Color.Blue, 7);
 32 
 33                     areaPath = new GraphicsPath();
 34                     areaPath.AddLine(pfs, pfe);
 35 
 36                     areaPath.Widen(areaPen);
 37 
 38                     areaRegion = new Region(areaPath);
 39                 }
 40                 catch
 41                 { 
 42                 
 43                 }
 44                 
 45             }
 46          
 47 
 48             
 49                 
 50                 
 51             
 52         }
 53 
 54         Bitmap OrgBmp = null;
 55         List<Line> Lines = new List<Line>();
 56         
 57         Line SelectedLine = null;
 58         public Region chooseReg = null;
 59         public Point oldpoint;
 60 
 61         public Form1()
 62         {
 63             InitializeComponent();
 64             OrgBmp = new Bitmap(PB.Width, PB.Height);
 65             this.AutoSize = true;
 66             this.KeyPreview = true;
 67             this.KeyDown += new KeyEventHandler(Form1_KeyDown);
 68             PB.MouseUp += new MouseEventHandler((o, e) => {
 69                 if (e.Button == MouseButtons.Left)
 70                 {
 71                     Lines.Add(new Line(oldpoint, e.Location, Color.Red));
 72                     RefreshImage();
 73                 }
 74                  });
 75             PB.MouseDown += new MouseEventHandler((o,e) => {
 76                 if (e.Button == MouseButtons.Left)
 77                 {
 78                     oldpoint = new Point(e.X, e.Y);
 79                 }
 80                 
 81             });
 82             PB.MouseClick += new MouseEventHandler((oo, ee) => {
 83 
 84                 if (ee.Button == MouseButtons.Right)
 85                 {
 86                     foreach (Line l in Lines)
 87                     {
 88                         if (l.areaPath.IsVisible(ee.Location))
 89                         {
 90                             l.selected = true;
 91                             l.Color = Color.LightBlue;
 92                             SelectedLine = l;
 93                             RefreshImage();
 94                         }
 95 
 96                         else
 97                         {
 98                             l.Color = Color.Red;
 99                             RefreshImage();
100                         }
101                         Console.WriteLine(ee.Location);
102                         Console.WriteLine(l.areaPath.IsVisible(ee.Location));
103                     }
104 
105                 }
106                 
107             
108             });
109             PB.Size = OrgBmp.Size;
110             PB.Parent = this;
111             PB.Image = OrgBmp;
112             Lines.Add(new Line(new Point(0, 0), new Point(100, 100), Color.Red));
113             Lines.Add(new Line(new Point(20, 30), new Point(450, 200), Color.Red));
114             Lines.Add(new Line(new Point(300, 400), new Point(60, 300), Color.Red));
115             RefreshImage();
116            
117 
118         }
119 
120         void Form1_KeyDown(object sender, KeyEventArgs e)
121         {
122             if (e.KeyCode == Keys.Delete && SelectedLine != null)
123             {
124                 Lines.Remove(SelectedLine);
125                 RefreshImage();
126             }
127         }
128         void RefreshImage()
129         {
130             Bitmap CacheBmp = new Bitmap(OrgBmp);
131             Graphics G = Graphics.FromImage(CacheBmp);
132             foreach (Line L in Lines)
133             G.DrawLine(new Pen(new SolidBrush(L.Color),2), L.pfs, L.pfe);
134             G.Dispose();
135             PB.Image = CacheBmp;
136         } 
138 
139        
140 153     }
154 }

 

转载于:https://www.cnblogs.com/seemenlee/p/5389883.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值