简体   繁体   English

如何在c# windows 窗体中删除绘制的圆?

[英]How to delete a drawn circle in c# windows form?

I have drawn a circle in windows form我在windows窗体中画了一个圆圈

Pen pen = new Pen(Color.Black, 3);
Graphics gr = this.CreateGraphics();
gr.DrawEllipse(pen, 5,5,20,20);

How to delete it...怎么删除啊...

You have to clear your Graphic:你必须清除你的图形:

Graphics.Clear();

But all drawn figures will be cleared.但所有绘制的数字都将被清除。 Simply, you will then need to redraw all figures except that circle.简单地说,您将需要重新绘制除该圆圈之外的所有图形。

Also, you can use the Invalidate method:此外,您可以使用 Invalidate 方法:

Control.Invalidate()

It indicates a region to be redrawn inside your Graphics.它表示要在 Graphics 内重绘的区域。 But if you have intersecting figures you will have to redraw the figures you want visible inside the region except the circle.但是,如果您有相交的图形,则必须重新绘制您希望在除圆圈之外的区域内可见的图形。

This can become messy, you may want to check out how to design a control graph or use any graph layout library .这可能会变得混乱,您可能需要查看如何设计控制图或使用任何图形布局

You can invalidate the draw region you want to refresh for example:您可以使要刷新的绘图区域无效,例如:

 this.Invalidate();

on the form...在表格上...

Assuming you're subscribing to the Paint event or overriding the protected OnPaint routine, then you will need to perform something like this:假设您订阅了 Paint 事件或覆盖了受保护的 OnPaint 例程,那么您将需要执行如下操作:

bool paint = false;

protected override void OnPaint(object sender, PaintEventArgs e)
{
  if (paint) 
  {
   // Draw circle.
  }
}

Then when you want to stop painting a circle:然后当你想停止画一个圆圈时:

paint = false;
this.Invalidate(); // Forces a redraw

You can make a figure of same dimensions using the backColor of your control in which you are drawing您可以使用您正在绘制的控件的背景颜色制作相同尺寸的图形

use after your code to clear your figure.在您的代码之后使用以清除您的数字。

Pen p = new Pen(this.BackColor);   
gr.DrawEllipse(p, 5,5,20,20);

If u are using Invalidate() and is not working, make a panel.Refresh() .如果您正在使用Invalidate()并且不起作用,请制作一个panel.Refresh()

That will work on you.这对你有用。

You don't "delete" it per se, there's nothing to delete.你不会“删除”它本身,没有什么可以删除的。 It's a drawing, you draw something else over it or you can call the Graphics.Clear() method .这是一张图,您可以在其上绘制其他内容,或者您​​可以调用Graphics.Clear() 方法

In fact, you can delete your circle and nothing but your circle.事实上,你可以删除你的圈子,除了你的圈子。

Everything you need is something like a screenshot of the "before state" of the area you want to clear, to make a TextureBrush from it.您需要的一切就像您要清除的区域的“之前状态”的屏幕截图,以从中制作TextureBrush You can achieve that step by something like this:您可以通过以下方式实现该步骤:

Bitmap _Background = new Bitmap(this.Width, this.Height);
Graphics.FromImage(_Background).CopyFromScreen(this.Left, this.Top, 0, 0, this.Size);

The first line will give you a bitmap in your windows forms size.第一行将为您提供 Windows 窗体大小的位图。 The second line will save a screenshot of it in the _Background -bitmap.第二行将在_Background -bitmap 中保存它的屏幕截图。

Now you create a TextureBrush out of it:现在你用它创建一个TextureBrush

Brush brsBackground = new TextureBrush(_Background);

The next thing you need are the dimensions of your circle, so you should save them into a variable, if they are not a fix value.接下来您需要的是圆的尺寸,因此如果它们不是固定值,您应该将它们保存到变量中。 When you got them at hand, you can clear the specific area like this:当您手头有它们时,您可以像这样清除特定区域:

Graphics gr = this.CreateGraphics();
gr.FillEllipse(brsBackground, 5, 5, 20, 20); // values referred to your example

Done!完毕!

Even complex figures are able to be deleted by this, like a GraphicsPath for example:即使是复杂的图形也可以通过这个删除,例如GraphicsPath

GraphicsPath gp = new GraphicsPath(); // any kind of GraphicsPath
gr.FillRegion(brsBackground, new Region(gp));

只需使用您想要的属性等制作另一个控件,将可见性设置为 false 并将控件的区域设置为另一个控件,如下所示:

pen.Region = pen2.Region;

It is very simple to delete a drawn circle from c.从 c 中删除一个绘制的圆非常简单。

There is only four steps:-只有四个步骤:-

  1. Open turbo app打开涡轮应用
  2. go to the command where you had drawn the circle转到您绘制圆圈的命令
  3. drag the command拖动命令
  4. click on delete button点击删除按钮

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

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