Adam Turner

Adam Turner

  • NA
  • 60
  • 0

grid for C#

Jan 5 2009 1:58 AM
I'm trying to draw a grid on a custom user control and have it scrollable. I think my logic works for drawing the grid, but I can't figure out the scrollable side of it, so I can't really tell. I'm trying to code up a map like that found in the old rpg maker series if anyone's curious.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace EmpyrealEngineControls
{
public partial class Map : UserControl
{
public Map() : this(0,0)
{
}
public Map(int _width,int _height)
{
InitializeComponent();
if(_width!=0)
{
width = _width;
}
if(_height!=0)
{
height = _height;
}
FindSizes();
CreateGrid();
}
int width = 40;
int height = 40;
//int viewX = 10;
//int viewY = 10;
public int tileWidth = 32;
public int tileHeight = 32;
List points;
void CustomPaint(object sender, PaintEventArgs e)
{
DrawGrid(sender, e);
}
void FindSizes()
{
AutoScrollMinSize = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
AutoScroll = true;
}
void CreateGrid()
{
/*g.DrawString("Text", new Font("Times New Roman", 20), new SolidBrush(Color.Red), this.DisplayRectangle);*/ points = new List();
int bottomX = this.Left; //so this.Left will not change during execution int bottomY = this.Bottom;
for (int y = 1; y < height; y++)
{
for (int x = 1; x < width; x++)
{
points.Add(new Point(bottomX + (tileWidth * x), bottomY - (tileHeight * y)));
}
}
}

void DrawGrid(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen gridPen = new Pen(Color.Black, 1);
/*g.DrawString("Text", new Font("Times New Roman", 20), new SolidBrush(Color.Red), this.DisplayRectangle);*/ int bottomX = this.Left; //so this.Left will not change during execution int bottomY = this.Bottom;
Point oldPoint = new Point(0, 0);
bool HAVEFIRSTPOINT = false;
foreach (Point P in points)
{
if ((P.X > this.AutoScrollPosition.X + this.ClientRectangle.Width || P.X < this.AutoScrollPosition.X) || (P.Y > this.AutoScrollPosition.Y + this.ClientRectangle.Height || P.Y < this.AutoScrollPosition.Y))
{
continue;
}

if (HAVEFIRSTPOINT)
{
g.DrawLine(gridPen, oldPoint, P);
}
HAVEFIRSTPOINT = true;
oldPoint = P;
}
} } } *Edit -- The formatting got all goofed up and stripped when I posted this. I'm using the browser Chrome if that makes a difference.