using System; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; using System.Net; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace PageDesign { public class Tools { /// /// 检测端口是否被占用 /// /// /// public static bool PortInUse(int port) { bool inUse = false; IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == port) { inUse = true; break; } } return inUse; // 返回true说明端口被占用 } /// /// 记录所有控件的宽、高、位置、字体等信息 /// /// public static void setTag(Control cons) { foreach (Control con in cons.Controls) { con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size + ";" + con.Name; if (con.Controls.Count > 0) { setTag(con); } } } /// /// 根据比例,调整控件的大小、位置、字体大小 /// /// /// public static void setControls(int width, int height, float ratio, Control cons) { Console.WriteLine(width + "*" + height); //遍历窗体中的控件,重新设置控件的值 foreach (Control con in cons.Controls) { //获取控件的Tag属性值,并分割后存储字符串数组 if (con.Tag != null) { string[] mytag = con.Tag.ToString().Split(new char[] { ';' }); //根据窗体缩放的比例确定控件的值 con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * ratio); //宽度 con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * ratio); //高度 if (con.Name == "lstIP" || con.Name == "lstPost") con.Height = (int)(con.Height * 1.2); if (Convert.ToInt32(Convert.ToSingle(mytag[2]) * ratio) + con.Width <= width) con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * ratio); //左边距 if(con.Name == "picWhite") { con.Left = width - con.Width; } if (Convert.ToInt32(Convert.ToSingle(mytag[3]) * ratio) + con.Height <= height) con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * ratio); //顶边距 if(con.Name == "picShip") { con.Top = height - con.Height; } Single currentSize = Convert.ToSingle(mytag[4]); if (Convert.ToSingle(mytag[4]) * ratio > 0) currentSize = Convert.ToSingle(mytag[4]) * ratio; //字体大小 con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit); if (con.Controls.Count > 0) { setControls(width, height, ratio, con); } } } } } }