Efficiently draw a grid in Windows Forms

断了今生、忘了曾经 提交于 2020-01-12 07:55:43

问题


I'm writing an implementation of Conway's Game of Life in C#. This is the code I'm using to draw the grid, it's in my panel_Paint event. g is the graphics context.

for (int y = 0; y < numOfCells * cellSize; y += cellSize)
{
     for (int x = 0; x < numOfCells * cellSize; x += cellSize)
     {
          g.DrawLine(p, x, 0, x, y + numOfCells * cellSize);
          g.DrawLine(p, 0, x, y + size * drawnGrid, x);
     }
}

When I run my program, it is unresponsive until it finishes drawing the grid, which takes a few seconds at numOfCells = 100 & cellSize = 10. Removing all the multiplication makes it faster, but not by very much.

Is there a better/more efficient way to draw my grid?

Thanks


回答1:


The problem is that you are drawing the X lines for every Y coordinate. You can simplify first by just rendering the Y lines in one loop and then the X lines in another loop.

Here is a quick example

  for (int y = 0; y < numOfCells; ++y)
  {
    g.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
  }

  for (int x = 0; x < numOfCells; ++x)
  {
    g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
  }

As you progress, you can use double buffering to reduce any flashing etc. Take a look at Control.SetStyle < br/> http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx




回答2:


You don't need nested loops :

for (int i = 0; i < numOfCells; i++)
{
    // Vertical
    g.DrawLine(p, i * cellSize, 0, i * cellSize, numOfCells * cellSize);
    // Horizontal
    g.DrawLine(p, 0, i * cellSize, numOfCells * cellSize, i * cellSize);
}



回答3:


Create a bitmap image of, say, 100x100 pixels with the grid lines and display it in tiles.
Make sure that the seam between tiles doesn't cause a discontinuity in the spaces of the grid.




回答4:


You should take a look at XNA for this. It would probably be more efficent to do this in a rendered window instead of a WinForm.

XNA is the game framework for C#. Find more information at http://creators.xna.com/




回答5:


I know this is late but here...

for (int x = 0; x < 20; x++)
{
    for (int y = 0; y < 20; y++)
    {
        g.drawRectangle(Pens.Black, x * 32, y * 32, 32, 32);
    }
}


来源:https://stackoverflow.com/questions/2753519/efficiently-draw-a-grid-in-windows-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!