当我在屏幕上显示大量动态创建的矩形时,如何停止闪烁。 [英] How can I stop flickering when I have a large number of dynamically created rectangles to display on screen.

查看:104
本文介绍了当我在屏幕上显示大量动态创建的矩形时,如何停止闪烁。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量动态创建的矩形在屏幕上显示。通过反复试验,我现在只绘制已更改为位图的矩形和OnPaint事件中的位图,但我仍然会闪烁。关于我可能出错的地方的任何建议。



我正在使用VS 2015 .Net 4.5。我创建了一个新的Windows窗体项目。将窗体的高度和宽度设置为550并添加一个按钮并将其命名为bRunTask。我创建了一个PathAViewModel实例(基于路径查找A star的类lib)50列x 50行。然后订阅OnUpdate事件。 OnUpdate传递一个int,该int对应于状态已更改的任何矩形。我得到状态并用新颜色重绘矩形并且每秒约30次无效。然后在按钮单击事件中调用PathAViewmodel.RunTask。



我包括下面的相关代码将附上两个项目文件。一个用于此项目,一个用于类Lib.if请求。

I have a large number of dynamically created rectangles to display on screen. Through trial and error I now draw only the rectangles that have changed to a bitmap and the bitmap in a OnPaint event, but i still get flickering. Any suggestions on where I might be going wrong.

I'm using VS 2015 .Net 4.5. I create a new Windows Forms project. Set with height and width of the form to 550 and add one button and name it bRunTask. I create an instance of PathAViewModel (class lib based on path find A star) 50 columns x 50 rows. Then subscribe to the OnUpdate event. OnUpdate passes an int that corresponds to any rectangle that state has changed. I get the state and redraw the rectangle with its new color and Invalidate about 30 times a sec. Then make a call to PathAViewmodel.RunTask in the button click event.

I'v included the relevant code below will attached two project files. One for this project and one for the class Lib.if requested.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PathAStarlib;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace wfBitmapDisplay
{
    public partial class Form1 : Form
    {
        PathAViewModel MyView;
        

        Rectangle[] MyRecArry;
        Stopwatch MyWatch = new Stopwatch();

        Graphics gDisplay;
        Bitmap bitmap;
        Graphics gBitmap;
        Brush MyBrush;

        public Form1()
        {
            InitializeComponent();

            bitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);
            gBitmap = Graphics.FromImage(bitmap);
            gBitmap.CompositingMode = CompositingMode.SourceCopy;

            MyView = new PathAViewModel(50,50);
            MyView.OnUpdate += new PathAViewModel.UpdateDisplayHandler(RunOnUpdate);

            this.Paint += Form1_Paint;
            this.DoubleBuffered = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            InitMyComponents();

            gDisplay = this.CreateGraphics();

            gBitmap.DrawRectangles(Pens.Black, MyRecArry);
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            gDisplay.DrawImage(bitmap, new Point(0, 0));
        }
        private void bRunTask_Click(object sender, EventArgs e)
        {
            MyView.RunTask();
        }
        private void RunOnUpdate(int x)
        {
            
            foreach (Node n in MyView.MyModel.lNodes.Where(u => u.Id == x))
            {
                switch (n.ToString())
                {
                    case "1":
                        MyBrush = Brushes.Aqua;
                        break;
                    case "2":
                        MyBrush = Brushes.Blue;
                        break;
                    case "3":
                        MyBrush = Brushes.Red;
                        break;
                    case "4":
                        MyBrush = Brushes.Green;
                        break;
                    case "5":
                        MyBrush = Brushes.Black;
                        break;
                    case "6":
                        MyBrush = Brushes.Yellow;
                        break;
                    default:
                        MyBrush = Brushes.White;
                        break;

                }
                gBitmap.FillRectangle(MyBrush, MyRecArry[n.Id - 1]);
            }
            if (MyWatch.ElapsedMilliseconds % 35 == 0)
                Invalidate();
        }
        
        private void InitMyComponents()
        {
            bRunTask.Top = this.Bottom - 60;
            bRunTask.Text = "Find Path";

            MyRecArry = new Rectangle[MyView.Columns * MyView.Rows];
            int x = 0;
            for (int r = 1; r <= MyView.Rows; r++)
            {
                for (int c = 1; c <= MyView.Columns; c++)
                {
                    MyRecArry[x].Width = ((bitmap.Width - 10) / MyView.Columns);
                    MyRecArry[x].Height = ((bitmap.Height - 50) / MyView.Rows);
                    MyRecArry[x].X = (c - 1) * MyRecArry[x].Width;
                    MyRecArry[x].Y = (r - 1) * MyRecArry[x].Height;
                    x++;
                }

            }

            foreach (Node n in MyView.MyModel.lNodes.Where(u => u.Id == MyView.MyModel.StartId))
                gBitmap.FillRectangle(Brushes.Green, MyRecArry[n.Id - 1]);
            foreach (Node n in MyView.MyModel.lNodes.Where(u => u.Id == MyView.MyModel.FinishId))
                gBitmap.FillRectangle(Brushes.Red, MyRecArry[n.Id - 1]);
            foreach (Node n in MyView.MyModel.lNodes.Where(u => u.IsPassable == false))
                gBitmap.FillRectangle(Brushes.Black, MyRecArry[n.Id - 1]);
        }

       
    }
}

推荐答案

你的油漆处理程序对我来说很奇怪。



Your paint handler looks strange to me.

private void Form1_Paint(object sender, PaintEventArgs e)
{
    gDisplay.DrawImage(bitmap, new Point(0, 0));
}





试试这个:





Try this:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(bitmap, new Point(0, 0));
}


打开表单上的双缓冲。



https://msdn.microsoft.com/en-us/library/3t7htc9c%28v = vs.110%29.aspx [ ^ ]
Turn on double buffering on the form.

https://msdn.microsoft.com/en-us/library/3t7htc9c%28v=vs.110%29.aspx[^]


这篇关于当我在屏幕上显示大量动态创建的矩形时,如何停止闪烁。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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