c#Drawing绘制矩形方块

1.可根据行(x)和列(y)参数，生成几行几列的矩形方块

2.可根据方块总数(z)，和一行几列(y)参数，逐一排列生成方块

1.根据行和列生成长宽自适应的方块

public void DrawRectangle1(Panel pan, int x, int y)
{
int Page_Width = pan.Width;
int Page_Height = pan.Height;
int Single_Width = Page_Width / (2 * x + 1);
int Single_Height = Page_Height / (2 * y + 1);
Size size = new Size(Single_Width, Single_Height);

for (int i = 0; i < x * y; i++)
{
int xNum = (i + 1) % x;  // 第几列
int yNum = i / x + 1; //第几行
if (xNum == 0)
{
xNum = x;
}
Point point = new Point(((xNum * 2) - 1) * Single_Width, ((yNum * 2) - 1) * Single_Height);
Label lable = new Label();
lable.Location = point;
lable.Size = size;

lable.Tag = lable.Size.Width.ToString() + ',' + lable.Size.Height.ToString();
lable.BackColor = Color.Yellow;
lable.Name = "lable" + i.ToString();
lable.Visible = true;
}
}
View Code

2.根据总数和列数生成长度大小一样的方块

 1  public void DrawRectangle(Panel page, int x, int y)
2         {
3             int Page_Width = page.Width;
4             int Page_Height = page.Height;
5             int Single_Width = Page_Width / (2 * x + 1);
6
7             int Single_Height = Page_Height / (2 * 4 + 1);
8             Size size = new Size(Convert.ToInt32(35), Convert.ToInt32(35));
9             int a = 0;
10             for (int i = 0; i < x; i++)
11             {
12                 if (i % y == 0 && i != 0)
13                 {
14                     a++;
15                 }
16                 int Xnum = (i + 1) % x;
17                 int Ynum = i / x + 1;
18                 if (Xnum == 0)
19                 {
20                     Xnum = x;
21                 }
22                 Label lable = new Label();
23                 lable.Location = new Point(20 + i % y * 70, 20 + a * 70);
24                 lable.Size = size;
25                 lable.Tag = lable.Size.Width.ToString() + ',' + lable.Size.Height.ToString();
26                 lable.BackColor = Color.Yellow;
27                 lable.Name = "lable" + i.ToString();
28                 lable.Visible = true;
30             }
31         }
View Code

https://www.cnblogs.com/wl192/p/11511200.html

posted @ 2019-12-19 13:40  漫漫步子  阅读(7578)  评论(1编辑  收藏  举报