在更新面板中的箭头位置时轻弹..请给出一些解决方案.. [英] It's flicking during updating the arrow position in panel .. please give some solution ..

查看:56
本文介绍了在更新面板中的箭头位置时轻弹..请给出一些解决方案..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
          //  Boolean log = false();
            this.Invalidate();
            this.Refresh();
            theta = theta - 1;
            if (theta <= 0)
            {
                theta = 180;
            }
            if (theta >= 90)
            {

                x0 = 212;
            }
           
            int x = Convert.ToInt32(x0 - r * Math.Cos(theta * Math.PI / 180));
            int y = Convert.ToInt32(y0 - r * Math.Sin(theta * Math.PI / 180));
            
            p.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
            panel1.CreateGraphics().DrawLine(p, x0, y0, x, y);
            ab=ab+2;
           
            
            
        }

推荐答案

1)是的,它会。

2)不要那样做。

3)真的不这样做或闪烁是你问题中最少的......



首先重要的一点:如果你创建一个图形上下文,你负责处理它。如果你不这样做,那么一个非常有价值的系统资源就是捆绑,你的系统将耗尽句柄 - 并且能够绘制任何结果 - 很久之前,垃圾收集器会被激活以便为你删除它们。

所以每次这样做:

1) Yes, it will.
2) Don't do it like that.
3) Really don't do it like that or flickering will be the least of your problems...

The important one first: If you create a graphics context, you are responsible for Disposing of it. If you don't, then a very valuable system resource is "Tied up" and your system will run out of handles - and the ability to draw anything as a result - long before the Garbage Collector is fired up to remove them for you.
So each time you do this:
panel1.CreateGraphics().DrawLine(p, x0, y0, x, y);



你稍微损坏你的系统。

这样做,并且它更好:


you are damaging your system slightly.
Do this, and it's better:

using (Graphics g = panel1.CreateGraphics())
   {
   g.DrawLine(p, x0, y0, x, y);
   }

这个问题是避免的。



其他两个是相关的:你使表格无效 - 这会强制重新粉刷面板 - 然后绘制到面板上。因为这一切都涉及清除面板的当前内容,所以你会闪烁。



Don; t这样做:相反,处理Panel.Paint事件,并绘制你的箭头那里。 Invalidate将强制重绘,因此如果您最小化并恢复应用程序,则不会丢失箭头 - 恢复将自动使表单无效,因此将重新绘制面板。在Paint事件中,你将使用Graphics上下文,所以你不需要创建或处理任何东西!



它仍然会闪烁!但这很容易解决:将Panel.DoubleBuffered属性设置为true,闪烁应该停止。

And that problem is averted.

The other two are related: you invalidate the form - which forces a repaint on the panel - and then draw onto the panel. Because this all involves clearing the panel's current content, you get flicker.

Don;t do it like that: instead, handle the Panel.Paint event, and draw your arrow there. The Invalidate will force it to redraw, so you won't lose the arrow if you minimise and restore your app - the restore will automatically Invalidate the form, so the panel will be redrawn. And in the Paint event, you are handed the Graphics context to use, so you don't need to create or dispose anything!

It'll still flicker though! But that's easy to fix: set the Panel.DoubleBuffered property to true, and the flicker should stop.


这篇关于在更新面板中的箭头位置时轻弹..请给出一些解决方案..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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