其实有关于这个话题的一些类似的问题,但我看不到答案我在找什么。如何删除表单上的绘制线?
例如我在Windows窗体上绘制了2行并且我想删除其中一个并保留其他,我该怎么做?我想删除特定行。你有其他解决方案吗?
其实有关于这个话题的一些类似的问题,但我看不到答案我在找什么。如何删除表单上的绘制线?
例如我在Windows窗体上绘制了2行并且我想删除其中一个并保留其他,我该怎么做?我想删除特定行。你有其他解决方案吗?
以下操作将以相反的时间顺序删除所有创建的行。
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();
}
下面是两行并排:
下面是两行重叠:
*请记住,处置您的图形对象!
画一条线越过另一条线并观察它也移除底层线的位。 –
@ LasseV.Karlsen好的,试试这个编辑。 –
只需使用视图模型使一个函数负责所有绘图。然后添加新行的按钮不需要绘制任何东西,它们只是更新视图模型。你现在拥有的是大量的重复,这几乎不可能维持。 –
不要使用CreateGraphics! –
唯一的方法是从你绘制的__list行中删除你想要删除的行。始终在绘画事件中一次绘制所有形状。如果你没有这种你想绘制的形状列表,那么你应该有。你需要它。相信我们。忽略所有其他明智的愚蠢建议,从MSDN可怕的介绍中抽取,可惜的是,误导性的比喻是“纸上画出的一条线”。这并不完全相同。事实上它完全不同! – TaW
你可能想研究[这篇文章](http://stackoverflow.com/questions/32408229/select-drawn-figure-within-panel-box/32422295#32422295)。 - 简短的回答:你不需要__have__线条,只需要彩色像素。并且:__您无法取消绘制像素_因此您需要重新创建整个图形。听起来很疯狂和浪费,但它是唯一的方法,而且实际上它的工作速度非常快。 – TaW