带坐标的平铺网格
Tile Grid with Coordinates
我正在尝试创建一个可以用宽度或高度指定的网格(即 10 个框宽 x 20 个高)。我已经创建了一个创建网格的脚本,但我想以一种可以创建不同宽度和高度的方式来创建网格。
它目前创建一个宽度与高度相等且不显示坐标的网格。
int numOfCells = 5;
int cellSize = 80;
Pen p = new Pen(Color.Black);
for (int y = 0; y < numOfCells; y++)
{
graphics.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
for (int x = 0; x < numOfCells; x++)
{
graphics.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
}
}
结果如下所示:
我想要实现的是像这样的网格,它可以有不同的宽度和高度,并在方框内显示坐标:
可自定义/可扩展的示例 Class 使用传递给其构造函数的参数生成网格。
class 还包含用于在设备上下文中绘制网格的方法(PaintEventArgs
参数的 Graphics
对象由 Paint
事件/OnPaint
控件的方法)。
作为基本实现,它允许指定在其单元格内绘制的文本的字体和颜色。
您可以添加更多属性,例如定义网格线的颜色和厚度。
▶ 在图形示例中,您可以看到三个 TextBox 控件(txtRows
、txtColumns
和 txtCellSize
)用于输入行数和列数以及单元格大小。
当按下 Button (btnDrawGrid
) 时,如果正确解析了 TextBoxes 的内容,则会生成一个新的 Grid 对象和用于呈现 Grid 的控件(这里是一个名为 [ 的 PictureBox 控件) =20=]) 被重新绘制,调用它的 Invalidate()
方法,引发它的 Paint
事件。
在Paint事件中,调用当前DrawingGrid
class实例的publicDrawGrid()
方法,传递Graphics
对象canvas控制.
private DrawingGrid drawingGrid = null;
private void btnDrawGrid_Click(object sender, EventArgs e)
{
if (!int.TryParse(txtRows.Text, out int gridRows)) return;
if (!int.TryParse(txtColumns.Text, out int gridColumns)) return;
if (!float.TryParse(txtCellSize.Text, out float cellSize)) return;
drawingGrid = new DrawingGrid(gridRows, gridColumns, cellSize);
gridCanvas.Invalidate();
}
private void gridCanvas_Paint(object sender, PaintEventArgs e)
{
if (drawingGrid == null) return;
drawingGrid.DrawGrid(e.Graphics);
}
DrawingGrid
class:
此 class 使用嵌套的 public class、GridCell
来定义网格的每个单元格。
List<GridCell>
包含创建 DrawingGrid
时生成的所有网格单元 - 使用传递给 class 构造函数的值 - 调用私有 BuildGrid()
方法。
生成的单元格列表由 DrawingGrid
class 的 public readonly Grid
属性 公开。
Font
属性 默认为 SystemInformation.MenuFont
而 TextColor
属性 为 Color.DimGray
.
这两个属性的值可以随时更改。
▶ 定义网格的 RectangleF
个对象的集合是使用 Graphics.DrawRectangles() 方法绘制的。
▶ 每个单元格(每个矩形)的文本使用 TextRenderer.DrawText() 绘制。
设置其 TextFormatFlags 选项,文本在单元格内垂直和水平居中 (TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter
),文本默认填充被删除 (TextFormatFlags.NoPadding
)。
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
public class DrawingGrid
{
private TextFormatFlags flags = TextFormatFlags.VerticalCenter |
TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding;
public DrawingGrid(int rows, int columns, float cellSize)
{
this.Grid = new List<GridCell>(rows * columns);
BuildGrid(rows, columns, cellSize);
}
public List<GridCell> Grid { get; }
public Font Font { get; set; } = SystemInformation.MenuFont;
public Color TextColor { get; set; } = Color.DimGray;
private void BuildGrid(int rows, int columns, float size)
{
for (int c = 0; c < columns; c++) {
for (int r = 0; r < rows; r++) {
Grid.Add(new GridCell(new RectangleF(c * size, r * size, size, size), $"{r},{c}"));
}
}
}
public void DrawGrid(Graphics g)
{
g.DrawRectangles(Pens.Black, Grid.Select(gc => gc.Cell).ToArray());
foreach (var item in Grid) {
TextRenderer.DrawText(g, item.Text, Font, Rectangle.Round(item.Cell), TextColor, flags);
}
}
public class GridCell {
public GridCell(RectangleF cellBounds, string text) {
this.Cell = cellBounds;
this.Text = text;
}
public RectangleF Cell { get; }
public string Text { get; }
}
}
我正在尝试创建一个可以用宽度或高度指定的网格(即 10 个框宽 x 20 个高)。我已经创建了一个创建网格的脚本,但我想以一种可以创建不同宽度和高度的方式来创建网格。
它目前创建一个宽度与高度相等且不显示坐标的网格。
int numOfCells = 5;
int cellSize = 80;
Pen p = new Pen(Color.Black);
for (int y = 0; y < numOfCells; y++)
{
graphics.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
for (int x = 0; x < numOfCells; x++)
{
graphics.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
}
}
结果如下所示:
我想要实现的是像这样的网格,它可以有不同的宽度和高度,并在方框内显示坐标:
可自定义/可扩展的示例 Class 使用传递给其构造函数的参数生成网格。
class 还包含用于在设备上下文中绘制网格的方法(PaintEventArgs
参数的 Graphics
对象由 Paint
事件/OnPaint
控件的方法)。
作为基本实现,它允许指定在其单元格内绘制的文本的字体和颜色。
您可以添加更多属性,例如定义网格线的颜色和厚度。
▶ 在图形示例中,您可以看到三个 TextBox 控件(txtRows
、txtColumns
和 txtCellSize
)用于输入行数和列数以及单元格大小。
当按下 Button (btnDrawGrid
) 时,如果正确解析了 TextBoxes 的内容,则会生成一个新的 Grid 对象和用于呈现 Grid 的控件(这里是一个名为 [ 的 PictureBox 控件) =20=]) 被重新绘制,调用它的 Invalidate()
方法,引发它的 Paint
事件。
在Paint事件中,调用当前DrawingGrid
class实例的publicDrawGrid()
方法,传递Graphics
对象canvas控制.
private DrawingGrid drawingGrid = null;
private void btnDrawGrid_Click(object sender, EventArgs e)
{
if (!int.TryParse(txtRows.Text, out int gridRows)) return;
if (!int.TryParse(txtColumns.Text, out int gridColumns)) return;
if (!float.TryParse(txtCellSize.Text, out float cellSize)) return;
drawingGrid = new DrawingGrid(gridRows, gridColumns, cellSize);
gridCanvas.Invalidate();
}
private void gridCanvas_Paint(object sender, PaintEventArgs e)
{
if (drawingGrid == null) return;
drawingGrid.DrawGrid(e.Graphics);
}
DrawingGrid
class:
此 class 使用嵌套的 public class、GridCell
来定义网格的每个单元格。
List<GridCell>
包含创建 DrawingGrid
时生成的所有网格单元 - 使用传递给 class 构造函数的值 - 调用私有 BuildGrid()
方法。
生成的单元格列表由 DrawingGrid
class 的 public readonly Grid
属性 公开。
Font
属性 默认为 SystemInformation.MenuFont
而 TextColor
属性 为 Color.DimGray
.
这两个属性的值可以随时更改。
▶ 定义网格的 RectangleF
个对象的集合是使用 Graphics.DrawRectangles() 方法绘制的。
▶ 每个单元格(每个矩形)的文本使用 TextRenderer.DrawText() 绘制。
设置其 TextFormatFlags 选项,文本在单元格内垂直和水平居中 (TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter
),文本默认填充被删除 (TextFormatFlags.NoPadding
)。
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
public class DrawingGrid
{
private TextFormatFlags flags = TextFormatFlags.VerticalCenter |
TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding;
public DrawingGrid(int rows, int columns, float cellSize)
{
this.Grid = new List<GridCell>(rows * columns);
BuildGrid(rows, columns, cellSize);
}
public List<GridCell> Grid { get; }
public Font Font { get; set; } = SystemInformation.MenuFont;
public Color TextColor { get; set; } = Color.DimGray;
private void BuildGrid(int rows, int columns, float size)
{
for (int c = 0; c < columns; c++) {
for (int r = 0; r < rows; r++) {
Grid.Add(new GridCell(new RectangleF(c * size, r * size, size, size), $"{r},{c}"));
}
}
}
public void DrawGrid(Graphics g)
{
g.DrawRectangles(Pens.Black, Grid.Select(gc => gc.Cell).ToArray());
foreach (var item in Grid) {
TextRenderer.DrawText(g, item.Text, Font, Rectangle.Round(item.Cell), TextColor, flags);
}
}
public class GridCell {
public GridCell(RectangleF cellBounds, string text) {
this.Cell = cellBounds;
this.Text = text;
}
public RectangleF Cell { get; }
public string Text { get; }
}
}