using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace UI.Framework.Forms { public static class UIExtend { /// /// 刷新控件 /// /// /// public static void RefreshContral(this Control parent, int top = 10) { try { List controls = new List(); foreach (Control item in parent.Controls) { controls.Add(item); } parent.Controls.Clear(); foreach (Control item in controls) { parent.AddContral(item, top); } } catch (Exception) { } } /// /// 删除Control /// /// public static void RemoveContral(this Control cur) { try { var p = cur.Parent; var top = cur.Top; if (p == null) return; p.Controls.Remove(cur); if (p.Controls.Count > 0) p.RefreshContral(); } catch (Exception) { } } /// /// 添加Control /// /// 需要添加 /// 父容器 public static void AddContral(this Control parent, Control add, int top = 20) { try { int addwidth = add.Width; //每行多少个 int number = parent.Width / addwidth;//总宽度/控件宽度 if (number == 0) return; //每行间距 int left = parent.Width % addwidth / (number + 1);//总剩余空间/总个数 //计算出总宽度、总高度 int sumLeft = 0, sumpTop = 0; int rowIndex = parent.Controls.Count / number; sumpTop = top * rowIndex + add.Height * rowIndex + top; int leftIndex = parent.Controls.Count % number; sumLeft = left * leftIndex + addwidth * leftIndex + left; if (sumLeft == 0) sumLeft = 10; add.Parent = parent; add.Top = sumpTop; add.Left = sumLeft; parent.Controls.Add(add); } catch (Exception) { } } /// /// 添加Controls /// /// 需要添加集合 /// 父容器 public static void AddContrals(this Control parent, Control[] adds, int top = 20) { try { foreach (var add in adds) { int addwidth = add.Width + 10; //每行多少个 int number = parent.Width / addwidth;//总宽度/控件宽度 if (number == 0) return; //每行间距 int left = parent.Width % addwidth / (number + 2);//总剩余空间/总个数 //计算出总宽度、总高度 int sumLeft = 0, sumpTop = 0; int rowIndex = parent.Controls.Count / number; sumpTop = top * rowIndex + add.Height * rowIndex + top; int leftIndex = parent.Controls.Count % number; sumLeft = left * leftIndex + addwidth * leftIndex + left; add.Parent = parent; add.Top = sumpTop; add.Left = sumLeft; } parent.Controls.AddRange(adds); } catch (Exception) { } } } }