old_flsystem/类库/UI.Framework/Forms/UIExtend.cs

132 lines
4.0 KiB
C#

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
{
/// <summary>
/// 刷新控件
/// </summary>
/// <param name="parentPage"></param>
/// <param name="top"></param>
public static void RefreshContral(this Control parent, int top = 10)
{
try
{
List<Control> controls = new List<Control>();
foreach (Control item in parent.Controls)
{
controls.Add(item);
}
parent.Controls.Clear();
foreach (Control item in controls)
{
parent.AddContral(item, top);
}
}
catch (Exception)
{ }
}
/// <summary>
/// 删除Control
/// </summary>
/// <param name="cur"></param>
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)
{ }
}
/// <summary>
/// 添加Control
/// </summary>
/// <param name="add">需要添加</param>
/// <param name="parent">父容器</param>
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)
{ }
}
/// <summary>
/// 添加Controls
/// </summary>
/// <param name="add">需要添加集合</param>
/// <param name="parent">父容器</param>
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)
{ }
}
}
}