Иллюстрирует применение ООП для компактной работы с линиями /////////////////////////////////////////////// using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class frmMain : Form { System.Collections.ArrayList Lns = new System.Collections.ArrayList(); //текущая линия Line curLn = new Line(); public frmMain() { InitializeComponent(); } private void OnMsDown(object sender, MouseEventArgs e) { //создаем класс Line Ln = new Line(); //обозначаем точки Ln.pt1 = curLn.pt1; Ln.pt2 = e.Location; //добавляем в список объектов Lns.Add(Ln); //беспричинная причина curLn.pt1 = e.Location; this.Text = Lns.Count.ToString(); } private void OnMsMove(object sender, MouseEventArgs e) { curLn.pt2 = e.Location; this.MPicture.Invalidate(); } private void LinePaint(object sender, PaintEventArgs e) { e.Graphics.DrawLine(Pens.Black, curLn.pt1, curLn.pt2); for (int i = 0; i < Lns.Count; i++) { e.Graphics.DrawLine(Pens.Black, ((Line)Lns[i]).pt1 , ((Line)Lns[i]).pt2); } } } class Line { private Point _pt1,_pt2; public Point pt1 { get { return _pt1; } set { _pt1 = value; } } public Point pt2 { get { return _pt2; } set { _pt2 = value; } } public Line() { } public Line(Point pt_1, Point pt_2) { pt1 = pt_1; pt2 = pt_2; } } }
|