如何擦除面板中绘制的线条或字符串,但不能删除所有线条或字符串 [英] How can I erase a line or string drawn in a panel but not all of them

查看:74
本文介绍了如何擦除面板中绘制的线条或字符串,但不能删除所有线条或字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的项目有问题。这是我的代码的一部分:

Hi
I have a problem in my project.this is a part of my code:

private void draw_notes(int x, int y)
        {
            y = (y / 5) * 5;
            
            note.DrawString("w", f1, Brushes.Black, x - 20, y - 41);
            for (int i = 9; i <= 15; i = i + 2)
            {



这个代码我可以画w但我不能删除其中的一些。

我测试了


whit this code I can draw "w" but I can not erase some of them .
I tested

note.Clear(Color.color);



但是使用此代码整个面板将被删除。



我尝试过:



我用过--- note.Clear(颜色) .color)---但它不起作用,因为我想要清除它们中的一些并非所有


but with this code whole panel will erased.

What I have tried:

I used ---note.Clear(Color.color)--- but it dosen't work because I want to clear some of them not all

推荐答案

基本上,你不能,不能在你的意思是。可以通过在背景上绘制它来擦除面板上的绘制组件,但实际上并没有将其擦除 - 它不会恢复它重叠的任何文本或图像部分,因此它们最终会它们之间存在很大差距。



正确做到这一点的唯一方法就是改变画画的方式(无论如何这都是个好主意 - 你不是即使你创建它们也应该挂起给Graphics对象。

而不是在你想要的时候绘制东西,从对象列表中的Panel.Paint事件处理程序中绘制它们。在更改列表时绘制并在面板上调用Invalidate。这样,您可以从列表中删除一个对象,它将从面板中删除而不会影响其他对象。

Paint事件提供Graphics对象作为PaintEventArgs的一部分,因此您不需要创建你自己的。



它还有一个优势,即绘图是持久的 - 它将被重绘,就像当你的应用程序被最小化并恢复时一样。您当前的方法不允许这样做。
Basically speaking, you can't, not in the way you mean. It's possible to "erase" a drawn component on a panel, by drawing over it with the background, but that doesn't actually erase it - it doesn't "restore" any text or image parts that it overlapped, so they end up with big gaps in them.

The only way to do this properly is to change the way you draw things (which would be a good idea anyway - you aren't supposed to "hang on" to Graphics objects even if you created them).
Instead of drawing things when you want to, draw them all in the Panel.Paint event handler from a list of "objects" to draw, and call Invalidate on the panel when you change the list. That way, you can remove an object from the list and it will be erased from the panel without affecting other objects.
The Paint event supplies the Graphics object as part of the PaintEventArgs so you don't need to create your own.

It also has the advantage that the drawing is "persistant" - it will be redrawn as it was when you app is minimized and restored for example. Your current method does not allow for that.


这篇关于如何擦除面板中绘制的线条或字符串,但不能删除所有线条或字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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