C# winform中用DrawLine画了好几条线,怎么选取画好的其中一根线然后按del键删除它,谢谢

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication11
    {
        class Line
        {
            public Point P1, P2;
            public Color Color;        public Line(Point P1, Point P2, Color Color)
            {
                this.P1 = P1;
                this.P2 = P2;
                this.Color = Color;
            }
        }    public partial class Form1 : Form
        {
            Bitmap OrgBmp = new Bitmap(500, 500);
            List<Line> Lines = new List<Line>();
            PictureBox PB = new PictureBox();
            Line SelectedLine = null;        public Form1()
            {
                InitializeComponent();            this.Controls.Clear();
                this.AutoSize = true;
                this.KeyPreview = true;
                this.KeyDown += new KeyEventHandler(Form1_KeyDown);            PB.Size = OrgBmp.Size;
                PB.Parent = this;
                PB.Image = OrgBmp;            Lines.Add(new Line(new Point(0, 0), new Point(100, 100), Color.Red));
                Lines.Add(new Line(new Point(20, 30), new Point(450, 200), Color.Green));
                Lines.Add(new Line(new Point(300, 400), new Point(60, 300), Color.Blue));            RefreshImage();            SelectedLine = Lines[1]; // 删除绿线
            }        void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Delete && SelectedLine != null)
                {
                    Lines.Remove(SelectedLine);
                    RefreshImage();
                }
            }        void RefreshImage()
            {
                Bitmap CacheBmp = new Bitmap(OrgBmp);
                Graphics G = Graphics.FromImage(CacheBmp);
                foreach (Line L in Lines)
                    G.DrawLine(new Pen(new SolidBrush(L.Color)), L.P1, L.P2);
                G.Dispose();
                PB.Image = CacheBmp;
            }
        }
    }
      

  2.   

    其实你最主要的问题是pick问题(就是是不是选中的问题)
    就是将先选中的问题
    楼上的已经把选中的给删除了,你可以写一个pick()方法解决
    如果有疑问可以加qq 282231501 注明: 图形
      

  3.   

    blog.csdn.net/dunao
    参照V1.0.0.5的那版本的(VS2008开发)
      

  4.   

    至于选中问题,比较麻烦,如果要鼠标直接可选中,可以试试套用直线的两点式公式,不过公式要变换形式为(y-y1)/(y2-y1)-(x-x1)/(x2-x1)<误差值的形式,不过细节处理起来还是蛮麻烦的