如何删除表单上的绘制线? [英] How to delete a drawn line on a form?

查看:175
本文介绍了如何删除表单上的绘制线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实这里有一些类似的问题,但我看不到答案是什么。



例如,我在Windows上绘制了2行代码表单和我想删除其中一个并保留其他,我该怎么做?



this.Invalidate(); Graphics.Clear(); 清除所有表格,我不想要这个,我想删除特定行。你还有其他的解决方案吗?

b
$ b

 图形g; 
Pen p;
位图bmp;
列表< 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); //这是我们的backcolor
}

private void btnLine1_Click(object sender,EventArgs e)
{
Point A = new Point(50,50);
B点=新点(100,50);
p =新笔(Color.Red);
g.DrawLine(p,A,B); //使用任何方法绘制线条
Lines.Add(A); //抓住第一点;添加到列表
Lines.Add(B); //抓住第二点;添加到列表
Refresh(); //刷新绘图到位图。
}

private void btnDrawLine2_Click(object sender,EventArgs e)
{
Point A = new Point(50,60);
B点=新点(100,60);
p =新笔(Color.White);
g.DrawLine(p,A,B); //同上面的逻辑
Lines.Add(A);
Lines.Add(B);
Refresh();
}

private void btnUndo_Click(object sender,EventArgs e)
{
c = new Pen(Color.DeepSkyBlue);
r =新笔(lastColor.ElementAt(lastColor.Count - 2));
尝试
{
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();
}

以下是两行并排:





*请记得处理您的图形对象!


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?

this.Invalidate(); or 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:

Here's 2 lines overlapping:

*Remember to dispose your graphics objects!

这篇关于如何删除表单上的绘制线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆