PageDesign/PageDesignWinform/PageDesign/Tools.cs
2024-01-29 10:05:47 +08:00

121 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
namespace PageDesign
{
public class Tools
{
/// <summary>
/// 检测端口是否被占用
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
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说明端口被占用
}
/// <summary>
/// 记录所有控件的宽、高、位置、字体等信息
/// </summary>
/// <param name="cons"></param>
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);
}
}
}
/// <summary>
/// 根据比例,调整控件的大小、位置、字体大小
/// </summary>
/// <param name="ratio"></param>
/// <param name="cons"></param>
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 (Convert.ToInt32(Convert.ToSingle(mytag[3]) * ratio) + con.Height <= height)
con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * ratio); //顶边距
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);
}
//if (con.Name == "picShip")
//{
// Console.WriteLine("Name:picShip\nWidth:" + con.Width + "\nHeight:" + con.Height + "\nLeft:" + con.Left + "\nBottom:" + con.Bottom );
//}
//if (con.Name == "picWave")
//{
// Console.WriteLine("Name:picWave\nWidth:" + con.Width + "\nHeight:" + con.Height + "\nLeft:" + con.Left + "\nBottom:" + con.Bottom );
//}
//if (con.Name == "picWhite")
//{
// Console.WriteLine("Name:picWhite\nWidth:" + con.Width + "\nHeight:" + con.Height + "\nRight:" + con.Right + "\nTop:" + con.Top);
//}
//if (con.Name == "llblSipLogin")
//{
// Console.WriteLine("Name:llblSipLogin\nWidth:" + con.Width + "\nHeight:" + con.Height + "\nLeft:" + con.Left + "\nBottom:" + con.Bottom);
//}
}
}
}
}
}