繁体   English   中英

C#WinForm绘图-如何清除和重绘

[英]C# WinForm Drawing - how to clear and redraw

这是我的游戏的屏幕截图。 左边是我的问题,似乎“旧画”仍然存在。 右边是应该的。

http://img682.imageshack.us/img682/1058/38995989.jpg

绘图代码

        Graphics g = e.Graphics;

        for (int i = 1; i < 27; i += 1)
        {
            for (int j = 0; j < 18; j += 1)
            {
                ZPoint zp = zpoints[i, j];

                if (zp != null)
                {
                    g.DrawImage(zp.sprite_index, new Point(zp.x, zp.y));

                    Image arrow;
                    if (zp.sprite_index == spr_green_zpoint)
                    {
                        arrow = spr_green_arrows[zp.image_index];
                    }
                    else if (zp.sprite_index == spr_red_zpoint)
                    {
                        arrow = spr_red_arrows[zp.image_index];
                    }
                    else
                    {
                        arrow = spr_grey_arrows[zp.image_index];
                    }

                    g.DrawImage(arrow, new Point(zp.x - 4, zp.y - 4));
                }
            }
        }

        if (latest_p1 != -1 && latest_p2 != -1)
        {
            ZPoint zp = zpoints[latest_p1, latest_p2];

            if (zp != null)
            {
                g.DrawImage(spr_focus, new Point(zp.x - 6, zp.y - 6));
            }
        }

Control类上有几种处理重绘的方法:

  • Invalidate -此方法将控件的特定区域标记为无效,并将在下一次绘制操作时重画该区域
  • Update -此方法触发任何无效区域的重绘
  • Refresh -此方法使控件的整个表面无效,并立即重绘它。 等效于立即连续调用Invalidate()Update()

如果您的游戏从来没有花时间在操作系统上,那么可能无法很好地吸引人。 就是在这里完成绘图工作。 如果您插入Invalidate()调用和“ Thread.Sleep(0);”, 进入您希望刷新屏幕的代码,这可能会有所帮助。

暂无
暂无

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

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