简体   繁体   English

如何删除表格上的画线?

[英]How to delete a drawn line on a form?

Actually there are some similar questions about this topic but I couldn't see the answer what I am looking for.实际上有一些关于这个主题的类似问题,但我看不到我正在寻找的答案。

For example I have drawn 2 lines on windows form and I want to delete one of them and keep the other , how can I do that?例如,我在 Windows 窗体上画了 2 条线,我想删除其中一条并保留另一条,我该怎么做?

this.Invalidate(); or Graphics.Clear();Graphics.Clear(); clear all the form, I don't want this, I want to delete specific line .清除所有表格,我不想要这个,我想删除特定行 Do you have any other solutions?你有其他解决方案吗?

The following will delete the all created lines in reverse chronological sequence.以下将按逆时间顺序删除所有创建的行。

    Graphics g;
    Pen p;
    Bitmap bmp;
    List<Point> Lines = new List<Point>();

    private void Form2_Load(object sender, EventArgs e)
    {
        bmp = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        BackgroundImage = bmp;
        g = Graphics.FromImage(BackgroundImage);
        g.Clear(Color.DeepSkyBlue); //This is our backcolor
    }

    private void btnLine1_Click(object sender, EventArgs e)
    {
        Point A = new Point(50, 50);
        Point B = new Point(100, 50);
        p = new Pen(Color.Red);
        g.DrawLine(p, A, B); //Use whatever method to draw your line
        Lines.Add(A); //Grab the first point; add to list
        Lines.Add(B); //Grab the second point; add to list
        Refresh(); //Refresh drawing to bitmap.
    }

    private void btnDrawLine2_Click(object sender, EventArgs e)
    {
        Point A = new Point(50, 60);
        Point B = new Point(100, 60);
        p = new Pen(Color.White);
        g.DrawLine(p, A, B); //Same logic as above
        Lines.Add(A);
        Lines.Add(B);
        Refresh();
    }

    private void btnUndo_Click(object sender, EventArgs e)
    {
        c = new Pen(Color.DeepSkyBlue);
        r = new Pen(lastColor.ElementAt(lastColor.Count - 2));
        try
        {
            g.DrawLine(c, Lines.ElementAt(Lines.Count - 2), Lines.ElementAt(Lines.Count - 1));
            Lines.RemoveAt(Lines.Count - 2);
            Lines.RemoveAt(Lines.Count - 1);
            for (int i = Lines.Count; i > 0; i--)
            {
            g.DrawLine(r, Lines.ElementAt(Lines.Count - 2), Lines.ElementAt(Lines.Count - 1));
            }
        }
        catch { }
        Refresh();
    }

Here's 2 lines side by side:这是并排的 2 行:

结果1

Here's 2 lines overlapping:这里有 2 行重叠:

结果2

*Remember to dispose your graphics objects! *记得处理你的图形对象!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM