显示二维表格的位置计算
Posted yansc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示二维表格的位置计算相关的知识,希望对你有一定的参考价值。
private void button1_Click(object sender, EventArgs e) { Size BOX_SIZE = new Size(90, 60); Point START_TOP_LEFT = new Point(100, 100); int COUNT = 5; var floorList = new List<string>() { "1", "2", "3", "4", "5~6" }; var floorDic = new Dictionary<string, List<string>>(); floorDic.Add("5~6", new List<string>() { "501a", "502a", "503", "504", "505", "506", "507" }); floorDic.Add("4", new List<string>() { "401", "402", "403", "404", "405"}); floorDic.Add("3", new List<string>() { "301", "302", "303", "304", "305", "306", "307", "308", "309", "310", "311", "312", "313", "304", "315", "316" }); floorDic.Add("2", new List<string>() { "201", "202", "203", "204", "205"}); floorDic.Add("1", new List<string>() { "门房", "商1", "商2", "商3", "商4", "商5", "商6", "商7", "商8", "商9", "商10", "商11", "商12", "商13", "商14", "商15", "商16", "商17" }); this.panel1.Controls.Clear(); int x = 0;//格子x,y 的左上坐标 int y = 0; int width = 0;//层格子的长、宽 int height = 0; int lines_per_floor = 0;//每层实际占几行(占几个格子) int real_floor = 0;//实际占几层 int idx_floor = 0;//层顺序 int delta_height = 0;//层格子累加的减少高度 int idx_room_x = 0;//指示当前X方向是第几个房间,用于扣减偏移量,每层清0、折行清0 int idx_room_y = 0;//累积的房间格子的高度偏移量,不清0 for (int i = floorList.Count - 1; i >= 0; i--) { x = START_TOP_LEFT.X; real_floor += lines_per_floor; y = START_TOP_LEFT.Y + real_floor * BOX_SIZE.Height - delta_height; y -= idx_floor;//每累加一层,所在y值-1,才能和上一个重合 width = BOX_SIZE.Width; lines_per_floor = (int)Math.Ceiling((double)floorDic[floorList[i]].Count / COUNT); delta_height += lines_per_floor - 1; height = BOX_SIZE.Height * lines_per_floor - (lines_per_floor - 1); //添加层格子 add_label_ctrl(x, y, new Size(width, height), floorList[i]); idx_room_x = 0; for (int j = 0; j < floorDic[floorList[i]].Count; j++) { //折行 if (j % COUNT == 0) { idx_room_x = 0; idx_room_y++; } x = START_TOP_LEFT.X + BOX_SIZE.Width + BOX_SIZE.Width * (j % COUNT);//X坐标=开始偏移 + 层格式宽 + 当前几个房间格子宽,因为折行,所以取余 x -= (idx_room_x + 1);//扣减重叠的格子线像素 y = START_TOP_LEFT.Y + BOX_SIZE.Height * real_floor + BOX_SIZE.Height * (j / COUNT);//Y坐标=开始偏移 + 实际占用层数* 格子高 + 当前第几行 * 房间格子高。因为折行,所以取商 y -= idx_room_y - 1;//摔跟头重叠的格子线像素 //添加房间格子 add_label_ctrl(x, y, BOX_SIZE, floorDic[floorList[i]][j]); idx_room_x++; } idx_floor++; } } void add_label_ctrl(int x, int y, Size size, string caption) { this.SuspendLayout(); var lbl = new Label(); lbl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; lbl.Location = new System.Drawing.Point(x, y); lbl.Name = Guid.NewGuid().ToString(); lbl.Size = size; lbl.Text = caption; lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.panel1.Controls.Add(lbl); this.ResumeLayout(); }
以上是关于显示二维表格的位置计算的主要内容,如果未能解决你的问题,请参考以下文章