private void PaintGrid(Graphics g)
{
RectangleF bounds = g.VisibleClipBounds;
int startCol = (int)(bounds.Left / m_cellWidth);
int endCol = (int)(bounds.Right / m_cellWidth);
for (int col = startCol; col <= endCol; col++)
{
float x = col * m_cellWidth;
g.DrawLine(m_gridPen, x, bounds.Top, x, bounds.Bottom);
}
int startRow = (int)(bounds.Top / m_cellHeight);
int endRow = (int)(bounds.Bottom / m_cellHeight);
for (int row = startRow; row <= endRow; row++)
{
float y = row * m_cellHeight;
g.DrawLine(m_gridPen, bounds.Left, y, bounds.Right, y);
}
Matrix viewportMatrix = g.Transform.Clone();
Point[] points = new Point[] { new Point(0, 0) };
viewportMatrix.Invert();
viewportMatrix.TransformPoints(points);
Point transformedOrigin = points[0];
int viewportX = (int)System.Math.Floor(transformedOrigin.X / m_cellWidth);
int viewportY = (int)System.Math.Floor(transformedOrigin.Y / m_cellHeight);
m_viewport = new Rectangle(viewportX, viewportY, (int)(bounds.Width / m_cellWidth) + 1, (int)(bounds.Height / m_cellHeight) + 1);
}