1. 程式人生 > >顯示二維表格的位置計算

顯示二維表格的位置計算

        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();
        }