using Newtonsoft.Json;
|
using System.Data;
|
using System.Diagnostics;
|
|
namespace LB_SmartVision.Forms.Pages
|
{
|
public partial class RunSettingPage : UserControl
|
{
|
ControlStateManager controlStateManager { get; set; }
|
|
public RunSettingPage()
|
{
|
InitializeComponent();
|
controlStateManager = new ControlStateManager(tableLayoutPanel);
|
lstControls.Items.Clear();
|
foreach (Control control in GetAllControls(tableLayoutPanel))
|
{
|
if (control is TextBox || control is CheckBox || control is ComboBox)
|
{
|
if (!string.IsNullOrEmpty(control.Name))
|
{
|
lstControls.Items.Add(control.Name);
|
}
|
}
|
}
|
}
|
|
// 获取所有子控件(递归)
|
private IEnumerable<Control> GetAllControls(Control control)
|
{
|
var controls = control.Controls.Cast<Control>();
|
return controls.SelectMany(ctrl => GetAllControls(ctrl)).Concat(controls);
|
}
|
|
// 保存所有控件状态(递归处理容器控件)
|
public void SaveControlsState(TableLayoutPanel tablePanel)
|
{
|
GlobalVar.ControlStates.Clear();
|
SaveControlsRecursive(tablePanel);
|
}
|
|
private void SaveControlsRecursive(Control parentControl)
|
{
|
foreach (Control control in parentControl.Controls)
|
{
|
// 递归处理容器控件
|
if (control.HasChildren && !(control is ComboBox)) // ComboBox有Children但不是容器控件
|
{
|
SaveControlsRecursive(control);
|
continue;
|
}
|
string key = $"{control.Name}_{control.GetType().Name}";
|
switch (control)
|
{
|
case TextBox textBox:
|
{
|
GlobalVar.ControlStates[key] = textBox.Text;
|
break;
|
}
|
case CheckBox checkBox:
|
{
|
GlobalVar.ControlStates[key] = checkBox.Checked;
|
break;
|
}
|
case ComboBox comboBox:
|
{
|
var comboData = new
|
{
|
Items = comboBox.Items.Cast<object>().ToList(),
|
SelectedIndex = comboBox.SelectedIndex
|
};
|
GlobalVar.ControlStates[key] = comboData;
|
break;
|
}
|
}
|
}
|
}
|
|
// 加载控件状态(递归处理容器控件)
|
public void LoadControlsState(TableLayoutPanel tablePanel)
|
{
|
LoadControlsRecursive(tablePanel);
|
}
|
|
private void LoadControlsRecursive(Control parentControl)
|
{
|
if (this.InvokeRequired)
|
{
|
this.Invoke(new Action(() => LoadControlsRecursive(parentControl)));
|
return;
|
}
|
foreach (Control control in parentControl.Controls)
|
{
|
if (control.HasChildren && !(control is ComboBox))
|
{
|
LoadControlsRecursive(control);
|
continue;
|
}
|
|
string key = $"{control.Name}_{control.GetType().Name}";
|
|
if (!GlobalVar.ControlStates.TryGetValue(key, out object value)) continue;
|
|
switch (control)
|
{
|
case TextBox textBox when value is string text:
|
{
|
textBox.Text = text;
|
break;
|
}
|
case CheckBox checkBox when value is bool isChecked:
|
{
|
checkBox.Checked = isChecked;
|
break;
|
}
|
case ComboBox comboBox when value != null:
|
{
|
try
|
{
|
// 动态解析ComboBox数据
|
var json = JsonConvert.SerializeObject(value);
|
var comboData = JsonConvert.DeserializeAnonymousType(json, new
|
{
|
Items = new List<object>(),
|
SelectedIndex = 0
|
});
|
|
comboBox.BeginUpdate();
|
comboBox.Items.Clear();
|
comboBox.Items.AddRange(comboData.Items.ToArray());
|
|
if (comboData.SelectedIndex >= 0 && comboData.SelectedIndex < comboBox.Items.Count)
|
{
|
comboBox.SelectedIndex = comboData.SelectedIndex;
|
}
|
comboBox.EndUpdate();
|
}
|
catch (Exception ex)
|
{
|
Debug.WriteLine($"加载ComboBox数据失败: {ex.Message}");
|
}
|
break;
|
}
|
}
|
}
|
}
|
|
// 保存到JSON文件
|
public void SaveToJson(string filePath = "")
|
{
|
try
|
{
|
SaveControlsState(tableLayoutPanel);
|
if (string.IsNullOrEmpty(filePath))
|
{
|
filePath = GlobalVar.allRunSettingStringPath;
|
}
|
var settings = new JsonSerializerSettings
|
{
|
Formatting = Formatting.Indented,
|
TypeNameHandling = TypeNameHandling.Auto // 处理多态类型
|
};
|
|
string json = JsonConvert.SerializeObject(GlobalVar.ControlStates, settings);
|
File.WriteAllText(filePath, json);
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show($"保存失败: {ex.Message}", "异常");
|
}
|
}
|
|
// 从JSON文件加载
|
public void LoadFromJson(string filePath = "")
|
{
|
try
|
{
|
if (string.IsNullOrEmpty(filePath))
|
{
|
filePath = GlobalVar.allRunSettingStringPath;
|
}
|
if (!File.Exists(filePath))
|
{
|
SaveToJson(filePath);
|
Debug.WriteLine($"文件不存在,已创建新文件: {filePath}");
|
}
|
|
string json = File.ReadAllText(filePath);
|
var settings = new JsonSerializerSettings
|
{
|
TypeNameHandling = TypeNameHandling.Auto // 处理多态类型
|
};
|
|
GlobalVar.ControlStates = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, settings);
|
LoadControlsState(tableLayoutPanel);
|
|
if (txtImagePath.Text.Trim() == "" || string.IsNullOrEmpty(txtImagePath.Text))
|
{
|
// 如果没有设置图片路径,则使用默认路径
|
txtImagePath.Text = GlobalVar.strApplicationPath;
|
SaveToJson(filePath);
|
LoadControlsState(tableLayoutPanel);
|
}
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show($"加载失败: {ex.Message}", "异常");
|
}
|
}
|
|
private void btnTest_Click(object sender, EventArgs e)
|
{
|
string searchControlName = txtTestSearch.Text;
|
if (searchControlName.StartsWith("txt"))
|
{
|
// 假设是 TextBox
|
TextBox textBox = controlStateManager.GetControl<TextBox>(searchControlName);
|
if (textBox != null)
|
{
|
MessageBox.Show($"找到控件: {textBox.Name}, 文本内容: {textBox.Text}");
|
}
|
else
|
{
|
MessageBox.Show($"未找到控件: {searchControlName}", "异常");
|
}
|
}
|
else if (searchControlName.StartsWith("ckb"))
|
{
|
// 假设是 CheckBox
|
CheckBox checkBox = controlStateManager.GetControl<CheckBox>(searchControlName);
|
if (checkBox != null)
|
{
|
MessageBox.Show($"找到控件: {checkBox.Name}, 状态: {checkBox.Checked}");
|
}
|
else
|
{
|
MessageBox.Show($"未找到控件: {searchControlName}", "异常");
|
}
|
}
|
else if (searchControlName.StartsWith("cmb"))
|
{
|
// 假设是 ComboBox
|
ComboBox comboBox = controlStateManager.GetControl<ComboBox>(searchControlName);
|
if (comboBox != null)
|
{
|
string selectedText = controlStateManager.GetComboBoxText(searchControlName);
|
MessageBox.Show($"找到控件: {comboBox.Name}, 选中项: {selectedText}");
|
}
|
else
|
{
|
MessageBox.Show($"未找到控件: {searchControlName}", "异常");
|
}
|
}
|
else
|
{
|
MessageBox.Show("请输入有效的控件名称前缀(txt、ckb、cmb)", "异常");
|
}
|
}
|
|
private void btnSave_Click(object sender, EventArgs e)
|
{
|
SaveToJson();
|
MessageBox.Show("控件状态已保存");
|
}
|
|
private void btnLoad_Click(object sender, EventArgs e)
|
{
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
// 设置文件对话框的属性
|
openFileDialog.Multiselect = false; // 不允许多选
|
// 设置文件过滤器,支持多种文件类型
|
openFileDialog.Filter = "Ini Files (*.json)|*.json|All Files (*.*)|*.*";
|
// 显示文件对话框
|
DialogResult result = openFileDialog.ShowDialog();
|
|
// 处理对话框返回结果
|
if (result == DialogResult.OK)
|
{
|
// 获取用户选择的文件名
|
string[] selectedFiles = openFileDialog.FileNames;
|
if (selectedFiles.Length > 0)
|
{
|
if (File.Exists(selectedFiles[0]))
|
{
|
LoadFromJson(selectedFiles[0]);
|
MessageBox.Show("控件状态已加载");
|
}
|
}
|
}
|
}
|
|
private void txtImagePath_MouseDoubleClick(object sender, MouseEventArgs e)
|
{
|
// 创建文件夹选择对话框
|
using (var folderDialog = new FolderBrowserDialog())
|
{
|
folderDialog.Description = "请选择文件夹";
|
folderDialog.ShowNewFolderButton = true; // 允许创建新文件夹
|
folderDialog.RootFolder = Environment.SpecialFolder.MyComputer; // 起始目录
|
|
// 显示对话框
|
DialogResult result = folderDialog.ShowDialog();
|
|
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderDialog.SelectedPath))
|
{
|
string selectedFolder = folderDialog.SelectedPath;
|
txtImagePath.Text = selectedFolder; // 设置文本框的值为选择的文件夹路径
|
}
|
}
|
}
|
|
ToolTip pathToolTip = new ToolTip()
|
{
|
ToolTipIcon = ToolTipIcon.Info, // 设置提示图标
|
UseAnimation = true, // 启用动画效果
|
UseFading = true // 启用淡入淡出效果
|
};
|
private void txtImagePath_MouseHover(object sender, EventArgs e)
|
{
|
pathToolTip.Show(txtImagePath.Text
|
, txtImagePath
|
, txtImagePath.Width / 2, txtImagePath.Height // 显示在控件底部中间
|
, 1000); // 显示1秒
|
}
|
|
private void txtImageQuality_TextChanged(object sender, EventArgs e)
|
{
|
if (int.TryParse(txtImageQuality.Text, out int quality))
|
{
|
if (quality < 0 || quality > 100)
|
{
|
MessageBox.Show("图像质量必须在0到100之间!", "异常");
|
txtImageQuality.Text = "100"; // 重置为默认值
|
}
|
}
|
else
|
{
|
MessageBox.Show("请输入有效的数字!", "异常");
|
txtImageQuality.Text = "100"; // 重置为默认值
|
}
|
}
|
}
|
}
|