public class lineMover : Form{ public lineMover() { this.DoubleBuffered = true; this.Paint += new PaintEventHandler(lineMover_Paint); this.MouseMove += new MouseEventHandler(lineMover_MouseMove); this.MouseDown += new MouseEventHandler(lineMover_MouseDown); this.MouseUp += new MouseEventHandler(lineMover_MouseUp); this.lines = new List<Graphline>() { new Graphline (10,10,100,200),new Graphline (10,150,120,40),}; } voID lineMover_MouseUp(object sender,MouseEventArgs e) { if (Moving != null) { this.Capture = false; Moving = null; } Refreshlineselection(e.Location); } voID lineMover_MouseDown(object sender,MouseEventArgs e) { Refreshlineselection(e.Location); if (this.Selectedline != null && Moving == null) { this.Capture = true; Moving = new MoveInfo { line = this.Selectedline,StartlinePoint = Selectedline.StartPoint,EndlinePoint = Selectedline.EndPoint,StartMoveMousePoint = e.Location }; } Refreshlineselection(e.Location); } voID lineMover_Paint(object sender,PaintEventArgs e) { e.Graphics.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.High; e.Graphics.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; foreach (var line in lines) { var color = line == Selectedline ? color.Red : color.Black; var pen = new Pen(color,2); e.Graphics.Drawline(pen,line.StartPoint,line.EndPoint); } } voID lineMover_MouseMove(object sender,MouseEventArgs e) { if (Moving != null) { Moving.line.StartPoint = new PointF(Moving.StartlinePoint.X + e.X - Moving.StartMoveMousePoint.X,Moving.StartlinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y); Moving.line.EndPoint = new PointF(Moving.EndlinePoint.X + e.X - Moving.StartMoveMousePoint.X,Moving.EndlinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y); } Refreshlineselection(e.Location); } private voID Refreshlineselection(Point point) { var selectedline = FindlineByPoint(lines,point); if (selectedline != this.Selectedline) { this.Selectedline = selectedline; this.InvalIDate(); } if (Moving != null) this.InvalIDate(); this.Cursor = Moving != null ? Cursors.Hand : Selectedline != null ? Cursors.SizeAll : Cursors.Default; } public List<Graphline> lines = new List<Graphline>(); Graphline Selectedline = null; MoveInfo Moving = null; static Graphline FindlineByPoint(List<Graphline> lines,Point p) { var size = 10; var buffer = new Bitmap(size * 2,size * 2); foreach (var line in lines) { //draw each line on small region around current point p and check pixel in point p using (var g = Graphics.FromImage(buffer)) { g.Clear(color.Black); g.Drawline(new Pen(color.Green,3),line.StartPoint.X - p.X + size,line.StartPoint.Y - p.Y + size,line.EndPoint.X - p.X + size,line.EndPoint.Y - p.Y + size); } if (buffer.GetPixel(size,size).ToArgb() != color.Black.ToArgb()) return line; } return null; } public static voID Main() { Application.Run(new lineMover()); }}public class MoveInfo{ public Graphline line; public PointF StartlinePoint; public PointF EndlinePoint; public Point StartMoveMousePoint;}public class Graphline{ public Graphline(float x1,float y1,float x2,float y2) { this.StartPoint = new PointF(x1,y1); this.EndPoint = new PointF(x2,y2); } public PointF StartPoint; public PointF EndPoint;}总结
以上是内存溢出为你收集整理的c# – Graphic – DrawLine – 绘制线并移动它全部内容,希望文章能够帮你解决c# – Graphic – DrawLine – 绘制线并移动它所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)