提交
This commit is contained in:
49
DHSoftware/Utils/AdaptiveHelper.cs
Normal file
49
DHSoftware/Utils/AdaptiveHelper.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AntdUI;
|
||||
|
||||
namespace DHSoftware.Utils
|
||||
{
|
||||
public static class AdaptiveHelper
|
||||
{
|
||||
#region 控件大小随窗体大小等比例缩放
|
||||
|
||||
|
||||
|
||||
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;
|
||||
if (con.Controls.Count > 0) setTag(con);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setControls(float newx, float newy, Control cons)
|
||||
{
|
||||
//遍历窗体中的控件,重新设置控件的值
|
||||
foreach (Control con in cons.Controls)
|
||||
//获取控件的Tag属性值,并分割后存储字符串数组
|
||||
if (con.Tag != null)
|
||||
{
|
||||
var mytag = con.Tag.ToString().Split(';');
|
||||
//根据窗体缩放的比例确定控件的值
|
||||
con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
|
||||
con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
|
||||
con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
|
||||
con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
|
||||
var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小
|
||||
if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
|
||||
con.Focus();
|
||||
if (con.Controls.Count > 0) setControls(newx, newy, con);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
155
DHSoftware/Utils/ConfigManager.cs
Normal file
155
DHSoftware/Utils/ConfigManager.cs
Normal file
@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Windows.Forms;
|
||||
using DH.Commons.Enums;
|
||||
using DH.Devices.Devices;
|
||||
using DH.Devices.PLC;
|
||||
|
||||
namespace DH.Commons.Helper
|
||||
{
|
||||
// 配置数据模型
|
||||
public class AppConfig
|
||||
{
|
||||
public List<CameraBase> Cameras { get; set; } = new List<CameraBase>();
|
||||
public List<PLCBase> PLCs { get; set; } = new List<PLCBase>();
|
||||
public List<DetectionConfig> Detections { get; set; } = new List<DetectionConfig>();
|
||||
}
|
||||
|
||||
// 配置管理工具类
|
||||
public static class ConfigManager
|
||||
{
|
||||
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
IgnoreNullValues = true
|
||||
};
|
||||
|
||||
// 默认路径配置
|
||||
private static readonly string DefaultConfigDir = Path.Combine(
|
||||
Application.StartupPath,
|
||||
"configs"
|
||||
);
|
||||
|
||||
public static readonly string DefaultConfigPath = Path.Combine(
|
||||
DefaultConfigDir,
|
||||
"appsettings.json"
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置文件(自动处理目录和备份)
|
||||
/// </summary>
|
||||
/// <param name="config">配置对象</param>
|
||||
/// <param name="filePath">可选文件路径</param>
|
||||
public static void SaveConfig(AppConfig config, string filePath = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 使用默认路径如果未指定
|
||||
filePath ??= DefaultConfigPath;
|
||||
|
||||
// 确保配置目录存在
|
||||
var configDir = Path.GetDirectoryName(filePath);
|
||||
if (!Directory.Exists(configDir))
|
||||
{
|
||||
Directory.CreateDirectory(configDir);
|
||||
}
|
||||
|
||||
// 备份已有配置
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
BackupConfig(filePath);
|
||||
}
|
||||
|
||||
// 序列化并保存
|
||||
string json = JsonSerializer.Serialize(config, _jsonOptions);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("配置保存失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载配置文件
|
||||
/// </summary>
|
||||
/// <param name="filePath">可选文件路径</param>
|
||||
/// <returns>配置对象</returns>
|
||||
public static AppConfig LoadConfig(string filePath = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
filePath ??= DefaultConfigPath;
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
return new AppConfig(); // 返回空配置而不是null
|
||||
}
|
||||
|
||||
string json = File.ReadAllText(filePath);
|
||||
return JsonSerializer.Deserialize<AppConfig>(json, _jsonOptions);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("配置加载失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建带时间戳的备份文件
|
||||
/// </summary>
|
||||
private static void BackupConfig(string originalPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 创建备份目录
|
||||
var backupDir = Path.Combine(
|
||||
Path.GetDirectoryName(originalPath),
|
||||
"backups"
|
||||
);
|
||||
|
||||
if (!Directory.Exists(backupDir))
|
||||
{
|
||||
Directory.CreateDirectory(backupDir);
|
||||
}
|
||||
|
||||
// 生成带时间戳的文件名
|
||||
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||
string fileName = $"{Path.GetFileNameWithoutExtension(originalPath)}_" +
|
||||
$"{timestamp}" +
|
||||
$"{Path.GetExtension(originalPath)}";
|
||||
|
||||
// 执行备份
|
||||
File.Copy(
|
||||
originalPath,
|
||||
Path.Combine(backupDir, fileName),
|
||||
overwrite: true
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("配置备份失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建新的配置对象
|
||||
/// </summary>
|
||||
public static AppConfig CreateConfig(
|
||||
List<CameraBase> cameras = null,
|
||||
List<PLCBase> plcs = null,
|
||||
List<DetectionConfig> detections = null)
|
||||
{
|
||||
return new AppConfig
|
||||
{
|
||||
Cameras = cameras ?? new List<CameraBase>(),
|
||||
PLCs = plcs ?? new List<PLCBase>(),
|
||||
Detections = detections ?? new List<DetectionConfig>()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user