使用.NET删除矩形? [英] Delete rectangle using .NET?

查看:156
本文介绍了使用.NET删除矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以删除旧的矩形,我已制定并绘制一个新的矩形?

 私人无效panel1_MouseClick(对象发件人,发送MouseEventArgs E)
{
        图形G = this.panel1.CreateGraphics();
        喷喷=新钢笔(Color.Black,2);

        g.DrawRectangle(笔,100,100,100,200);
        g.dispose();
}
 

解决方案

没有,你不能删除东西是已经被绘制。你可以的覆盖的是别的东西,但图形对象绘制就像是画在现实生活中:一旦油漆干,你只能油漆过用另一种颜色,你不能擦除了。

您可能不应该画的东西在响应鼠标点击,无论是。这是最好的只有的响应油漆赛事借鉴的东西。我会做什么在这种情况下是添加矩形结构向名单上的鼠标点击,然后调用 panel1.Invalidate()来要求它重绘自身。然后在油漆事件的面板中,执行绘图那里。

这将杀死二鸟一石,因为你将能够通过简单地从东西绘制列表中删除它们抹掉的事情。

Can I delete the old rectangle which I have drawn and draw a new rectangle?

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
        Graphics g = this.panel1.CreateGraphics();
        Pen pen = new Pen(Color.Black, 2);

        g.DrawRectangle(pen, 100,100, 100, 200);
        g.dispose();
}

解决方案

No, you cannot "delete" something that's already been drawn. You can overwrite it with something else, but drawing with Graphics objects is like painting in real-life: once the paint is dry, you can only paint over it with another colour, you can't "erase" it.

You probably shouldn't be drawing things in response to a MouseClick, either. It's best to only draw things in response to a Paint event. What I would do in this situation is add a Rectangle structure to a list on the MouseClick and then call panel1.Invalidate() to ask it to redraw itself. Then in the Paint event for the panel, do the drawing there.

This will kill two birds with one stone, because you will be able to "erase" thing by simply removing them from the list of stuff to draw.

这篇关于使用.NET删除矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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