PageDesign/PageDesignWinform/PageDesign/Tools.cs
春风过客 c8ce32195d 1 增加打开软件自动安装字体“方正楷体简体”,若无法安装则从文件中读取字体并应用到控件中
2 增加根据打开电脑的分辨率自动适配大小的功能,避免更改大小时控件闪烁严重的问题
3 对于重复输入的配置信息,不录入配置文件
2024-02-02 15:34:34 +08:00

112 lines
3.8 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;
using System.Runtime.InteropServices;
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(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);
}
}
}
}
}
}