using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ryCommon { /// /// 自动排列控件 /// public class ListX { Control parent = null; private int _maxColCount = 1; /// /// 最大列数 /// public int MaxColCount { get { return _maxColCount>0? _maxColCount:1; } set { _maxColCount = value; } } /// /// 开始排列的坐标 /// public Point StartPoint{ get; private set; } = new Point(0, 0); /// /// 下一项的位置 /// public Point NowPoint { get; private set; } = new Point(0, 0); /// /// 下一项的右下角坐标 /// public Point RightBottomPoint { get; private set; } = new Point(0, 0); /// /// 竖向间隔像素 /// public int spaceY = 0; /// /// 横向间隔像素 /// public int spaceX = 0; /// /// 最小保留右边距,只对ReDraw2有效 /// public int RightSpace = 20; /// /// 自动排列控件构造函数 /// /// public ListX(Control _parent) { parent = _parent; } /// /// 自动排列控件构造函数 /// /// /// /// public ListX(Control _parent,int x,int y) { parent = _parent; StartPoint= new Point(x, y); NowPoint = StartPoint; RightBottomPoint = StartPoint; } /// /// 开始排列控件 /// public void ReDraw() { NowPoint = StartPoint; RightBottomPoint = StartPoint; InitAdd(true); for (int i=0;i /// 开始排列控件,当列数超过显示空间时,自动换行 /// public void ReDraw2() { NowPoint = StartPoint; RightBottomPoint = StartPoint; InitAdd(true); int max_item_width = 0; int max_item_height = 0; int curCol = 0; for (int i = 0; i < ControlList.Count; i++) { if(ControlList[i].Width> max_item_width) { max_item_width = ControlList[i].Width; } if (ControlList[i].Height > max_item_height) { max_item_height = ControlList[i].Height; } if (i == 0) { ControlList[i].Location = new Point(StartPoint.X, StartPoint.Y); } else { ControlList[i].Location = NowPoint; } if (RightBottomPoint.X < NowPoint.X + ControlList[i].Width) { RightBottomPoint = new Point(NowPoint.X + ControlList[i].Width, RightBottomPoint.Y); } if (RightBottomPoint.Y < NowPoint.Y + ControlList[i].Height) { RightBottomPoint = new Point(RightBottomPoint.X, NowPoint.Y + ControlList[i].Height); } if (curCol >= MaxColCount - 1) { NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + max_item_height);//开始新的一行 max_item_width = 0; max_item_height = 0; curCol = 0; } else { if (NowPoint.X + spaceX + ControlList[i].Width+ ControlList[i].Width >= parent.Width- RightSpace)//超出宽度 { curCol = 0; NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + max_item_height);//开始新的一行 max_item_width = 0; max_item_height = 0; } else { curCol++; NowPoint = new Point(NowPoint.X + spaceX + ControlList[i].Width, NowPoint.Y); } } } InitAdd(false); } /// /// 加入的控件列表 /// public List ControlList { get;private set; } = new List(); /// /// 初始化添加,自动将容器设置为滚动条自动显示 /// /// public void InitAdd(bool isInit) { if (parent is Form) { ((Form)parent).AutoScroll = !isInit; } else if (parent is Panel) { ((Panel)parent).AutoScroll = !isInit; } else if (parent is TabPage) { ((TabPage)parent).AutoScroll = !isInit; } } /// /// 添加控件 /// /// /// public int Add(Control ctl) { InitAdd(true); ctl.Parent = parent; int curCol = ControlList.Count % MaxColCount; ctl.Location = NowPoint; parent.Controls.Add(ctl); ControlList.Add(ctl); InitAdd(false); if (RightBottomPoint.X< NowPoint.X+ctl.Width) { RightBottomPoint = new Point(NowPoint.X + ctl.Width, RightBottomPoint.Y); } if (RightBottomPoint.Y < NowPoint.Y + ctl.Height) { RightBottomPoint = new Point(RightBottomPoint.X, NowPoint.Y + ctl.Height); } if (curCol == MaxColCount - 1) //已经是该行最后一项 { NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + ctl.Height); } else { NowPoint = new Point(NowPoint.X + spaceX + ctl.Width, NowPoint.Y); } return ControlList.Count; } /// /// 删除控件 /// /// public void Delete(int index) { parent.Controls.Remove(ControlList[index]); ControlList.RemoveAt(index); ReDraw(); if (ControlList.Count == 0) { NowPoint = StartPoint; RightBottomPoint = StartPoint; } } /// /// 删除控件 /// /// public void Delete(Control ctl) { ControlList.Remove(ctl); parent.Controls.Remove(ctl); ReDraw(); if (ControlList.Count == 0) { NowPoint = StartPoint; RightBottomPoint = StartPoint; } } /// /// 清空所有控件 /// public void Clear() { parent.Controls.Clear(); ControlList.Clear(); NowPoint = StartPoint; RightBottomPoint = StartPoint; } } }