GDI+扫雷棋盘网格 绘制
本帖最后由 dvmudev 于 2017-7-31 21:43 编辑效果图
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyERP.Code
{
public static class ShowGrid
{
//高度
public static int BlockHeight = 50;
//宽度
public static int BlockWidth = 50;
//宽度
public static int BlockOffset = 5;
//行数
public static int GridRowCount = 5;
//列数
public static int GridColumnCount = 10;
//边框
public static int BorderSize = 5;
//边框颜色
public static Color BorderColor = Color.Blue;
//空白颜色
public static Color EmptyColor = Color.White;
//通过颜色
public static Color SucceedColor = Color.Green;
//石板颜色
public static Color FailedColor = Color.Red;
//警告颜色
public static Color WarningColor = Color.Yellow;
//背景颜色
public static Color BackgroundColor = Color.GhostWhite;
//字体
public static Font BlockFont = new Font("微软雅黑", 12);
//颜色
public static Color BlockColor = Color.Black;
//横向方向
public static bool GridColumnFirst = true;
//画板控件
public static Control GridBoard { get; set; }
//Graphics
private static Graphics GridGraphics { get; set; }
static int index = 0;
public static void InitializeGrid(Control gridBoard)
{
index = 0;
GridBoard = gridBoard;
if (GridBoard == null) return;
GridGraphics = GridBoard.CreateGraphics();
GridGraphics.Clear(BackgroundColor);
for (int i = 0; i < GridColumnCount; i++)
{
for (int j = 0; j < GridRowCount; j++)
{
GridBlock gridBlock = new GridBlock();
gridBlock.BlockIndex = index;
index += 1;
gridBlock.BlockResult = ResultType.Empty;
gridBlock.BlockText = gridBlock.BlockIndex.ToString();
PaintBlock(gridBlock);
}
}
}
public static void PaintBlock(GridBlock gridBlock)
{
if (GridBoard == null) return;
if (GridGraphics == null) return;
int x = BlockOffset;
int y = BlockOffset;
//根据序号取得当前Index所对应位置
if (GridColumnFirst)
{
x += (gridBlock.BlockIndex % GridColumnCount) * (BlockWidth + BlockOffset);
y += (gridBlock.BlockIndex / GridColumnCount) * (BlockHeight + BlockOffset);
}
else
{
x += (gridBlock.BlockIndex / GridRowCount) * (BlockWidth + BlockOffset);
y += (gridBlock.BlockIndex % GridRowCount) * (BlockHeight + BlockOffset);
}
//先画一个大的底块
GridGraphics.FillRectangle(new SolidBrush(BorderColor), new Rectangle(x, y, BlockWidth, BlockHeight));
x = x + BorderSize;
y = y + BorderSize;
int w = BlockWidth - (BorderSize * 2);
int h = BlockHeight - (BorderSize * 2);
//再画块
switch (gridBlock.BlockResult)
{
case ResultType.Empty:
GridGraphics.FillRectangle(new SolidBrush(EmptyColor), new Rectangle(x, y, w, h));
break;
case ResultType.Succeed:
GridGraphics.FillRectangle(new SolidBrush(SucceedColor), new Rectangle(x, y, w, h));
break;
case ResultType.Warning:
GridGraphics.FillRectangle(new SolidBrush(WarningColor), new Rectangle(x, y, w, h));
break;
case ResultType.Failed:
GridGraphics.FillRectangle(new SolidBrush(FailedColor), new Rectangle(x, y, w, h));
break;
default:
break;
}
//写入文字
GridGraphics.DrawString(gridBlock.BlockText, BlockFont, Brushes.Black, new Rectangle(x, y, w, h));
// GridBoard.Refresh();
}
public static void PaintBlock(int x,int y, Color color, string text)
{
GridGraphics.FillRectangle(new SolidBrush(BorderColor), new Rectangle(x, y, BlockWidth, BlockHeight));
x = x + BorderSize;
y = y + BorderSize;
int w = BlockWidth - (BorderSize * 2);
int h = BlockHeight - (BorderSize * 2);
GridGraphics.FillRectangle(new SolidBrush(color), new Rectangle(x, y, w, h));
//写入文字
GridGraphics.DrawString(text, BlockFont, Brushes.Black, new Rectangle(x, y, w, h));
// GridBoard.Refresh();
}
}
public class GridBlock
{
public int BlockIndex { get; set; }
public ResultType BlockResult { get; set; }
public string BlockText { get; set; }
}
public enum ResultType
{
Succeed = 0,
Warning = 1,
Failed = 2,
Empty = 3
}
}
转自:http://myerp.oicp.io:88/?p=701
页:
[1]